1 / 11

Java

Java. Methods. Five-minute starter task. Write a method to output multiplication tables up to 12, e.g. 1 2 3 4 5 6 7 8 9 10 11 12 2 4 6 8 10 12 14 16 18 20 22 24 3 6 9 12 15 18 21 24 27 30 33 36 … 12 24 36 48 60 72 84 96 108 120 132 144

Download Presentation

Java

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. Java Methods

  2. Five-minute starter task • Write a method to output multiplication tables up to 12, e.g. 1 2 3 4 5 6 7 8 9 10 11 12 2 4 6 8 10 12 14 16 18 20 22 24 3 6 9 12 15 18 21 24 27 30 33 36 … 12 24 36 48 60 72 84 96 108 120 132 144 • This should really take you no more than five minutes!

  3. Methods • Methods are what objects can do • They are analogous to functions in VB • They have this structure: header String getStars(int n){ String s = ""; for (int i = 0; i<n; i++){ s += "*"; } return s;} parameter/argument return type Method name parameter/argument type return statement

  4. Task • Write the header for the method getAverage(), which takes two integer arguments m and n, and which returns a double. double getAverage(int m, int n)

  5. Calling methods class MethodDemo { void start(){ int x = 3; String stars = getStars(x); System.out.println(stars); } String getStars(int n){ String s = ""; for (int i = 0; i<n; i++){ s += "*"; } return s; } } method call Notice how, in the calling function, the int argument has the identifier 'x', but in the called method it is 'n'. This is fine. The called method has its own way of referring to variables.

  6. Scope class ScopeDemo { public int w = 1; // can be seen by any other class. Don't do this! int x = 2; // "Friendly" scope. Can be seen by any other class in the same package protected int y = 2; // can only be seen by the object and any of its subclasses private int z = 3; // can only be seen by the object itself void method(){ System.out.println(x); int x = 4; // Local scope. Can only be seen within this method. System.out.println(x); } } The output of the method is: 2 4

  7. Encapsulation  class Person { public String name; }  class Person { private String name; String getName(){ return name; } void setName(String s){ name = s; } } “Accessor” “Getter” “Mutator” “Setter”

  8. Constructors • Constructors are special methods that are called when an object is created public static void main(String[] args){ Person p = new Person("Jeff"); } class Person { private String name; Person(String s){ name = s; } } Note that the constructor doesn't specify a return type in the header. This is because its return type is always the object it finds itself in.

  9. Overloading constructors public static void main(String[] args){ Person p = new Person("Jeff"); Person q = new Person(); } class Person { private String name; Person(){ name = "John"; } Person(String s){ name = s; } } Here we have an overloaded constructor. If you call the constructor with no argument, you will end up with a person called "John". This allows for default values to be set.

  10. Overloading methods class Person { private String name; Person(String s){ name = s; } void speak(){ System.out.println("Hello"); } void speak(String s){ System.out.println(s); } void speak(int x){ System.out.println(String.valueOf(x)); } void speak(String s1, String s2){ System.out.println(s1); System.out.println(s2); } } Here we have an overloaded method speak(). You can establish a new overloaded method by having (a) a different argument type or (b) a different number of arguments (or both of course).

  11. Task • Construct a class Animal, that has an overloaded constructor allowing you to set its type and at least two overloaded speak() methods. • Give the animal a private instance variable name and make this variable editable through get and set

More Related