View
22
Download
0
Category
Tags:
Preview:
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
Five-minute starter taskFive-minute starter task
Write a method to output multiplication tables Write a method to output multiplication tables up to 12, e.g.up to 12, e.g.
1 2 3 4 5 6 7 8 9 10 11 121 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 242 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 363 6 9 12 15 18 21 24 27 30 33 36
… …
12 24 36 48 60 72 84 96 108 120 132 14412 24 36 48 60 72 84 96 108 120 132 144
This should really take you no more than five This should really take you no more than five minutes!minutes!
String getStars(int n){ String s = ""; for (int i = 0; i<n; i++){ s += "*"; } return s;}
MethodsMethods
Methods are what objects can Methods are what objects can dodo They are analogous to functions in VBThey are analogous to functions in VB They have this structure:They have this structure:
return type
Method name
parameter/argument
parameter/argument
typereturn
statement
header
TaskTask
Write the header for the method Write the header for the method getAverage()getAverage(), which takes two integer , which takes two integer arguments m and n, and which returns a arguments m and n, and which returns a double.double.
double getAverage(int m, int n)double getAverage(int m, int n)
Calling methodsCalling methods
class MethodDemo {class MethodDemo {
void start(){void start(){
int x = 3;int x = 3;
String stars = getStars(x);String stars = getStars(x);
System.out.println(stars);System.out.println(stars);
}}
String getStars(int n){String getStars(int n){String s = "";String s = "";
for (int i = 0; i<n; i++){for (int i = 0; i<n; i++){
s += "*";s += "*";
}}
return 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.
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);}
}
ScopeScope
The output of the method is:24
EncapsulationEncapsulation
class Person {public String name;
}
class Person {private String name;
String getName(){return name;
}
void setName(String s){name = s;
}}
“Accessor”
“Getter”
“Mutator”
“Setter”
ConstructorsConstructors
Constructors are special methods that are Constructors are special methods that are called when an object is createdcalled 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.
Overloading constructorsOverloading constructorspublic 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.
Overloading methodsOverloading methodsclass 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).
TaskTask
Construct a class Construct a class AnimalAnimal, that has an , that has an overloaded constructor allowing you to set overloaded constructor allowing you to set its type and at least two overloaded its type and at least two overloaded speak() speak() methods.methods.
Give the animal a private instance variable Give the animal a private instance variable namename and make this variable editable and make this variable editable through get and setthrough get and set
Recommended