16 - Recall from last day CS133: Developing Programming...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Recall from last day CS133: Developing Programming Principles Lecture 16 Types, more method resolution, casting Object Person Student CoopStudent CS 133 Course Notes Lecture 16, Slide 2 1 2 Types • An object of a derived class is also an object of any ancestor class. Person Student CoopStudent CS 133 Course Notes Lecture 16, Slide 3 Method resolution rules Student kael = new CoopStudent("Kael", 4444); The reference (or static) type is Student The object (or dynamic) type is CoopStudent The Rules: marcus is a CoopStudent object marcus is a Student object marcus is a Person object CoopStudent marcus = new CoopStudent("Marcus",4321); The reference type determines whether a method can be called. The object type determines which class's implementation of the method is called. CS 133 Course Notes Lecture 16, Slide 4 ● ● 3 4 Types and method resolution • Suppose we do the following: Student kael = new CoopStudent("Kael", 4444); • Remember that CoopStudents are Students • What value is returned by kael.calcFees()? • Answer: ________________ CS 133 Course Notes Lecture 16, Slide 5 Using method resolution • Suppose we wish to keep track of all (4) UWaterloo students. • Some may be coop, others would be “just” students. • We would like to determine to total of all the fees for all these students. CS 133 Course Notes Lecture 16, Slide 6 5 6 Using method resolution (continued) // array declaration and allocation // fill array // (Kael, Mike are in coop, Chantelle, Marcus are not) Types and method resolution (continued) • What about the CoopStudent method setWorkTerm, as in kael.setWorkTerm(true); // determine totalFees • Answer: _________________ • Why? Course Notes Lecture 16, Slide 7 CS 133 Course Notes Lecture 16, Slide 8 CS 133 7 8 How to set the workterm? • We need to tell Java to treat kael like a CoopStudent, not just a Student • So, we can say: Casting • The same idea can be applied to numbers: double mass = 3.00; int massValue = mass; Instead, we should say: Question: What if mass = 3.67 above? CS 133 Course Notes Lecture 16, Slide 9 CS 133 Course Notes Lecture 16, Slide 10 9 10 Another inheritance example • Recall the Coordinate class: public class Coordinate { private int row; private int col; public Coordinate(int theRow, int theCol) { this.row = theRow; this.col = theCol; } public int getRow() { return this.row; } public int getCol() { return this.col; } } CS 133 Course Notes Lecture 16, Slide 11 Augmenting Coordinate • We could add some methods to Coordinate: public double distance(Coordinate other) { int xDiff = this.col – other.col; int yDiff = this.row – other.row; int sqDist = xDiff * xDiff + yDiff * yDiff; return Math.sqrt(sqDist); } public String toString() { String s = "(" + this.row + "," + this.col + ")"; return s; } public boolean equals(Coordinate other) { return (this.row == other.row) && (this.col == other.col); } CS 133 Course Notes Lecture 16, Slide 12 11 12 Extending the Coordinate class to include a colour import java.awt.Color; public class ColourCoordinate extends Coordinate { private static final Color DEFAULT_COLOUR = Color.BLACK; private Color colour; public ColourCoordinate(int row, int col, Color aColour) { super(row, col); this.colour = aColour; } public ColourCoordinate(int rows, int cols) { this(row, col, DEFAULT_COLOUR); } CS 133 Course Notes Lecture 16, Slide 13 ColourCoordinate class continued public Color getColour() { return this.colour; } public void setColour(Color aColour) { this.colour = aColour; } public String toString() { } CS 133 Course Notes Lecture 16, Slide 14 13 14 ColourCoordinate class continued public boolean sameColour( ColourCoordinate other) { return this.colour.equals(other.colour); } public boolean equals(ColourCoordinate other) { } } // end class Using the ColourCoordinate class: Valid or Invalid Coordinate coord0 = new Coordinate(5, 4); ColourCoordinate colour0 = new ColourCoordinate(0, 10, Color.RED); Coordinate coord1 = colour0; ______________ ColourCoordinate colour1 = coord0; ________ Object obj1 = colour0; _____________ Object obj2 = coord0; _____________ System.out.println(coord1.toString()); _____ System.out.println(colour0.toString()); ____ CS 133 Course Notes Lecture 16, Slide 15 CS 133 Course Notes Lecture 16, Slide 16 15 16 Using the ColourCoordinate class continued ColourCoordinate colour2 = new ColourCoordinate(7, 6, Color.BLUE); if (colour2.sameColour(colour0)) … _________ if (colour2.sameColour(coord0)) … __________ double d1 = coord1.distance(coord0); _______ double d2 = coord1.distance(colour0); ______ double d3 = colour0.distance(coord1); ______ double d4 = colour0.distance(colour2); _____ CS 133 Course Notes Lecture 16, Slide 17 Summary • Role of this and super in a derived (extended) class • Resolving method calls • Casting CS 133 Course Notes Lecture 16, Slide 18 17 18 ERROR: undefined OFFENDING COMMAND: STACK: ...
View Full Document

This note was uploaded on 07/17/2010 for the course CS 133 taught by Professor Kierstead during the Fall '07 term at Waterloo.

Ask a homework question - tutors are online