33
1 COS 260 DAY 5 Tony Gauvin

1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

Embed Size (px)

Citation preview

Page 1: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

1

COS 260 DAY 5

Tony Gauvin

Page 2: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

2

Agenda

• Questions? • 2nd Mini quiz

– Password “squareRootChess” – 6 questions and 1 extra credit

• Assignment 1 Due Next Class• Assignment 2 will be posted soon • Begin Object Interaction

Page 3: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

Object interaction

Creating cooperating objects

5.0

Page 4: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

4

A digital clock

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Page 5: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Source: http://www.autoevolution.com/news/lexus-ct-200h-disassembled-after-100000km-german-endurance-test-79389.html

Page 6: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

6

How to design a car?

• Divide and Conquer– Every engineering team has their part(s)– Wheels & tires– Engines & transmissions– Interiors– Electronics– Body and Paint

• Have to agree on standard interfaces

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Page 7: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

7

Abstraction and modularization

• Abstraction is the ability to ignore details of parts to focus attention on a higher level of a problem.

• Modularization is the process of dividing a whole into well-defined parts, which can be built and examined separately, and which interact in well-defined ways.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Page 8: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

8

Modularizing the clock display

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

One four-digit display?

Or two two-digit displays with similar behaviours?

Page 9: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

9

Implementation - NumberDisplay

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

public class NumberDisplay{ private int limit; private int value;

Constructor and methods omitted.}

Page 10: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

10

Implementation - ClockDisplay

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

public class ClockDisplay{ private NumberDisplay hours; private NumberDisplay minutes;

Constructor and methods omitted.}

Page 11: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

11

Object diagram

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Page 12: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

12

Class diagram

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Page 13: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

13

Primitive types vs. object types

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

32

object type

primitive type

SomeObject obj;

int i;

Page 14: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

14

Quiz: What is the output?

• int a;int b;a = 32;b = a;a = a + 1;System.out.println(b);

• Person a;Person b;a = new Person("Everett");b = a;a.changeName("Delmar");System.out.println(b.getName());

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Page 15: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

15

Primitive types vs. object types

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

32

ObjectType a;

int a;

ObjectType b;

32

int b;

b = a;

Page 16: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

16

Number Display behaviors

00 (or some other value) limit 00 Possible methods increment setValue hitLimit

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Page 17: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

17

Source code: NumberDisplay

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

public NumberDisplay(int rollOverLimit){ limit = rollOverLimit; value = 0;}

public void increment(){ value = (value + 1) % limit;}

Page 18: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

18

The modulo operator• The 'division' operator (/), when applied to

int operands, returns the result of an integer division.

• The 'modulo' operator (%) returns the remainder of an integer division.

• E.g., generally: 17 / 5 gives result 3, remainder 2

• In Java: 17 / 5 == 3 17 % 5 == 2 – what is largest passible remainder

• Some times called “clock math” Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Page 19: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

19

Quiz

• What is the result of the expression 8 % 3

• For integer n >= 0, what are all possible results of:

n % 5

• Can n be negative?

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Page 20: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

20

Source code: NumberDisplay

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

public String getDisplayValue(){ if(value < 10) { return "0" + value; } else { return "" + value; }}

Pad leading zeros for single digits

Page 21: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

21

Concepts• abstraction• modularization• classes define new types• class diagram

• object diagram• object references • object types• primitive types

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Page 22: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

22

Objects creating objects

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

public class ClockDisplay{ private NumberDisplay hours; private NumberDisplay minutes; private String displayString; public ClockDisplay() { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); … }}

Page 23: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

23

Objects creating objects

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

public NumberDisplay(int rollOverLimit);

in class NumberDisplay:

formal parameter

hours = new NumberDisplay(24);

in class ClockDisplay:

actual parameter

Page 24: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

24

ClockDisplay object diagram

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Page 25: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

25

Method calling

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

public void timeTick(){ minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay();}

Page 26: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

26

External method call

• external method callsminutes.increment();

object . methodName ( parameter-list )

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Page 27: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

27

Internal method call

• internal method callsupdateDisplay();

• No variable name is required.• this

– could be used as a reference to the invoking object, but not used for method calls.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Page 28: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

28

Internal method

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

/** * Update the internal string that * represents the display. */private void updateDisplay(){ displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue();}

Page 29: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

29

Method calls

• NB: A method call on another object of the same type would be an external call.

• ‘Internal’ means ‘this object’.• ‘External’ means ‘any other

object’, regardless of its type.

Page 30: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

30

null

• null is a special value in Java• Object fields are initialized to null by default.

• You can test for and assign null:

private NumberDisplay hours;

if(hours != null) { ... }

hours = null;

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Page 31: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

31

The debugger

• Useful for gaining insights into program behavior …

• … whether or not there is a program error.

• Set breakpoints.• Examine variables.• Step through code.

Page 32: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

32

The debugger

Page 33: 1 COS 260 DAY 5 Tony Gauvin. 2 Agenda Questions? 2 nd Mini quiz –Password “squareRootChess” –6 questions and 1 extra credit Assignment 1 Due Next Class

33

Concept summary

• object creation• overloading• internal/external method

calls• debugger

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling