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

Slides:



Advertisements
Similar presentations
So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.
Advertisements

Chapter 6 Introduction to Defining Classes
Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
Inheritance Inheritance Reserved word protected Reserved word super
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Lecture 7 Polymorphism. Review: Constructors ●Parenthesis after constructor, not in class declaration ●The constructor makes the SamBot – do not “ new.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Lecture 9 Polymorphism Richard Gesick.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
Quiz 1 What is this? (explain the use of the reserved word “this” in a class method). Answer each question briefly. – What is a Constructor? –Under what.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Clicker questions 10/24/13 CSE 1102 Fall θ (x,y) (0,0) What is the relationship between θ and (x,y) ? A.y = mx + b B.θ = x 2 + y 2 C.tan(θ) = x/y;
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods.
© 2006 Pearson EducationPolymorphism 1 of 22 POLYMORPHISM Polymorphism Partial Overriding OOP Review.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Gayle J Yaverbaum School of Business Administration Penn State Harrisburg Fall 2006 Object-Oriented Design and Program Development in Business INFSY
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Inheritance and Polymorphism
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Clicker quiz 9/3/13 CSE 1102 Fall Look out behind you! The picture associated with one of the instructors on Piazza includes an animal. That animal.
Clicker quiz 9/17/13 CSE 1102 Fall // In Blob: public void mousePressed(MouseEvent e){ this.setFillColor(java.awt.Color.blue); } // in WinkingBlob.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Clicker questions 10/3/13 CSE 1102 Fall public class { … private _ ; public ) { … _ = ; … } … } In the above code, what is the purpose of the parameter.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Clicker quiz 10/1/13 CSE 1102 Fall public class Sun extends Ellipse { 2. public Sun(Color c) { 3. super(c); 4. … 5. } 6. public Sun() { 7. this(Color.yellow);
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Gayle J Yaverbaum School of Business Administration Penn State Harrisburg Fall 2006 Object-Oriented Design and Program Development in Business INFSY
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Advanced Programming in Java
Advanced Programming in Java
Clicker questions 10/3/13 CSE 1102 Fall 2013.
Interface, Subclass, and Abstract Class Review
Inheritance and Polymorphism
EKT 472: Object Oriented Programming
Intro To Classes Review
Agenda Warmup AP Exam Review: Litvin A2
Clicker quiz 10/1/13 CSE 1102 Fall 2013.
Lecture 23 Polymorphism Richard Gesick.
Defining Classes and Methods
Advanced Programming in Java
Advanced Programming in Java
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Java Programming Language
Abstract Classes and Interfaces
Review of Previous Lesson
Final and Abstract Classes
Agenda Inheritance Polymorphism Type compatibility Homework.
Abstract Classes and Interfaces
Presentation transcript:

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

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

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]

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

Clicker questions 10/8/13 CSE 1102 Fall 2013

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

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

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

The midterm exam

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

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?

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:

Multiple choice questions Use the following UML Diagram to answer the questions 10-19 below.

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

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

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.