Presentation is loading. Please wait.

Presentation is loading. Please wait.

Clicker quiz 10/8/13 CSE 1102 Fall 2013 (E-30 reminder!)

Similar presentations


Presentation on theme: "Clicker quiz 10/8/13 CSE 1102 Fall 2013 (E-30 reminder!)"— Presentation transcript:

1 Clicker quiz 10/8/13 CSE 1102 Fall 2013 (E-30 reminder!)

2 One of the things that allows us to use polymorphism is that the declared type and actual type of a variable may be different. NOTE: order of answers corrected In Java, the actual type of a parameter or variable’s value can be any concrete class that is the same as the declared type, or any subclass of the declared type (if the declared type is a class) any subclass of a class that implements the declared type (if the declared type is an interface) any class that implements the declared type (if the declared type is an interface) All of the above [correct] A and C above, but not B

3 Polymorphism is used when different classes we are modeling can do the same thing (i.e. respond to the same method calls). In Java, this can be implemented using either inheritance or interfaces. Choose the best reason below for choosing inheritance polymorphism there is no defined interface that works for all of the classes the classes are closely related in inheritance hierarchy; [correct] the classes are in the same containment hierarchy it is the kind of polymorphism that I understand the best [correct]

4 Class diagram from Ch. 5 redrawn as one diagram The inheritance hierarchy shown in this diagram includes: All of these classes UpButton, DownButton, LeftButton, and RightButton UpButton, DownButton, LeftButton, RightButton, and Drawbutton [correct] UpButton, DownButton, LeftButton, RightButton, Drawbutton, and Cursor UpButton, DownButton, LeftButton, RightButton, Cursor, and SketchApp

5 Clicker questions 10/8/13 CSE 1102 Fall 2013

6 Given this class diagram, suppose we execute the following code:
int ndw; Car min = new Minivan(); ... ndw = min.drivesOnRoad(); Where is the drivesOnRoad() that is used defined? In Minivan In Minivan or Car In Minivan, Car, or Vehicle [correct] In Car In Car or Vehicle

7 Given this class diagram, suppose there is a method pass, defined in Car as
public void pass(Car other){ (some code for body) } Suppose imp is an instance of Car (or one of its subtypes). When I invoke the above method using imp.pass(pokey), what must be true of the actual parameter pokey? Its actual type is Car Its actual type is Car, SportsCar, or Minivan [correct] Its actual type is Car or Vehicle It depends on the actual type of imp None of the above

8 code fragment: Garage gary = new Garage(); Car fineCar = new SportsCar(Color.red); ... gary.openDoor(); gary.parkCar(fineCar); gary.closeDoor(); Given the above class diagram and code fragment, what happens? The code compiles and executes fine Code fails at compile time, error in line 2 Code fails at compile time, error in line 5 [correct] Code fails at execution time, error in line 2 Code fails at execution time, error in line 5

9 The midterm exam

10 Possible structure Code listing, followed by questions about the code
Multiple choice questions, like clicker questions Short answer questions Write some code to match some description

