Comp1004 building better objects i
Download
1 / 38

Comp1004: Building Better Objects I - PowerPoint PPT Presentation


  • 111 Views
  • Uploaded on

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;

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 ' Comp1004: Building Better Objects I' - dafydd


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

Coming up
Coming up

  • Methods and Parameters

    • Why Parameterise?

    • Call by value, call by reference

  • Return Types

    • Methods as a Function

  • Overloading


Methods and parameters
Methods and Parameters


Why parameterise
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


Why parameterise1
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


Why parameterise2
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 passed into a method are called arguments

Values received by a method are called parameters.

Within the method they can be used like any other local variable


Why parameterise3
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


Why parameterise4
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


Why parameterise5
Why Parameterise?

public class Account{

int balance = 100;

public static void main(String[] args){

Account myAccountObject = new Account();

myAccountObject.withdraw(5, “SotonUni 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


Parameters primitives and objects
Parameters, Primitives and Objects

b

a

int a;

a = 10;

Elephant b;

b = new Elephant();

10

Elephant

int


Parameters primitives and objects1
Parameters, Primitives and Objects

public 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?


Parameters primitives and objects2
Parameters, Primitives and Objects

Visitors is a primitive, so when it is sent to a method it is pass by value

public 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

}


Parameters primitives and objects3
Parameters, Primitives and Objects

Visitors is a primitive, so when it is sent to a method it is pass by value

visitors

public 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

}

0

int


Parameters primitives and objects4
Parameters, Primitives and Objects

Visitors is a primitive, so when it is sent to a method it is pass by value

visitors

v

public 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

}

0

int

int

0


Parameters primitives and objects5
Parameters, Primitives and Objects

Visitors is a primitive, so when it is sent to a method it is pass by value

visitors

v

public 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

}

0

int

int

1


Parameters primitives and objects6
Parameters, Primitives and Objects

Visitors is a primitive, so when it is sent to a method it is pass by value

visitors

public 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

}

0

int


Parameters primitives and objects7
Parameters, Primitives and Objects

Visitors is a primitive, so when it is sent to a method it is pass by value

visitors

public 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

}

0

int

So this line will print “0”


Parameters primitives and objects8
Parameters, Primitives and Objects

elephant is an object reference, so when it is sent to a method it is pass by reference

public 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

}


Parameters primitives and objects9
Parameters, Primitives and Objects

elephant is an object reference, so when it is sent to a method it is pass by reference

elephant

public 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


Parameters primitives and objects10
Parameters, Primitives and Objects

elephant is an object reference, so when it is sent to a method it is pass by reference

elephant

e

public 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

Elephant


Parameters primitives and objects11
Parameters, Primitives and Objects

elephant is an object reference, so when it is sent to a method it is pass by reference

elephant

e

public 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

Elephant


Parameters primitives and objects12
Parameters, Primitives and Objects

elephant is an object reference, so when it is sent to a method it is pass by reference

elephant

public 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

So this line will print “false”


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



Methods as functions
Methods as Functions

  • One way to think about methods is like mathematical functions

Function

Inputs

Output


Return types1
Return types

What will happen?

public class CashRegister{

public static void main(String[] args){

CashRegisterreg = new CashRegister();

float p1 = 10.0;

reg.calcVAT(p1);

System.out.println(p1);

}

public void calcVAT(float price) {

price = price * 1.2;

}

}


Return types2
Return types

What will happen?

public class CashRegister{

public static void main(String[] args){

CashRegisterreg = 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.


Return types3
Return types

public class CashRegister{

public static void main(String[] args){

CashRegisterreg = new CashRegister();

float p1 = 10.0;

p1 = reg.calcVAT(p1);

System.out.println(p1);

}

public floatcalcVAT(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


Return types4
Return types

public class CashRegister{

public static void main(String[] args){

CashRegisterreg = 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!)


Can i return more than one thing
Can I return more than one thing?

public intgetAgeAndName(){

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


Collections
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



Variations on a method
Variations on a Method

public class CashRegister{

public static void main(String[] args){

CashRegisterreg = new CashRegister();

float p1 = 10.0;

p1 = reg.calcVAT(p1);

System.out.println(p1);

}

public float calcVAT(float price) {

return price * 1.2;

}

}

What if we wanted to pass the VAT rate as one of the parameters?


Variations on a method1
Variations on a Method

public class CashRegister{

public static void main(String[] args){

CashRegisterreg = 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);

}

}

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


This is called overloading
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

    • intcalcVAT(float price, char band) OK


This is called overloading1
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

    • intcalcVAT(float price, char band) OK

    • intcalcVAT(float price) not OK

clashes


Variations on a method2

Overloading

Variations on a Method

public class CashRegister{

public static void main(String[] args){

CashRegisterreg = 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);

}

}

When a method is called Java invokes the method with the matching signature


Summary
Summary

  • Methods and Parameters

    • Why Parameterise?

    • Call by value, call by reference

  • Return Types

    • Methods as a Function

  • Overloading


ad