comp1004: building better objects i
DESCRIPTION
Comp1004: Building Better Objects I. Methods. Coming up. Methods and Parameters Why Parameterise? Call by value, call by reference Return Types Methods as a Function Overloading. Methods and Parameters. Why Parameterise?. public class Account{ int balance = 100; - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/1.jpg)
Comp1004: Building Better Objects IMethods
![Page 2: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/2.jpg)
Coming up
• Methods and Parameters– Why Parameterise?– Call by value, call by reference
• Return Types– Methods as a Function
• Overloading
![Page 3: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/3.jpg)
Methods and Parameters
![Page 4: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/4.jpg)
Why Parameterise?public class Account{
int balance = 100;
public static void main(String[] args){Account myAccountObject = new Account();myAccountObject.withdrawFiver();myAccountObject.withdrawTenner();
}
public void withdrawFiver(){balance = balance - 5;
}
public void withdrawTenner(){int tenner = 10;balance = balance – 10;
}}
These two methods do almost the same thing. It is wasteful (inelegant?) to write them twice
![Page 5: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/5.jpg)
Why Parameterise?public class Account{
int balance = 100;
public static void main(String[] args){Account myAccountObject = new Account();myAccountObject.withdraw(5);myAccountObject.withdraw(10);
}
public void withdraw(int amount){balance = balance - amount;
}}
They can be replaced by a single method that behaves differently depending on what values are passed to it
![Page 6: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/6.jpg)
Why Parameterise?public class Account{
int balance = 100;
public static void main(String[] args){Account myAccountObject = new Account();myAccountObject.withdraw(5);myAccountObject.withdraw(10);
}
public void withdraw(int amount){balance = balance - amount;
}}
Values received by a method are called parameters.
Within the method they can be used like any other local variable
Values passed into a method are called arguments
![Page 7: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/7.jpg)
Why Parameterise?public class Account{
int balance = 100;
public static void main(String[] args){Account myAccountObject = new Account();myAccountObject.withdraw(5);myAccountObject.withdraw(10);myAccountObject.withdraw(“ten pounds”);myAccountObject.withdraw(‘5’);
}
public void withdraw(int amount){balance = balance - amount;
}}
Each parameter is typed. You will get a compiler error if you try and pass a method a value of the wrong type
![Page 8: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/8.jpg)
Why Parameterise?public class Account{
int balance = 100;
public static void main(String[] args){Account myAccountObject = new Account();myAccountObject.withdraw(5);myAccountObject.withdraw(10);myAccountObject.withdraw(“ten pounds”);myAccountObject.withdraw(‘5’);
}
public void withdraw(int amount){balance = balance - amount;
}}
Each parameter is typed. You will get a compiler error if you try and pass a method a value of the wrong type
“ten pounds” is of type String
‘5’ is of type char
So these lines will not compile
![Page 9: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/9.jpg)
Why Parameterise?public class Account{
int balance = 100;
public static void main(String[] args){Account myAccountObject = new Account();myAccountObject.withdraw(5, “Soton Uni Shop”);myAccountObject.withdraw(10, “ATM”);
}
public void withdraw(int amount, String desc){balance = balance - amount;System.out.print(“Withdrew £”);System.out.print(amount);System.out.print(“ via ”);System.out.println(desc);
}}
Methods can take multiple parameters
Each is separated by a comma, and has its own name and type
![Page 10: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/10.jpg)
Parameters, Primitives and Objects
b
Elephant
a
int
10int a;a = 10;
Elephant b;b = new Elephant();
![Page 11: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/11.jpg)
Parameters, Primitives and Objectspublic class Zoo{
public static void main(String[] args){ int visitors = 0;
Elephant elephant = new Elephant();
incVisitors(visitors);feedElephant(elephant);
System.out.println(visitors);System.out.println(elephant.isHungry());}
public void incVisitors(int v) {if(zooIsOpen()) v++;}
public void feedElephant(Elephant e) {if(zooHasFood()) e.feed();}
//some code omitted}
Assuming that isHungry returns true or false depending on whether the elephant has been fed, and that the zoo is open and has food - what will be printed here?
![Page 12: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/12.jpg)
Parameters, Primitives and Objectspublic class Zoo{
public static void main(String[] args){ int visitors = 0;
Elephant elephant = new Elephant();
incVisitors(visitors);feedElephant(elephant);
System.out.println(visitors);System.out.println(elephant.isHungry());}
public void incVisitors(int v) {if(zooIsOpen()) v++;}
public void feedElephant(Elephant e) {if(zooHasFood()) e.feed();}
//some code omitted}
Visitors is a primitive, so when it is sent to a method it is pass by value
![Page 13: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/13.jpg)
Parameters, Primitives and Objectspublic class Zoo{
public static void main(String[] args){ int visitors = 0;
Elephant elephant = new Elephant();
incVisitors(visitors);feedElephant(elephant);
System.out.println(visitors);System.out.println(elephant.isHungry());}
public void incVisitors(int v) {if(zooIsOpen()) v++;}
public void feedElephant(Elephant e) {if(zooHasFood()) e.feed();}
//some code omitted}
Visitors is a primitive, so when it is sent to a method it is pass by value
visitors
int
0
![Page 14: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/14.jpg)
Parameters, Primitives and Objectspublic class Zoo{
public static void main(String[] args){ int visitors = 0;
Elephant elephant = new Elephant();
incVisitors(visitors);feedElephant(elephant);
System.out.println(visitors);System.out.println(elephant.isHungry());}
public void incVisitors(int v) {if(zooIsOpen()) v++;}
public void feedElephant(Elephant e) {if(zooHasFood()) e.feed();}
//some code omitted}
Visitors is a primitive, so when it is sent to a method it is pass by value
visitors
int
0
v
int
0
![Page 15: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/15.jpg)
Parameters, Primitives and Objectspublic class Zoo{
public static void main(String[] args){ int visitors = 0;
Elephant elephant = new Elephant();
incVisitors(visitors);feedElephant(elephant);
System.out.println(visitors);System.out.println(elephant.isHungry());}
public void incVisitors(int v) {if(zooIsOpen()) v++;}
public void feedElephant(Elephant e) {if(zooHasFood()) e.feed();}
//some code omitted}
Visitors is a primitive, so when it is sent to a method it is pass by value
visitors
int
0
v
int
1
![Page 16: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/16.jpg)
Parameters, Primitives and Objectspublic class Zoo{
public static void main(String[] args){ int visitors = 0;
Elephant elephant = new Elephant();
incVisitors(visitors);feedElephant(elephant);
System.out.println(visitors);System.out.println(elephant.isHungry());}
public void incVisitors(int v) {if(zooIsOpen()) v++;}
public void feedElephant(Elephant e) {if(zooHasFood()) e.feed();}
//some code omitted}
Visitors is a primitive, so when it is sent to a method it is pass by value
visitors
int
0
![Page 17: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/17.jpg)
Parameters, Primitives and Objectspublic class Zoo{
public static void main(String[] args){ int visitors = 0;
Elephant elephant = new Elephant();
incVisitors(visitors);feedElephant(elephant);
System.out.println(visitors);System.out.println(elephant.isHungry());}
public void incVisitors(int v) {if(zooIsOpen()) v++;}
public void feedElephant(Elephant e) {if(zooHasFood()) e.feed();}
//some code omitted}
Visitors is a primitive, so when it is sent to a method it is pass by value
visitors
int
0
So this line will print “0”
![Page 18: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/18.jpg)
Parameters, Primitives and Objectspublic class Zoo{
public static void main(String[] args){ int visitors = 0;
Elephant elephant = new Elephant();
incVisitors(visitors);feedElephant(elephant);
System.out.println(visitors);System.out.println(elephant.isHungry());}
public void incVisitors(int v) {if(zooIsOpen()) v++;}
public void feedElephant(Elephant e) {if(zooHasFood()) e.feed();}
//some code omitted}
elephant is an object reference, so when it is sent to a method it is pass by reference
![Page 19: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/19.jpg)
Parameters, Primitives and Objectspublic class Zoo{
public static void main(String[] args){ int visitors = 0;
Elephant elephant = new Elephant();
incVisitors(visitors);feedElephant(elephant);
System.out.println(visitors);System.out.println(elephant.isHungry());}
public void incVisitors(int v) {if(zooIsOpen()) v++;}
public void feedElephant(Elephant e) {if(zooHasFood()) e.feed();}
//some code omitted}
elephant is an object reference, so when it is sent to a method it is pass by reference
elephant
Elephant
![Page 20: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/20.jpg)
Parameters, Primitives and Objectspublic class Zoo{
public static void main(String[] args){ int visitors = 0;
Elephant elephant = new Elephant();
incVisitors(visitors);feedElephant(elephant);
System.out.println(visitors);System.out.println(elephant.isHungry());}
public void incVisitors(int v) {if(zooIsOpen()) v++;}
public void feedElephant(Elephant e) {if(zooHasFood()) e.feed();}
//some code omitted}
elephant is an object reference, so when it is sent to a method it is pass by reference
elephant
Elephant
e
Elephant
![Page 21: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/21.jpg)
Parameters, Primitives and Objectspublic class Zoo{
public static void main(String[] args){ int visitors = 0;
Elephant elephant = new Elephant();
incVisitors(visitors);feedElephant(elephant);
System.out.println(visitors);System.out.println(elephant.isHungry());}
public void incVisitors(int v) {if(zooIsOpen()) v++;}
public void feedElephant(Elephant e) {if(zooHasFood()) e.feed();}
//some code omitted}
elephant is an object reference, so when it is sent to a method it is pass by reference
elephant
Elephant
e
Elephant
![Page 22: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/22.jpg)
Parameters, Primitives and Objectspublic class Zoo{
public static void main(String[] args){ int visitors = 0;
Elephant elephant = new Elephant();
incVisitors(visitors);feedElephant(elephant);
System.out.println(visitors);System.out.println(elephant.isHungry());}
public void incVisitors(int v) {if(zooIsOpen()) v++;}
public void feedElephant(Elephant e) {if(zooHasFood()) e.feed();}
//some code omitted}
elephant is an object reference, so when it is sent to a method it is pass by reference
elephant
Elephant
So this line will print “false”
![Page 23: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/23.jpg)
Sometimes (often), we (everyone) gets lazy and says we pass a method an object.
This really means we pass that object’s reference.
Just so you know
![Page 24: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/24.jpg)
Return Types
![Page 25: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/25.jpg)
Methods as Functions
• One way to think about methods is like mathematical functions
FunctionInputs Output
![Page 26: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/26.jpg)
Return typespublic class CashRegister{
public static void main(String[] args){ CashRegister reg = new
CashRegister();float p1 = 10.0;
reg.calcVAT(p1);System.out.println(p1);
}
public void calcVAT(float price) {price = price * 1.2;
}
}
What will happen?
![Page 27: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/27.jpg)
Return typespublic class CashRegister{
public static void main(String[] args){ CashRegister reg = new
CashRegister();float p1 = 10.0;
reg.calcVAT(p1);System.out.println(p1);
}
public void calcVAT(float price) {price = price * 1.2;
}
}
Because p1 is a float (a primitive) it is pass by value. So this line will not increase the value of p1.
The program will print 10.0 on the screen.
What will happen?
![Page 28: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/28.jpg)
Return typespublic class CashRegister{
public static void main(String[] args){ CashRegister reg = new
CashRegister();float p1 = 10.0;
p1 = reg.calcVAT(p1);System.out.println(p1);
}
public float calcVAT(float price) {return price * 1.2;
}
}
Instead we can specify a return type
And use the return keyword to pass back a value to wherever the method was called
![Page 29: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/29.jpg)
Return typespublic class CashRegister{
public static void main(String[] args){ CashRegister reg = new
CashRegister();float p1 = 10.0;
p1 = reg.calcVAT(p1);System.out.println(p1);
}
public float calcVAT(float price) {return price * 1.2;
}
}
Instead we can specify a return type
And use the return keyword to pass back a value to wherever the method was called
Whatever called the method can then assign the return type to a variable (or do anything else with it!)
![Page 30: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/30.jpg)
Can I return more than one thing?public int getAgeAndName(){
return age, name;}
• This is not legal Java• Like a mathematical function you can only
return one thing• So there can only be one return type• But....
![Page 31: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/31.jpg)
Collections
• Later in the course we deal with collections (implemented as classes and objects)
• You can put many objects or primitives into collections
• So you could pass or return a collection from a method in order to process many values at once
![Page 32: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/32.jpg)
Overloading
![Page 33: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/33.jpg)
public class CashRegister{
public static void main(String[] args){ CashRegister reg = new
CashRegister();float p1 = 10.0;
p1 = reg.calcVAT(p1);System.out.println(p1);
}
public float calcVAT(float price) {return price * 1.2;
}
}
Variations on a Method
What if we wanted to pass the VAT rate as one of the parameters?
![Page 34: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/34.jpg)
public class CashRegister{
public static void main(String[] args){ CashRegister reg = new
CashRegister();float p1 = 10.0;
p1 = reg.calcVAT(p1);p1 = reg.calcVAT(p1, 0.175);System.out.println(p1);
}
public float calcVAT(float price) {return price * 1.2;
}
public float calcVAT(float price, float rate) {
return price * (1.0 + rate); }
}
Variations on a Method
What if we wanted to pass the VAT rate as one of the parameters?
We could add it as a second parameter.
N.B. that the two methods have the same name
![Page 35: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/35.jpg)
This is called Overloading• A method is recognised by its signature
– (its name, parameters and the order of parameters)
• When overloading each method must have a unique signature– Remember that the return type is NOT part of the signature
– float calcVAT(float price) – float calcVAT(float price, float rate) OK– int calcVAT(float price, char band) OK
![Page 36: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/36.jpg)
This is called Overloading• A method is recognised by its signature
– (its name, parameters and the order of parameters)
• When overloading each method must have a unique signature– Remember that the return type is NOT part of the signature
– float calcVAT(float price) – float calcVAT(float price, float rate) OK– int calcVAT(float price, char band) OK– int calcVAT(float price) not OK
clashes
![Page 37: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/37.jpg)
public class CashRegister{
public static void main(String[] args){ CashRegister reg = new
CashRegister();float p1 = 10.0;
p1 = reg.calcVAT(p1);p1 = reg.calcVAT(p1, 0.175);System.out.println(p1);
}
public float calcVAT(float price) {return price * 1.2;
}
public float calcVAT(float price, float rate) {
return price * (1.0 + rate); }
}
Variations on a Method
When a method is called Java invokes the method with the matching signature
Overloading
![Page 38: Comp1004: Building Better Objects I](https://reader031.vdocuments.us/reader031/viewer/2022012917/56816140550346895dd0ad1e/html5/thumbnails/38.jpg)
Summary
• Methods and Parameters– Why Parameterise?– Call by value, call by reference
• Return Types– Methods as a Function
• Overloading