java object-oriented programming 1€¦ · cs 6452: prototyping interactive systems 5 class car int...

38
Java Object-oriented Programming 1

Upload: others

Post on 18-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

Java Object-oriented Programming 1

Page 2: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

HWs Redux

• HW 3 • HW 4

2

Page 3: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Learning Objectives

• Java classes and objects − Instance data − Methods − Constrcutors − Visibility − Scope − Static

3

Page 4: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Modeling Objects

• Car − General attributes: year, color, VIN #,

horsepower, speed, mpg, … − Behaviors: drive, brake, wash, park

• Individual instances of a car − Hayley’s, Larry’s, …

4

Page 5: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems 5

class Car

int year; double mpg; Color col; …

drive()

wash()

main()

park()

Datadeclarations

Methods

A class is a “type”

All class members - Data (instance vars) - Methods

Page 6: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Instance Data

• Put values inside class but not in method • Each object that gets instantiated for a

class receives its own copy of them • Variables are automatically initialized, but

good practice to do it manually (in constructor)

6

Page 7: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems 7

Car c1 = new Car();

Use the new operator tocreate an instance

Access fields through the . operator

c1.year c1.vin c1.mpg c1.col c1.speed

c1

year

vin

mpg

col

speed

Page 8: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Methods

• Functions/procedures (behaviors) within a class

• When we do c1.drive(20); control flows to method, through it, then returns

8

public double drive(int time) { double distance;

distance = time * speed; return distance; }

Page 9: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Methods

• return statement – Control immediately goes back (need not be at end of method)

• Local variables – declared inside a method and only visible there (e.g., distance)

9

public double drive(int time) { double distance;

distance = time * speed; return distance; }

Page 10: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Methods

• Other code

• The method drive returns a double that added to 100.0 and copied into total

10

public double drive(int time) { double distance;

distance = time * speed; return distance; }

double total; total = 100.0 + c1.drive(24);

Page 11: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Methods

• Parameters – values passed in to method − Formal params – Names of params in header − Actual params – Values passed in when running

• Formal params are just local variables literally

11

public double drive(int time) { double distance;

distance = time * speed; return distance; }

Page 12: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Methods

• At execution time, values copied into formal parameters

• Parameters passed in call by value method

12

int a; a = 12; total = 100.0 + c1.drive(a+3);

// elsewhere

public double drive(int time) { time = 1; return time; }

Page 13: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Methods

• Nothing in front of speed • Which speed?

• The instance variable within the object upon which this method was called

13

public double drive(int time) { double distance;

distance = time * speed; return distance; }

Page 14: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Methods

14

double d; Car c3 = new Car(); d = c3.drive(50); // in this case, it uses c3’s speed

Other code

It’s like

or

or

distance = time * <thecallingobject>.speed;

distance = time * (c3).speed;

distance = time * this.speed;

Page 15: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Methods

• this – java reserved word used inside methods − It refers to object upon which method was

invoked

• These type of method calls always performed in the context of an object

15

distance = time * this.speed;

Page 16: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Example Program

• RollingDice − chap 4

16

Page 17: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Encapsulation

• Objects should be responsible for themselves

• Don’t want outsiders modifying instance data

• Specify certain methods for outsiders (other classes) to use − Called the class interface

17

Page 18: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems 18

class Car

int year; double mpg; Color col; …

instance data

client interface externally used methods

internally used methods

Page 19: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Visibility

• How do we specify what is externally visible? − Use modifiers

• Visibility modifiers – Control access − public, private, protected

19

outsiders only inclass

(later)

Page 20: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Access

20

public

variables

methods

private

X natural

service to clients

internalclass support

Class has access to all private members

Page 21: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems 21

public class Car { private int vin,year; private double speed, mpg;

public void drive() { … } public int getYear() { return year; }

public void setYear(int y) { year = y; }

public Car() { … }

private void diagnose() { … }

public static void main (String[] args) { … } }

Internal method

“Accessor” method

“Modifier” method

Constructor

Page 22: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Constructor

• Special method called when objects are instantiated

• Same name as the class • Their primary use is to initialize instance

variables

22

Page 23: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Constructors

• What if we did

23

public Car(int y, double s, double m) { year = y; speed = s; mpg = m; }

public Car(int year, double speed, double mpg) { year = year; speed = speed; mpg = mpg; }

Page 24: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Constructors

• How to correct that?

24

public Car(int year, double speed, double mpg) { this.year = year; this.speed = speed; this.mpg = mpg; }

Page 25: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Variable Scope

• What is the scope of a variable? − Region of a program where it's visible

• Formal parameter − The method in which it is a parameter

• Local variable − The method in which it is defined

• Instance variable − Entire class

25

Page 26: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Questions

• Legal?

• No, compile error − Two local variable declarations of a

26

public void foo(int a) { int a; … }

Page 27: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Method Overloading

• Use of same method name with different parameter lists to create multiple versions of method

• How does it know which is called? − Looks at call and matches − c1.drive(5, 23.4);

27

public int drive(int a) { … }

public int drive(int a, double d) { … }

Page 28: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Example Program

• Account & Transactions − chap 4

28

Page 29: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Static Variables

• Another modifier • So far, seen local vars and instance vars • Another kind: static (class) variable

− One copy shared by all instances of class − private static int count = 0; − Memory space for it is in class, not instances − Useful for object counters

29

Page 30: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Example Program

• Slogan − chap 6

30

Page 31: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Static Methods

• Do not operate in the context of a particular object (no this) − So they cannot reference instance variables − Typically worker functions, often mathematical

• Look at Slogan again − static getCount() cannot access phrase

31

Page 32: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

null

32

public class Worker { private String name; private int id;

public Worker(name, id) { this.name = name; this.id = id; } }

Worker w1; w1 = new Worker("Mary", 12);

After declaration, what is w1?null

After instantiation:

w1

Page 33: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Quiz

33

int x = 3; int y = 7; y = x;

Worker mary = new Worker("Mary", 3); Worker jane = new Worker("Jane", 7); jane = mary;

jane.id = 33;

System.out.println(mary.id);

mary

jane

Mary 3

Jane 7

Page 34: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Quiz

34

int x = 3; int y = 7; y = x;

Worker mary = new Worker("Mary", 3); Worker jane = new Worker("Jane", 7); jane = mary;

jane.id = 33;

System.out.println(mary.id);

mary

jane

Mary 3

Jane 7

Page 35: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Quiz

35

int x = 3; int y = 7; y = x;

Worker mary = new Worker("Mary", 3); Worker jane = new Worker("Jane", 7); jane = mary;

jane.id = 33;

System.out.println(mary.id);

mary

jane

Mary 33

Jane 7

Page 36: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Example Program

• RationalNumber − chap 6

36

Page 37: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Learning Objectives

• Java classes and objects − Instance data − Methods − Constrcutors − Visibility − Scope − Static

37

Page 38: Java Object-oriented Programming 1€¦ · CS 6452: Prototyping Interactive Systems 5 class Car int year; double mpg; Color col; … drive() wash() main() park() Data declarations

CS 6452: Prototyping Interactive Systems

Next Time

• More with classes and OOP − inheritance & hierarchies − interfaces − abstract classes − dynamic binding

38