Top Banner
Chapter 6 Methods 1
52

Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

May 22, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Chapter 6

Methods

1

Page 2: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Class and Method

Definitions

2

Class Name

Instance Variables

Methods

Page 3: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Java is Object Oriented

3

It can model any real world object

Page 4: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

A class is a blueprint of what an object will look like

4

Page 5: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

The object is just an instance of the class

5

Page 6: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Object Oriented Programming deals with the creation of

objects

6

and their relationships and interactions

Page 7: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Start by defining the class

7

Car

- bodyPaintColor: Color- numberOfTires: int

+ getBodyPaintColor(): Color + setBodyPaintColor(Color color): void + getNumberOfTires(): int

Use a UML class diagram

instance variables

instance methods

Page 8: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Code the class definition

8

public class Car {

private Color bodyPaintColor; private int numberOfTires; public Color getBodyPaintColor() { return bodyPaintColor; } // end getPaintColor()

public void setBodyPaintColor(Color color) { bodyPaintColor = color; } // end setPaintColor()

public int getNumberOfTires() { return numberOfTires; } // end getNumberOfTires()

public void setNumberOfTires(int tireCount) { numberOfTires = tireCount; } // end setNumberOfTires()} // end Car

instance variables

instance methods

Page 9: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

An Object consists of data ...

9

bodyPaintColor Color.Green

numberOfTires 4greenCar

object's memoryfootprint

Page 10: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

and operations that store and manage the data

10

bodyPaintColor Color.Green

numberOfTires 4

greenCar

bodyPaintColor Color.Red

numberOfTires 4

redCar

Class Car methods

methods are shared by all car objects

Page 11: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Each class should be in a separate file

11

public class Car { // code omitted} // end Car

Car.java

public class Driver { // code omitted} // end Driver

Driver.java

Page 12: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

new Creates an instance of a class

12

Car myCar = new Car();

Page 13: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Recap

▪ An object is an instance of class

▪ Use new to create an object

▪ Objects have data (instance variables)

▪ Objects offer functionality (methods)

13

Page 14: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

There are two types of methods

14

Methods that do not return a value (void)

System.out.println("println does not return");

and methods that do return a value

int num = keyboard.nextInt();

Page 15: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Let's see how methods work

15

Car myCar = new Car();

First create the object

bodyPaintColor null

numberOfTires 0myCar Default values

Page 16: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

You can then call a method to setan instance variable

16

myCar.setNumberOfTires(4);

bodyPaintColor null

numberOfTires 4myCar

Receiving Object

Page 17: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

or a get method to retrieve an instance variable

17

int tireCount = myCar.getNumberOfTires()

bodyPaintColor null

numberOfTires 4myCar

4

Page 18: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

this Demystified

18

public int getNumberOfTires() { return this.numberOfTires;} // end getNumberOfTires()

public void setNumberOfTires(int tireCount) { this.numberOfTires = tireCount;} // end setNumberOfTires()

Since each method is shared by all the objects, the methods need to be able to identify the receiving object.

this refers to the receiving object, implicitly.

Page 19: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

void Method Definition

19

public void setNumberOfTires(int tireCount) {

numberOfTires = tireCount;

} // end setNumberOfTires()

Method is accessible by defining class and any other

class

Parameter list can be empty or list

parameters needed

Instance Variable

Page 20: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

return Method Definition

20

public int getNumberOfTires() {

return numberOfTires;

} // end getNumberOfTires()

return type of int

Parameter list can be empty or list

parameters needed

Instance Variable

Page 21: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Recap

▪ Methods expose a class's functionality

▪ Call a method on a receiving object

▪ this identifies the receiving object inside the method's definition

▪ Each class is stored in its own .java file

21

Page 22: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Local variables are defined within a method

22

public double updateSumAmount(double amount) {

double newSumAmount += amount;

return newSumAmount;

} // end updateSumAmount()

local variable

Page 23: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Methods can definelocal variables named the same

23

public void method1() { double someDouble = 0;

// Code omitted

} // end method1()

public void method2() { double someDouble = 0;

// Code omitted

} // end method2()

local to method1

local to method2

Page 24: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Information Hiding and

Encapsulation

24

Page 25: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

A method should hide how it is implemented

25

I know what the method

does,just not how!

Page 26: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Know "what" a method does, not "how" it does it

26

Page 27: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Methods can be public

27

These define the class's interface

Page 28: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

or private

28

These are part of the implementation

Page 29: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Instance Variables are private

29

They define the implementation

Page 30: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Accessor methods controlaccess to instance variables

30

Getters retrieve instance variables

Setters set instance variables

Page 31: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Recap

▪ Local variables are defined within a method

▪ Know "what" a method does, not "how" it does it

▪ Public methods define the class's interface

▪ Private instance variables/methods are part of the implementation

31

Page 32: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Class Deconstructed <Fraction>

32

Fraction

- numerator: int- denominator: int

- reduce(): void+ getNumerator(): int + setNumerator(int n): void + getDenominator(): int + setDenominator(int d): void + setNumeratorAndDenominator(int n, int d): void + add(Fraction f): Fraction + subtract(Fraction f): Fraction

Page 33: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Application Deconstructed<Fraction.java>

package fractiondemo;

public class Fraction { private int numerator; private int denominator;

33

private void reduce() { int u = numerator; int v = denominator; int temp; while (v != 0) { temp = u % v; u = v; v = temp; }// end while numerator /= u; denominator /= u; }// end reduce()

Page 34: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Application Deconstructed<Fraction.java>

public int getNumerator() { return numerator; }// end getNumerator()

34

public void setNumerator(int n) { setNumeratorAndDenominator(n, denominator); }// end setNumerator()

public int getDenominator() { return denominator; }// end getDenominator()

public void setDenominator(int d) { setNumeratorAndDenominator(numerator, d); }// end setDenominator()

Page 35: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Application Deconstructed<Fraction.java>

public void setNumeratorAndDenominator(int n, int d) { numerator = n; if (d == 0) { System.err.println("ERROR: Invalid parameter (" + d + ") in setNumeratorAndDenonimator"); System.exit(1); } else { denominator = d; }// end if }// end setNumeratorAndDenominator()

35

public Fraction add(Fraction f) { Fraction sum = new Fraction(); sum.setNumeratorAndDenominator(numerator * f.denominator + denominator * f.numerator, denominator * f.denominator); sum.reduce(); return sum;

Page 36: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Application Deconstructed<Fraction.java>

public Fraction subtract(Fraction f) { Fraction difference = new Fraction(); difference.setNumeratorAndDenominator( numerator * f.denominator - denominator * f.numerator, denominator * f.denominator); difference.reduce(); return difference; }// end subtract()

36

public Fraction multiply(Fraction f) { Fraction product = new Fraction(); product.setNumeratorAndDenominator( numerator * f.numerator, denominator * f.denominator); product.reduce(); return product; }// end multiply()

Page 37: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Application Deconstructed<Fraction.java>

public Fraction divide(Fraction f) { Fraction division = new Fraction(); division.setNumeratorAndDenominator( numerator * f.denominator, denominator * f.numerator); division.reduce(); return division; }// end divide()

37

public void show() { System.out.print("(" + numerator + " / " + denominator + ")"); }// end show()}// end Fraction()

Page 38: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Application Deconstructed<FractionDemo.java>

package fractiondemo;

public class FractionDemo { public static void main(String[] args) { Fraction f1 = new Fraction(); Fraction f2 = new Fraction(); Fraction result = new Fraction(); // Set f1 to 1 / 4. f1.setNumeratorAndDenominator(1, 4); // Set f2 to 1 / 2. f2.setNumeratorAndDenominator(1, 2);

38

Page 39: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Application Deconstructed<FractionDemo.java>

39

// Output their sum, difference, product and division. result = f1.add(f2); f1.show(); System.out.print(" + "); f2.show(); System.out.print(" = "); result.show(); System.out.println(); result = f1.subtract(f2); f1.show(); System.out.print(" - "); f2.show(); System.out.print(" = "); result.show(); System.out.println();

Page 40: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Application Deconstructed<FractionDemo.java>

40

result = f1.multiply(f2); f1.show(); System.out.print(" * "); f2.show(); System.out.print(" = "); result.show(); System.out.println(); result = f1.divide(f2); f1.show(); System.out.print(" / "); f2.show(); System.out.print(" = "); result.show(); System.out.println(); }// end main()}// end FractionDemo

Page 41: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Application Deconstructed<FractionDemo.java>

41

Page 42: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Objects and References

42

Page 43: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

There are two types of variables

43

Value: Stores the actual value

1

Reference: Stores a reference to the actual value

2

Page 44: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Value types store values

44

int x = 100; 100x

Page 45: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Reference types store references

45

Fraction f = new Fraction();

2040

.

.

fnumerator ?denominator ?

.

.

2040

Page 46: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Lets compare value types

46

int x = 100; x 100

int y = 200; y 200

x == y ? false

x = y; x 200

y 200

x == y ? true

Page 47: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Now lets compare reference types

47

f1 == f2 ? false

f1 == f2 ? true

Fraction f1 = new Fraction();f1.setNumeratorAndDenominator(1,2);

f1 200numerator 1 denominator 2

200

Fraction f2 = new Fraction();f2.setNumeratorAndDenominator(1,2);

f2 208numerator 1 denominator 2

208

f1 = f2;f1 208

f2 208numerator 1 denominator 2

208

Page 48: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

The solution to the == problem?

48

Define an equals method

Page 49: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Code Deconstructed <equals method>

public boolean equals(Fraction f) {

return this.numerator == f.numerator && this.denominator == f.denominator;

}// end equals()

49

Two fractions are equal if both their numerator and denominator values are the same.

Page 50: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Code Deconstructed <equals method>

Fraction f1 = new Fraction();f1.setNumeratorAndDenominator(1, 2);

Fraction f2 = new Fraction();f2.setNumeratorAndDenominator(1, 2);

50

if (f1 == f2) System.out.println("Both variables refer to the same object");else System.out.println("Each variable refers to a different object"); if ( f1.equals(f2) ) System.out.println("Both objects have the same value");else System.out.println("Each object has a different value");

Page 51: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Code Deconstructed <Object variables as parameters>

51

Fraction f1 = new Fraction();f1.setNumeratorAndDenominator(1,2);

f1 200numerator 1 denominator 2

200

Fraction f2 = new Fraction();f2.setNumeratorAndDenominator(1,2);

f2 208numerator 1 denominator 2

208

if (f1.equals(f2) {...}...public boolean equals(Fraction f) {...}

f2 208numerator 1 denominator 2

208

f 208

Both the argument (f2) and the parameter (f) point to the same object.

Notice how the parameter (f) refers to the same object as the argument (f2) and thus object can be changed from within the method.

Page 52: Chapter 6 Methods - SIUEstornar/courses/notes/cs140/06.Methods.pdf · Chapter 6 Methods 1. Class and Method Definitions 2 Class Name Instance Variables Methods. Java is Object Oriented

Code Deconstructed <Object variables as parameters>

52

Fraction f1 = new Fraction();f1.setNumeratorAndDenominator(1,2);

f1 200numerator 1 denominator 2

200

resetFraction(f1);...

public void resetFraction(Fraction f)

f1 200numerator 1 denominator 2

200

f 200

Notice here how the object variable (f) has been assigned a new object, and that f1 stills refers to its original object.

{ f = new Fraction(); f.setNumeratorAndDenominator(1, 1);}

f1 200numerator 1 denominator 2

200

f 208numerator 1 denominator 1

208