final-sample-1 - CSE 143 Sample Final Exam#1 1 Inheritance...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 143 Sample Final Exam #1 1. Inheritance and Polymorphism . Consider the following classes ( System.out.println has been abbreviated as S.o.pln ): public class Cup extends Box { public void method1() { S.o.pln("Cup 1"); } public void method2() { S.o.pln("Cup 2"); super.method2(); } } public class Pill { public void method2() { S.o.pln("Pill 2"); } } public class Jar extends Box { public void method1() { S.o.pln("Jar 1"); } public void method2() { S.o.pln("Jar 2"); } } public class Box extends Pill { public void method2() { S.o.pln("Box 2"); } public void method3() { method2(); S.o.pln("Box 3"); } } The following variables are defined: Box var1 = new Box(); Pill var2 = new Jar(); Box var3 = new Cup(); Box var4 = new Jar(); Object var5 = new Box(); Pill var6 = new Pill(); In the table below, indicate in the right-hand column the output produced by the statement in the left-hand column. If the statement produces more than one line of output, indicate the line breaks with slashes as in "a / b / c" to indicate three lines of output with "a" followed by "b" followed by "c". If the statement causes an error, fill in the right-hand column with the phrase "error" to indicate this. Statement var1.method2(); var2.method2(); var3.method2(); var4.method2(); var5.method2(); var6.method2(); var1.method3(); var2.method3(); var3.method3(); var4.method3(); ((Cup) var1).method1(); ((Jar) var2).method1(); ((Cup) var3).method1(); ((Cup) var4).method1(); ((Jar) var4).method2(); ((Box) var5).method2(); ((Pill) var5).method3(); ((Jar) var2).method3(); ((Cup) var3).method3(); ((Cup) var5).method3(); Output _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________ _________________________
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2. Inheritance and Comparable Programming . You have been asked to extend a pre-existing class Point that represents 2-D (x, y) coordinates. The Point class includes the following constructors and methods: Constructor/Method Description public Point() constructs the point (0, 0) public Point(int x, int y) constructs a point with the given x/y coordinates public void setLocation(int x, int y) sets the coordinates to the given values public int getX() returns the x-coordinate public int getY() returns the y-coordinate public String toString() returns a String in standard "(x, y)" notation public double distanceFromOrigin() returns the distance from the origin (0, 0), computed as the square root of (x 2 + y 2 ) You are to define a new class called Point3D that extends this class through inheritance . It should behave like a Point except that it should be a 3-dimensional point that keeps track of a z-coordinate.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern