1 / 16

More on methods

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…

yvon
Download Presentation

More on methods

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on methods More keywords: static, void, final, protected…

  2. 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…

  3. 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; } }

  4. 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)); } }

  5. 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; } }

  6. 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")); } }

  7. 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(""); } }

  8. 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…

  9. 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(""); } }

  10. 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(); } }

  11. 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!

  12. 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); } }

  13. 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); } }

  14. 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); } }

  15. 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; } }

  16. Make sure you know what makes sense and what doesn’t…

More Related