Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.awt.Rectangle; import java.awt.Point; import java.util.*; import jav

ID: 3811707 • Letter: I

Question

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

// These rectangles must be created, in this order, for your final version

// Be sure to display the toString() for each after construction

//    These rectangles will test the four constructors

//    bRectB and bRectC are for the utility tests - five tests

//    bRectA is for the accessor tests - four tests

//    bRectD is for the mutator tests - two tests: both valid and invalid scale (use +/- 3)

BetterRectangle bRectA = new BetterRectangle(4,3);

BetterRectangle bRectB = new BetterRectangle(bRectA);

BetterRectangle bRectC = new BetterRectangle(1,1,3,4);

BetterRectangle bRectD = new BetterRectangle();

GIVEN THE FOLLOWING CODE, MODIFY IT TO SUIT THESE NEEDS:

// Five utility tests could look like:

//    boolean result = bRectB.equals(bRectC);

//    boolean result = bRectB.congruent(bRectC);

//    boolean result = bRectB.equivalent(bRectC);

//    boolean result = bRectB.similar(bRectC);

//    boolean result = bRectB.concentric(bRectC);

// Be sure to display the results, display the toString() for each, and include your expected values…

// Four accessor tests could look like:

//    int area = bRectA.getArea();

//    int perimeter = bRectA.getPerimeter();

//    float slope = bRectA.getSlope();

//    Point midPoint = bRectA.getMidPoint();

// Be sure to display the results, display the toString(), and include your expected values…

// Two mutator tests could look like:

//    boolean result = bRectD.scaleBy(3);

//    boolean result = bRectD.scaleBy(-3);

// Be sure to display the results, display the toString(): before and after, and include your expected values…

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)
{
       //Changing the equals method to use super instead of this. Otherwise, it would give
       // Stackoverflow error
if(super.equals(obj)) 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());
     
   //Adding tests asked in question below this line
   //Rectangle creation and toString() methods
     
   BetterRectangle bRectA = new BetterRectangle(4,3);
   System.out.println(bRectA.toString());
   BetterRectangle bRectB = new BetterRectangle(bRectA);
   System.out.println(bRectB.toString());
   BetterRectangle bRectC = new BetterRectangle(1,1,3,4);
   System.out.println(bRectC.toString());
   BetterRectangle bRectD = new BetterRectangle();
   System.out.println(bRectD.toString());
     
   //Five utility tests
     
   boolean result1 = bRectB.equals(bRectC);
   //Since the equals method compares two objects and the location properties of both rectangles
   //are different, so expected answer should be false.
   System.out.println(result1);
     
boolean result2 = bRectB.congruent(bRectC);
   //Since congruent method compares the sum of width and height of two rectangles, which is 7
   //in both cases. So expected answer should be true.
   System.out.println(result2);
     
boolean result3 = bRectB.equivalent(bRectC);
   //The equivalent method compares the perimeter of both rectangles, which is same. So expected
   // answer should be true.
   System.out.println(result3);
     
boolean result4 = bRectB.similar(bRectC);
   //Since similar method compares the area of both the rectangles, which is 12 in both cases.
   // So expected answer should be true.
   System.out.println(result4);
     
   boolean result5 = bRectB.concentric(bRectC);
   //Since concentric method compares the midPoint of both rectangles, which is different, so
   // the expected answer should be false.
   System.out.println(result5);
     
   // Four accessor tests
int area = bRectA.getArea();
   //The expected answer should be 4*3 = 12
   System.out.println("Area = " + area);
     
   int perimeter = bRectA.getPerimeter();
   // The expected answer should be 2*(4 + 3) = 14
   System.out.println("Perimeter = " + perimeter);
     
float slope = bRectA.getSlope();
   // The expected answer should be 3/4 ~ 0.75
   System.out.println("Slope = " + slope);
     
Point midPoint = bRectA.getMidPoint();
   // Since there is an integer casting, So the expected answer should be (2, 1)
   System.out.println("MidPoint = " + midPoint);
     
   // Two mutator tests
   // Before making any changes stats for bRectD are, x=0,y=0,width=1,height=1,
   // Area = 1*1 = 1, Perimeter = 2*(1+1) = 4, Slope = 1/1 = 1, MidPoint = (0,0)
   System.out.println(bRectD.toString());
     
boolean result6 = bRectD.scaleBy(3);
   //After scaling, the height and width are both 2. The stats will be
   // x=0,y=0,width=2,height=2, Area = 2*2 = 4, Perimeter = 2*(2+2) = 8,
   //Slope = 2/2 = 1, MidPoint = (1, 1)
   System.out.println(bRectD.toString());
     
boolean result7 = bRectD.scaleBy(-3);
   //There will be no change here as scaleBy method will just return false for
   // negative inputs. So expected stats should be same as before, i.e, x=0,y=0,
   // width=2,height=2, Area = 2*2 = 4, Perimeter = 2*(2+2) = 8, Slope = 2/2 = 1,
   // MidPoint = (1, 1)
   System.out.println(bRectD.toString());
     
}
}


// These rectangles must be created, in this order, for your final version
// Be sure to display the toString() for each after construction
// These rectangles will test the four constructors
// bRectB and bRectC are for the utility tests - five tests
// bRectA is for the accessor tests - four tests
// bRectD is for the mutator tests - two tests: both valid and invalid scale (use +/- 3)

BetterRectangle bRectA = new BetterRectangle(4,3);
BetterRectangle bRectB = new BetterRectangle(bRectA);
BetterRectangle bRectC = new BetterRectangle(1,1,3,4);
BetterRectangle bRectD = new BetterRectangle();

GIVEN THE FOLLOWING CODE, MODIFY IT TO SUIT THESE NEEDS:
// Five utility tests could look like:
// boolean result = bRectB.equals(bRectC);
// boolean result = bRectB.congruent(bRectC);
// boolean result = bRectB.equivalent(bRectC);
// boolean result = bRectB.similar(bRectC);
// boolean result = bRectB.concentric(bRectC);
// Be sure to display the results, display the toString() for each, and include your expected values…

// Four accessor tests could look like:
// int area = bRectA.getArea();
// int perimeter = bRectA.getPerimeter();
// float slope = bRectA.getSlope();
// Point midPoint = bRectA.getMidPoint();
// Be sure to display the results, display the toString(), and include your expected values…

// Two mutator tests could look like:
// boolean result = bRectD.scaleBy(3);
// boolean result = bRectD.scaleBy(-3);
// Be sure to display the results, display the toString(): before and after, and include your expected values…