11 On which lines are the constructor(s) for UpButton defined?
File DrawButton.java: [1] import wheels.users.*; [2] import java.awt.Point; [3] public abstract class DrawButton extends Ellipse { [4] private Cursor _cursor; // peer object [5] public DrawButton(int x, int y, Cursor cursor) { [6] super(x,y); [7] _cursor = cursor; [8] this.setSize(20,20); [9] } [10] public void mousePressed(java.awt.event.MouseEvent e){ [11] this.setFillColor(java.awt.Color.blue); [12] } [13] public void mouseReleased(java.awt.event.MouseEvent e){ [14] Point lastPoint = _cursor.getLocation(); [15] Point nextPoint = this.computeNextPoint(lastPoint); [16] Line line = new Line(lastPoint, nextPoint); [17] line.setColor(java.awt.Color.black); [18] _cursor.setLocation(nextPoint); [19] this.setFillColor(java.awt.Color.red); [20] } [21] public abstract Point computeNextPoint(Point p); [22] } File UpButton.java: [23] import wheels.users.*; [24] import java.awt.Point; [25] public class UpButton extends DrawButton { [26] public UpButton(int x, int y, Cursor cursor) { [27] super(x, y, cursor); [28] } [29] public Point computeNextPoint(Point pointNow) { [30] return new Point(pointNow.x, pointNow.y - 5); [31] } [32] } What does line 27 do? What does line 8 do? Where does the setSize method come from? To what does this refer? On which lines are the constructor(s) for UpButton defined? For each of the following, identify all examples in the program, giving their names and the line numbers where they are declared in the program. Instance variables: Local variables: Abstract methods: Suppose we have an instance of UpButton, and the mouse is pressed over it. What (if anything) happens--what methods are invoked, what happens on screen, and what changes occur?

12 Point nextPoint = this.computeNextPoint(lastPoint);
File DrawButton.java: [1] import wheels.users.*; [2] import java.awt.Point; [3] public abstract class DrawButton extends Ellipse { [4] private Cursor _cursor; // peer object [5] public DrawButton(int x, int y, Cursor cursor) { [6] super(x,y); [7] _cursor = cursor; [8] this.setSize(20,20); [9] } [10] public void mousePressed(java.awt.event.MouseEvent e){ [11] this.setFillColor(java.awt.Color.blue); [12] } [13] public void mouseReleased(java.awt.event.MouseEvent e){ [14] Point lastPoint = _cursor.getLocation(); [15] Point nextPoint = this.computeNextPoint(lastPoint); [16] Line line = new Line(lastPoint, nextPoint); [17] line.setColor(java.awt.Color.black); [18] _cursor.setLocation(nextPoint); [19] this.setFillColor(java.awt.Color.red); [20] } [21] public abstract Point computeNextPoint(Point p); [22] } File UpButton.java: [23] import wheels.users.*; [24] import java.awt.Point; [25] public class UpButton extends DrawButton { [26] public UpButton(int x, int y, Cursor cursor) { [27] super(x, y, cursor); [28] } [29] public Point computeNextPoint(Point pointNow) { [30] return new Point(pointNow.x, pointNow.y - 5); [31] } [32] } Suppose we have a Cursor named foiledAgain. Write the Java statement that will instantiate an UpButton located at point (200, 300) that is associated with that Cursor. Briefly explain the purpose of each underlined word or phrase in the specified statements from the program. 2 points for each word or phrase. Point nextPoint = this.computeNextPoint(lastPoint); Point: nextPoint: this: computeNextPoint: lastPoint:

13 Multiple choice questions
Use the following UML Diagram to answer the questions below.

14 Multiple choice questions (ctd.)
10. If sporty is an instance of Car, what gets called if you execute sporty.move()? A. The move method defined in Vehicle B. The move method defined in PeopleHauler C. The move method defined in Car D. All of the above E. None of the above 11. Suppose the move method of OilTanker has the line super.move(). Which move method does that refer to? B. The move method defined in PeopleHauler C. The move method defined in Car D. All of the above E. None of the above

15 Short answer questions
Consider the similarities and differences between abstract classes and interfaces. Give two similarities and two differences. Give an example of when we might want to use polymorphism

16 Write some code Give a simple class definition for Owl. It should have Color and Location attributes (with associated accessor, but not mutator, methods), two constructors (one which takes Location and Color parameters, the other which takes a Location parameter only and sets the Color attribute to java.awt.Color.white). Its capabilities are flyTo, which takes a Location parameter (for its destination), and eat, which takes a parameter of type Rodent, and returns something of type OwlPellet. Follow the conventions we have been using in this course! You may continue your answer onto the next page.


Download ppt "Clicker quiz 10/8/13 CSE 1102 Fall 2013 (E-30 reminder!)"

Similar presentations


Ads by Google