more on methods n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
More on methods PowerPoint Presentation
Download Presentation
More on methods

Loading in 2 Seconds...

play fullscreen
1 / 16

More on methods - PowerPoint PPT Presentation


  • 45 Views
  • Uploaded on

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…

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

PowerPoint Slideshow about 'More on methods' - yvon


An Image/Link below is provided (as is) to download presentation

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 on methods

More keywords: static, void, final, protected…

a method is a function
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…
example

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
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
Other example

class StringReverser {

int i;

int length;

String answer;

StringReverser() {

i = 0;

length = 0;

answer = new String("");

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

}

public String reverseMessage(String message) {

answer = "";

length = message.length();

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

answer = answer + message.charAt(length - i - 1);

}

return answer;

}

}

other example continued
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
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("");

}

}

the key word static

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…
slide9

Here, for static variables, you can initialize upon declaration

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
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 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!
example1

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
  • 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;

private double radius;

Circle(double someRadius) {

radius = someRadius;

}

public void showInfo() {

System.out.println("Radius: " + radius);

System.out.println("Circumference: " + radius*2*pi);

System.out.println("Area: " + radius*radius*pi);

}

}

continued
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
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;

}

}