Provide a BetterRectangle sub-class that extends the Rectangle class of the stan
ID: 3863599 • Letter: P
Question
Provide a BetterRectangle sub-class that extends the Rectangle class of the standard Java library by adding methods to compute the area, perimeter, slope and mid-point of the rectangle, as well as valid constructors for the new sub-class. Provide a Tester program that will execute and validate the BetterRectangle sub-class
Output: Output will provide validation that all new features in the sub-class are functioning properly - presented in a clear, readable and attractive manner. All output should be handled by the Tester class, not the super or sub-class. Use the toString() method to display the characteristics of each rectangle.
Input: No user input required.
No switch or breaks statements allowed. No Magic numbers!
The Tester class will create four (4) “better” rectangles, using each of the required constructors. Next, selecting one of the rectangles, the Tester will execute all of the added accessor methods. Then, using two of the remaining rectangles, the Tester will execute the “utility” methods (and the equals() method) listed below, comparing the two rectangles. Finally, using the remaining rectangle, the Tester will execute the mutator method. Expected values should be displayed for each result to confirm correct results.
The BetterRectangle sub-class may NOT add any new instance variables; however, it can provide new constants. In the sub-class constructors, use the setLocation() and setSize() methods of the Rectangle class. Include an override for the toString() and equals() methods, as appropriate. The sub-class must implement all of the below listed methods/constructors, overriding as appropriate.
Constructors - must use setLocation() and setSize() methods
BetterRectangle(): creates a unit rectangle at Origin
BetterRectangle(int w, int h): creates rectangle at Origin
BetterRectangle(int x, int y, int w, int h): create rectangle
BetterRectangle(BetterRectangle r): creates a copy if rectangle r
Override - as appropriate
toString() to also include area, perimeter, slope and mid-point; in
addition to the lower left coordinates, width and height.
equals(r)
Added Accessor - no new instance variables
getArea() return (int) - width * height
getPerimeter() return (int) - (width + height) * 2
getSlope() return (float) - height / width (float)
getMidPoint() return (Point) - center of rectangle + lower left corner
(rounded off to int)
Added Utilities - (r) is another rectangle
congruent(r) return true if (width + height) is same for both
equivalent(r) return true if perimeter is same for both
similar(r) return true if area is same for both concentric(r) return true if midpoint is same for both
Added Mutator
boolean ScaleBy(scale) multiply height, width by fixed + int scale,
returns true upon success, returns false upon failure.
Use of the “Point” class is required
JAVA
Explanation / Answer
import java.awt.Rectangle;
import java.awt.Point;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.Random;
public class BetterRectangle extends Rectangle{
//constructors of 4 types
BetterRectangle()
{
Point p = new Point();
super.setLocation(p);
super.setSize(1, 1);
}
BetterRectangle(int w,int h)
{
Point p = new Point();
super.setLocation(p);
super.setSize(w,h);
}
BetterRectangle(int x,int y,int w,int h)
{
Point p = new Point(x,y);
super.setLocation(p);
super.setSize(w,h);
}
BetterRectangle(BetterRectangle r)
{
super.setLocation(r.getLocation());
super.setSize(r.getSize());
}
//overridden method toString and equals
public String toString()
{
return String.format(super.toString()+ "Area : "+this.getArea()+ " Perimeter : "+this.getPerimeter()+ " Slope : "+this.getSlope()+" MidPoint : "+this.getMidPoint());
}
public boolean equals(Object obj)
{
if(obj!=null)
{
if(obj.equals(this)) return true;
return false;
}
return false;
}
// Added accessor methods
public int getArea()
{
return this.width*this.height;
}
public int getPerimeter()
{
return 2*(this.width+this.height);
}
public float getSlope()
{
return height/(float)width;
}
public Point getMidPoint()
{
Point p = new Point((int)(this.x+this.width/2),(int)(this.y+this.height/2));
return p;
}
// Added utilities
public boolean congruent(BetterRectangle r)
{
if(r!=null && (this.width+this.height)==(r.width+r.height)) return true;
return false;
}
public boolean equivalent(BetterRectangle r)
{
if(r!=null && this.getPerimeter()==r.getPerimeter()) return true;
return false;
}
public boolean similar(BetterRectangle r)
{
if(r!=null && this.getArea()==r.getArea()) return true;
return false;
}
public boolean concentric(BetterRectangle r)
{
if(r!=null && this.getMidPoint()==r.getMidPoint()) return true;
return false;
}
// Added mutator
public boolean scaleBy(int x)
{
if(x>0)
{
this.height*=2;
this.width*=2;
return true;
}
return false;
}
public static void main (String[] args)
{
// tester code highlighting usage of all methods as per question
BetterRectangle b1 = new BetterRectangle();
BetterRectangle b2 = new BetterRectangle(2,3);
BetterRectangle b3 = new BetterRectangle(1,1,2,3);
BetterRectangle b4 = new BetterRectangle(b2);
System.out.println(b1.toString()); //toString uses all the accessor methods
if(b2.congruent(b3))
{
System.out.println("Congruent");
}
else System.out.println("Not congruent");
if(b2.equivalent(b3))
{
System.out.println("Equivalent");
}
else System.out.println("Not equivalent");
if(b2.similar(b3))
{
System.out.println("Similar");
}
else System.out.println("Not similar");
if(b2.concentric(b3))
{
System.out.println("Concentric");
}
else System.out.println("Not concentric");
b4.scaleBy(2);
System.out.println(b4.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.