Question

In: Computer Science

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

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);

Solutions

Expert 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

  • Here to eliminate unexpected results it is better to write separate perimeter functions as well for each class

Related Solutions

public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
A incomplete definition of a class Temperature is given below: public class Temperature { private double...
A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3}; } [6] (i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
I have three classes: class VolleyPlayer, class VolleyTeam and class TestDriver 1.class: public class VolleyPlayer implements...
I have three classes: class VolleyPlayer, class VolleyTeam and class TestDriver 1.class: public class VolleyPlayer implements Comparable<VolleyPlayer> { // instance variables - private String name; private int height; private boolean female; public VolleyPlayer(String name, int height, boolean female) { this.name = name; this.height = height; this.female = female; } public String toString() {    return (female ?"Female":"Male")+": "+name+" ("+height+" cm)"; } public boolean isFemale() {    return female; } public boolean isMale() {    return !female; } public int getHeight()...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this uml as a reference.
C++ Classes & Objects Create a class named Student that has three private member: string firstName...
C++ Classes & Objects Create a class named Student that has three private member: string firstName string lastName int studentID Write the required mutator and accessor methods/functions (get/set methods) to display or modify the objects. In the 'main' function do the following (1) Create a student object "student1". (2) Use set methods to assign StudentID: 6337130 firstName: Sandy lastName: Santos (3) Display the students detail using get functions in standard output using cout: Sandy Santos 6337130
Is a college class at a public university a Private good, Public good, or Club good
Is a college class at a public university a Private good, Public good, or Club good
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT