1 cos 260 day 19 tony gauvin. 2 agenda questions? 8 th mini quiz not corrected yet 9 th mini quiz...

18
1 COS 260 DAY 19 Tony Gauvin

Upload: hillary-waters

Post on 18-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

1

COS 260 DAY 19

Tony Gauvin

Page 2: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

2

Agenda• Questions?• 8th Mini Quiz not corrected yet• 9Th Mini Quiz next class

– Due November 19

• Finish Discussion on More on Inheritance

Page 3: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

3

Final Countdown

• Nov 16– Finish Chap 9

• Nov 19– 9th Mini Quiz– Assignment 5 due – Begin Chap 10

• Nov 23 – Finish Chapter 10

• Nov 30 – 10th Mini quiz– Begin Chap 11

• Dec 3– Finish Chapter 11– Assignment 6 due

• Dec 7– 11th mini quiz– Begin Chapter 12

• Dec 10– Finish Chapter 12 – Assignment 7 Due

• Dec 14 @ 10 AM– 12th mini quiz– Capstone Presentations

Copyright © 2014 Pearson Education, Inc. Slide 1-3

Page 4: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

4

Change to Grade Calculations

• 12 instead of 11 mini quizzes– Still best 10 @ 3% each

• 7 instead of 8 assignments– AVG(7) = 40% of final grade– Was 8 @ 5% each

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

Page 5: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

More about inheritance

Exploring polymorphism

5.0

Page 6: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

6

Question

• What is the difference between static type and dynamic type in JAVA variable assignments?

• What is method polymorphism>

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

Page 7: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

7

The instanceof operator

• Used to determine the dynamic type.

• Recovers ‘lost’ type information.• Usually precedes assignment with

a cast to the dynamic type:if(post instanceof MessagePost) { MessagePost msg = (MessagePost) post; … access MessagePost methods via msg …}

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

Page 8: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

8

The Object class’s methods

• Methods in Object are inherited by all classes.

• Any of these may be overridden.• The toString method is

commonly overridden:– public String toString()– Returns a string representation of

the object.

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

Page 9: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

9

Overriding toString in Post

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

public String toString(){ String text = username + "\n" + timeString(timestamp); if(likes > 0) { text += " - " + likes + " people like this.\n"; } else { text += "\n"; } if(comments.isEmpty()) { return text + " No comments.\n"; } else { return text + " " + comments.size() + " comment(s). Click here to view.\n"; }}

Page 10: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

10

Overriding toString

• Explicit print methods can often be omitted from a class:System.out.println(post.toString());

• Calls to println with just an object automatically result in toString being called:System.out.println(post);

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

Page 11: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

11

StringBuilder

• Consider using StringBuilder as an alternative to concatenation:

StringBuilder builder = new StringBuilder();builder.append(username);builder.append('\n');builder.append(timeString(timestamp));…return builder.toString();

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

Page 12: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

12

Object equality

• What does it mean for two objects to be ‘the same’?– Reference equality.– Content equality.

• Compare the use of == with equals() between strings.

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

Page 13: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

13

Overriding equals

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

public boolean equals(Object obj){ if(this == obj) { return true; } if(!(obj instanceof ThisType)) { return false; } ThisType other = (ThisType) obj; … compare fields of this and other}

Page 14: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

14

Overriding equals in Student

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

public boolean equals(Object obj){ if(this == obj) { return true; } if(!(obj instanceof Student)) { return false; } Student other = (Student) obj; return name.equals(other.name) && id.equals(other.id) && credits == other.credits;}

Page 15: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

15

Overriding hashCode in Student

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

/** * Hashcode technique taken from * Effective Java by Joshua Bloch. */public int hashCode(){ int result = 17; result = 37 * result + name.hashCode(); result = 37 * result + id.hashCode(); result = 37 * result + credits; return result;}

Page 16: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

16

Protected access

• Private access in the superclass may be too restrictive for a subclass.

• The closer inheritance relationship is supported by protected access.

• Protected access is more restricted than public access.

• We still recommend keeping fields private.– Define protected accessors and mutators.

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

Page 17: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

17

Access levels

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

Page 18: 1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More

18

Review

• The declared type of a variable is its static type.– Compilers check static types.

• The type of an object is its dynamic type.– Dynamic types are used at runtime.

• Methods may be overridden in a subclass.

• Method lookup starts with the dynamic type.

• Protected access supports inheritance.

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