Practice Quiz 2 - Object Oriented Programming and Design | C SC 335, Quizzes of Computer Science

Material Type: Quiz; Class: Object-Oriented Programming and Design; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Unknown 1989;

Typology: Quizzes

Pre 2010

Uploaded on 08/31/2009

koofers-user-dcx
koofers-user-dcx 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 335 Practice Quiz 2 Section Leader ___________ Name _________________________ 50pts
Given class Point and a test driver for class Circle and class Rectangle, establish a properly designed inheritance hierarchy. Let Shape be the abstract class. Implement all three classes to the right of the non-
inheritance version below. Completely implement all constructors and all methods. Include all instance variables. The Circle and Rectangle classes must behave exactly the same with the inheritance hierarchy as
without. The assertions must pass. (22pts)
@Test
public void test getArea() {
// 10 pixels over, 10 pixels down, radius 2.0
Shape c = new Circle(10, 10, 2.0);
asssertEquals(3.14159, c.getArea(), 0.001);
// width = 3.25, height = 5.75
Shape r = new Rectangle(40, 10, 3.25, 5.75);
sssertEquals(18.6875, r.getArea(), 0.001);
// width = 3.25, height = 5.75
Shape r2 = new Rectangle(40, 60, 2, 3);
sssertEquals(6.0, r.getArea(), 0.1);
}
public class Point { // Use this Point class
private int xPos;
private int yPos;
public Point(int x, int y) {
xPos = x;
yPos = y;
}
public int getX() {
return xPos;
}
public int getY() {
return yPos;
}
}
public class Rectangle {
private Point upperLeft;
private double width;
private double height;
public Rectangle(int x, int y,
double height, double width) {
upperLeft = new Point(x, y);
width = width;
height = height;
}
public int getX() {
return upperLeft.getX();
}
public int getY() {
return upperLeft.getY();
}
public double getArea() {
return width * height;
}
}
public class Circle {
private Point upperLeft;
private double radius;
public Circle(int x, int y,
double diameter) {
upperLeft = new Point(x,y);
radius = diameter / 2;
}
public int getX() {
return upperLeft.getX();
}
public int getY() {
return upperLeft.getY();
}
public double getArea() {
return Math.PI*Math.pow(radius, 2);
}
}
Completely implement the hierarchy here: Shape, Circle, and Rectangle
pf3
pf4
pf5

Partial preview of the text

Download Practice Quiz 2 - Object Oriented Programming and Design | C SC 335 and more Quizzes Computer Science in PDF only on Docsity!

C Sc 335 Practice Quiz 2 Section Leader ___________ Name _________________________ 50pts

Given class Point and a test driver for class Circle and class Rectangle, establish a properly designed inheritance hierarchy. Let Shape be the abstract class. Implement all three classes to the right of the non- inheritance version below. Completely implement all constructors and all methods. Include all instance variables. The Circle and Rectangle classes must behave exactly the same with the inheritance hierarchy as without. The assertions must pass. (22pts) @Test public void test getArea() { // 10 pixels over, 10 pixels down, radius 2. Shape c = new Circle(10, 10, 2.0); asssertEquals(3.14159, c.getArea(), 0.001); // width = 3.25, height = 5. Shape r = new Rectangle(40, 10, 3.25, 5.75); sssertEquals(18.6875, r.getArea(), 0.001); // width = 3.25, height = 5. Shape r2 = new Rectangle(40, 60, 2, 3); sssertEquals(6.0, r.getArea(), 0.1); } public class Point { // Use this Point class private int xPos; private int yPos; public Point(int x, int y) { xPos = x; yPos = y; } public int getX() { return xPos; } public int getY() { return yPos; } } public class Rectangle { private Point upperLeft; private double width; private double height; public Rectangle(int x, int y, double height, double width) { upperLeft = new Point(x, y); width = width; height = height; } public int getX() { return upperLeft.getX(); } public int getY() { return upperLeft.getY(); } public double getArea() { return width * height; } } public class Circle { private Point upperLeft; private double radius; public Circle(int x, int y, double diameter) { upperLeft = new Point(x,y); radius = diameter / 2; } public int getX() { return upperLeft.getX(); } public int getY() { return upperLeft.getY(); } public double getArea() { return Math.PI*Math.pow(radius, 2); } }

Completely implement the hierarchy here: Shape, Circle, and Rectangle

C Sc 335 Meaningless Foo Inheritance Hierarchy used in questions 2 and 3

class Foo {

public void one() {

System.out.println("Foo1");

public void three() {

System.out.println("Foo3");

class Bar extends Foo {

public void one() {

System.out.println("Bar1");

public void two() {

System.out.println("Bar2");

one();

class Baz extends Foo {

public void one() {

System.out.println("Baz1");

super.three();

public void two() {

System.out.println("Baz2");

class Goo extends Bar {

public void one() {

super.one();

System.out.println("Goo1");

public void two() {

System.out.println("Goo2");

public void three() {

super.two();

System.out.println("Goo3");

Answers

1. Refactor into an inheritance hierarchy

public abstract class Shape { private Point p; Shape(int x, int y) { p = new Point(x, y); } public int getX() { return p.getX(); } public int getY() { return p.getY(); } abstract double getArea(); } public class Rectangle extends Shape { private double my_width; private double my_height; public Rectangle(int x, int y, double height, double width) { super(x, y); my_width = width; my_height = height; } public double getArea() { return my_width * my_height; } } public class Circle extends Shape { private double my_radius; public Circle(int x, int y, double diameter) { super(x, y); my_radius = diameter / 2; } public double getArea() { return Math.PI * Math.pow(my_radius, 2); } }

2. Draw a UML class diagram

  1. RE, CE, or ouput

b.one();

Baz

Foo

c.one();

Bar

c.two();

CE

e.one();

CE

((Baz)c).two();

RE (ClassCastException)

((Goo)d).three();

Bar

Bar

Goo

Goo

((Foo)e).one();

Baz 1

Foo

((Goo)((Foo)e)).one(); RE (ClassCastException)