This quiz uses the three classes below Watch out for bugs pu
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(); }
}
QUESTION 1
What is output by the following code?
Trapezoid t = new Trapezoid(2, 4, 6);
double a = t.area();
double p = t.perimeter();
System.out.print(a + \" \" + p);
5 points
QUESTION 2
What is output by the following code?
Rectangle r = new Rectangle(4, 6);
double a = r.area();
double p = r.perimeter();
System.out.print(a + \" \" + p);
5 points
QUESTION 3
What is output by the following code?
Square s = new Square(6);
double a = s.area();
double p = s.perimeter();
System.out.print(a + \" \" + p);
5 points
QUESTION 4
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);
5 points
QUESTION 5
What is output by the following code?
Rectangle r = new Rectangle(4, 6);
r.setWidth(8);
double a = r.area();
double p = r.perimeter();
System.out.print(a + \" \" + p);
5 points
QUESTION 6
What is output by the following code?
Square s = new Square(6);
s.setWidth(8);
double a = s.area();
double p = s.perimeter();
System.out.print(a + \" \" + p);
5 points
QUESTION 7
What is output by the following code?
Trapezoid t = new Trapezoid(2, 4, 6);
t.setHeight(8);
double a = t.area();
double p = t.perimeter();
System.out.print(a + \" \" + p);
5 points
QUESTION 8
What is output by the following code?
Rectangle r = new Rectangle(4, 6);
r.setHeight(8);
double a = r.area();
double p = r.perimeter();
System.out.print(a + \" \" + p);
5 points
QUESTION 9
What is output by the following code?
Square s = new Square(6);
s.setHeight(8);
double a = s.area();
double p = s.perimeter();
System.out.print(a + \" \" + p);
5 points
QUESTION 10
What is output by the following code?
Trapezoid t = new Trapezoid(2, 4, 6);
t.setBase(8);
double a = t.area();
double p = t.perimeter();
System.out.print(a + \" \" + p);
| This quiz uses the three classes below. Watch out for bugs! public class Trapezoid { |
Solution
1)
Trapezoid t = new Trapezoid(2, 4, 6); //the obect for Trapezoid class is created and constuctor is invoked
double a = t.area();//return value of area method of Trapezoid class is stored in a
double p = t.perimeter();//return value of perimeter method of Trapezoid class is stored in p
System.out.print(a + \" \" + p); //a and p are printed
output:10.00 14.00
2)
Rectangle r = new Rectangle(4, 6);
double a = r.area();
double p = r.perimeter();
System.out.print(a + \" \" + p);
output: 24.00 20.00
3)
Square s = new Square(6);
double a = s.area();
double p = s.perimeter();
System.out.print(a + \" \" + p);
output: 36.0 24.0
4)Trapezoid t = new Trapezoid(2, 4, 6);
t.setWidth(8);
double a = t.area();
double p = t.perimeter();
System.out.print(a + \" \" + p);
output:- 14.0 18.0
5)
Rectangle r = new Rectangle(4, 6); //here the height will be set to 4 and both width and base will be set to 6
r.setWidth(8);//here only width willbe set to 8, leaving base as 6. so the result will not be as expected
double a = r.area();
double p = r.perimeter();
System.out.print(a + \" \" + p);
output: 32.0 22.0
6)Square s = new Square(6);//here height, width and base are all set to 6
s.setWidth(8); //only width changed to 8
double a = s.area(); //this uses only width so result is as expected.
double p = s.perimeter();// this uses all the three dimensions so result will not be as expected
System.out.print(a + \" \" + p);
output: 64.0 26.0
7)Trapezoid t = new Trapezoid(2, 4, 6);
t.setHeight(8);
double a = t.area();
double p = t.perimeter();
System.out.print(a + \" \" + p);
output: 40.0 26.0
8)Rectangle r = new Rectangle(4, 6);
r.setHeight(8);
double a = r.area();
double p = r.perimeter();
System.out.print(a + \" \" + p);
output: 48.0 28.0
9)Square s = new Square(6);
s.setHeight(8);
double a = s.area();
double p = s.perimeter();
System.out.print(a + \" \" + p);
output:36.0 28.0
10)Trapezoid t = new Trapezoid(2, 4, 6);
t.setBase(8);
double a = t.area();
double p = t.perimeter();
System.out.print(a + \" \" + p);
output:12.0 16.0




