I completed the first program but I am not completely sure what the follow up qu
ID: 3792619 • Letter: I
Question
I completed the first program but I am not completely sure what the follow up question is telling me to modify, I have included the first problem and my working code for reference, please modify my code for the second problem.
• e9.10 The java.awt.Rectangle class of the standard Java library does not supply a method to compute the area or perimeter of a rectangle. Provide a subclass BetterRectangle of the Rectangle class that has getPerimeter and getArea methods. Do not add any instance variables. In the constructor, call the setLocation and setSize methods of the Rectangle class. Provide a program that tests the methods that you supplied.
import java.awt.Rectangle;
class BetterRectangle extends Rectangle
{
BetterRectangle ()
{
setLocation(5,5);
setSize(3,5);
}
double getPerimeter()
{
return (2*(width+height));
}
double getArea()
{
return (height*width);
}
public static void main(String[]args)
{
BetterRectangle rectangle=new BetterRectangle ();
System.out.println("The Perimeter of Rectangle is "+rectangle.getPerimeter());
System.out.println("The Area of Rectangle is "+rectangle.getArea());
}
}
Output:
The Perimeter of Rectangle is 16.0
The Area of Rectangle is 15.0
• e9.11 Repeat Exercise E9.10, but in the BetterRectangle constructor, invoke the superclass constructor i.e. super.Rectangle. Provide a program that tests the modification.
Explanation / Answer
Hi, Please find my implementation.
import java.awt.Rectangle;
public class BetterRectangle extends Rectangle
{
BetterRectangle ()
{
super(5,5,3,5); // first two parameters are point(x, y) and then widht and height
//setLocation(5,5);
//setSize(3,5);
}
double getPerimeter()
{
return (2*(width+height));
}
double getArea()
{
return (height*width);
}
public static void main(String[]args)
{
BetterRectangle rectangle=new BetterRectangle ();
System.out.println("The Perimeter of Rectangle is "+rectangle.getPerimeter());
System.out.println("The Area of Rectangle is "+rectangle.getArea());
}
}
/*
Sample run:
The Perimeter of Rectangle is 16.0
The Area of Rectangle is 15.0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.