This quiz uses the three classes below Watch out for bugs Th

This quiz uses the three classes below. Watch out for bugs!

This quiz uses the three classes below. Watch out for bugs!

public class Trapezoid {
private double height, width, base;

public Trapezoid(double h, double w, double b) {
setHeight(h);
setWidth(w);
setBase(b);
}

public double getHeight() { return height; }
public double getWidth() { return width; }
public double getBase() { return base; }

public void setHeight(double h) { height = h; }
public void setWidth(double w) { width = w; }
public void setBase(double b) { base = b; }

public double area() {
return getHeight() * (getWidth() + getBase()) / 2;
}

public double perimeter() {
return 2 * getHeight() + getWidth() + getBase();
}
}


public class Rectangle extends Trapezoid {
public Rectangle(double h, double w) {
super(h, w, w);
}

public double area() { return getHeight() * getWidth(); }
}


public class Square extends Rectangle {
public Square(double w) {
super(w, w);
}

public double area() { return getWidth() * getWidth(); }
}
What is output by the following code?
Trapezoid t = new Trapezoid(2, 4, 6);
t.setWidth(8);
double a = t.area();
double p = t.perimeter();
System.out.print(a + \" \" + p)
Rectangle r = new Rectangle(4, 6);
r.setWidth(8);
double a = r.area();
double p = r.perimeter();
System.out.print(a + \" \" + p);
Trapezoid t = new Trapezoid(1, 1, 1);
Rectangle r = new Rectangle(2, 2);
Square s = new Square(3);
r = s;
double a1 = t.area();
double a2 = r.area();
double a3 = s.area();
System.out.print(a1 + \" \" + a2 + \" \" + a3);

Solution

Trapezoid.java

public class Trapezoid {
private double height, width, base;

public Trapezoid(double h, double w, double b) {
setHeight(h);
setWidth(w);
setBase(b);
}

public double getHeight() { return height; }
public double getWidth() { return width; }
public double getBase() { return base; }

public void setHeight(double h) { height = h; }
public void setWidth(double w) { width = w; }
public void setBase(double b) { base = b; }

public double area() {
return getHeight() * (getWidth() + getBase()) / 2;
}

public double perimeter() {
return 2 * getHeight() + getWidth() + getBase();
}
}

____________________

Rectangle.java

public class Rectangle extends Trapezoid {
public Rectangle(double h, double w) {
super(h, w, w);
}

public double area() { return getHeight() * getWidth(); }
}

__________________

Square.java

public class Square extends Rectangle {
public Square(double w) {
super(w, w);
}

public double area() { return getWidth() * getWidth(); }
}

_____________________

Test.java

public class Test {

   public static void main(String[] args) {
       Trapezoid t = new Trapezoid(2, 4, 6);
       t.setWidth(8);
       double a = t.area();
       double p = t.perimeter();
       System.out.println(a + \" \" + p);
       Rectangle r = new Rectangle(4, 6);
       r.setWidth(8);
       double rectarea = r.area();
       double rectperim = r.perimeter();
       System.out.println(rectarea + \" \" + rectperim);
       Trapezoid t1 = new Trapezoid(1, 1, 1);
       Rectangle r1 = new Rectangle(2, 2);
       Square s = new Square(3);
       r = s;
       double a1 = t1.area();
       double a2 = r1.area();
       double a3 = s.area();
       System.out.print(a1 + \" \" + a2 + \" \" + a3);

   }

}

_________________

Output:

14.0 18.0
32.0 22.0
1.0 4.0 9.0

_________Thank You

 This quiz uses the three classes below. Watch out for bugs! This quiz uses the three classes below. Watch out for bugs! public class Trapezoid { private double
 This quiz uses the three classes below. Watch out for bugs! This quiz uses the three classes below. Watch out for bugs! public class Trapezoid { private double
 This quiz uses the three classes below. Watch out for bugs! This quiz uses the three classes below. Watch out for bugs! public class Trapezoid { private double

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site