More on methods

1 / 16

# More on methods - PowerPoint PPT Presentation

More on methods. More keywords: static, void, final, protected…. A method is a function. Like f(x) = 2x+1 When we give a value to x, f(x) is also equal to something, f(x) is the return value using a parameter x… A java method works the same way…

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'More on methods' - yvon

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### More on methods

More keywords: static, void, final, protected…

A method is a function
• Like f(x) = 2x+1
• When we give a value to x, f(x) is also equal to something, f(x) is the return value using a parameter x…
• A java method works the same way…
• We need to define a return type… int, double, Object, String, void, boolean, etc.
• We need to define the parameters…

Variable of type double that can contain decimals…

Constructor, initializing y

Specifying the return type

Method (or function) that returns the value of y, given an x

Parameter x of type double

Returning the answer using the keyword return

Example…
• Let’s code a object called Linear1 and let’s give it a method called calculate:

class Linear1 {

double y;

Linear1() {

y = 0;

System.out.println("Linear1 has been created");

}

public double calculate(double x) {

y = 2*x+1;

return y;

}

}

Using this…

public class Method1 {

public static void main(String[] args) {

Linear1 f;

f = new Linear1();

System.out.println("using a parameter: x = " + 2.0);

System.out.println("calculating: y = " + f.calculate(2.0));

}

}

Other example

class StringReverser {

int i;

int length;

StringReverser() {

i = 0;

length = 0;

System.out.println("a reverser has been created");

}

public String reverseMessage(String message) {

length = message.length();

for (i = 0; i < length; i++) {

}

}

}

Other example, continued…

public class Method2 {

public static void main(String[] args) {

StringReverser g;

g = new StringReverser();

System.out.println("attempting to reverse this: MyNameIs SCRUNGY");

System.out.println("the answer is: " + g.reverseMessage("MyNameIs SCRUNGY"));

}

}

The void return type
• Sometimes we do not need to return anything… Use the keyword void

class Employee {

private String name;

public int salary;

private int prevSal1;

private int prevSal2;

Employee(String givenName, int givenSalary) {

name = givenName;

salary = givenSalary;

prevSal1 = 0;

prevSal2 = 0;

}

public void changeSalary(int newSalary) {

prevSal2 = prevSal1;

prevSal1 = salary;

salary = newSalary;

}

public void displayInfo() {

System.out.println("Current salary: " + salary);

System.out.println("Previous salary: " + prevSal1);

System.out.println("Previous salary: " + prevSal2);

System.out.println("");

}

}

class Car {

static int count;

String name;

}

count

Car x name

Car y name

Car z name

The key word static…
• A static variable or method exists only once for all objects of the same class…
• A static variable will contain the data once for all objects…

Incrementing number, remember this when you see the execution

Example

class Car {

private String name;

private static int number = 0;

Car(String givenName) {

name = givenName;

number++;

}

public void showInfo() {

System.out.println("Name of the car: " + name);

System.out.println("Cars created: " + number);

System.out.println("");

}

}

Results…

public class StaticVar {

public static void main(String[] args) {

Car toyota = new Car("Echo");

Car honda = new Car("Civic");

Car ford = new Car("Junk");

toyota.showInfo();

honda.showInfo();

ford.showInfo();

}

}

Private vs Protected
• Private makes a variable or a method inaccessible for another user, class, etc…
• Protected has the same effect, except for child classes…
• Meaning a class that inherits or extends another one can access the protected members
• Use with care!

This will not compile because we are accessing secret which is private from a child class, but accessing category is ok

Example:

class Animal {

public String name;

protected String category;

private String secret;

Animal() {

name = new String();

category = new String();

secret = new String();

}

Animal(String n, String c, String s) {

name = n;

category = c;

secret = s;

}

}

class Mammal extends Animal {

public void show() {

System.out.println("name: " + name);

System.out.println("category: " + category);

System.out.println("secret: " + secret);

}

}

The keyword final
• The keyword final is used for constants, just to make sure that no one will change it…
• Ex:

class Circle {

public static final double pi = 3.14159;

}

public void showInfo() {

}

}

Continued…

public class Constants {

public static void main(String[] args) {

Circle a = new Circle(5.5);

a.showInfo();

System.out.println("");

System.out.println(a.pi);

}

}

But if we try this…

public class Constants {

public static void main(String[] args) {

Circle a = new Circle(5.5);

a.showInfo();

System.out.println("");

System.out.println(a.pi);

a.pi = 9.121212;

}

}