Write a superclass encapsulating a rectangle. A rectangle has two attributes rep
ID: 640650 • Letter: W
Question
Write a superclass encapsulating a rectangle. A rectangle has two attributes representing the width and the height of the rectangle. It has methods returning the perimeter and the area of the rectangle. This class has a subclass, encapsulating a parallelepiped,or box.A parallelepiped has a rectangle as its base, and another attribute, its length; it has two methods that calculate and return its area and vol- ume. You also need to include a JUnit to test these two classes. - Use Statement and Branch coverage techniques.
I have the code mostly done. I am getting an error or two. I am mostly confused with how to set up the J-Unit test stuff. If anyone could help it would be much appreciated!
Thanks
package rectangle;
public class Rectangle
{
protected double width;
protected double height;
public Rectangle(double width, double height){
this.width = width;
this.height = height;
}
public double getWidth(){
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight(){
return height;
}
public void setHeight(double height){
this.height = height;
}
public double getArea(){
return width * height;
}
public double getPerimeter(){
return 2 * (width + height);
}
}
public class Box extends Rectangle {
protected double length;
public Box(double length){
super(length, length);
}
public double getLength(){
return length;
}
public void setLength(double length){
this.length = length;
}
public double getVolume(){
return width * height * length;
}
}
public class TestRectangle {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(2,4);
Box box = new Box(5);
System.out.println(" A rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +rectangle.getPerimeter());
System.out.println("The volume is " + box.getVolume());
}
}
Explanation / Answer
public class Rectangle1
{
private double width;
private double height;
public Rectangle1(){
}
public Rectangle1(double width, double height){
this.width = width;
this.height = height;
}
public double getWidth(){
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight(){
return height;
}
public void setHeight(double height){
this.height = height;
}
public double getArea(){
return width * height;
}
public double getPerimeter(){
return 2 * (width + height);
}
}
public class Box extends Rectangle {
protected double length;
public Box(double length){
super(length, length);
}
public double getLength(){
return length;
}
public void setLength(double length){
this.length = length;
}
public double getVolume(){
return width * height * length;
}
}
public class TestRectangle {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(2,4);
Box box = new Box(5);
System.out.println(" A rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +rectangle.getPerimeter());
System.out.println("The volume is " + box.getVolume());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.