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

I am wanting to test my class \"Rectangle\" in my teachers tester, but I cant se

ID: 665879 • Letter: I

Question

I am wanting to test my class "Rectangle" in my teachers tester, but I cant seem to get my code right thus far.

Here is his tester program:

public class DHSRectangleTester {
   static int fails = 0;

   public static void main(String[] args) {
       Rectangle t,q;
       double w;

       t = new Rectangle(8.5);
       String s = "Square Test ";
       c(s+ "height", t.getHeight(),8.5);
       c(s+ "width", t.getWidth(),8.5);
       c(s+ "area", t.getArea(),72.25);
       c(s+ "diagonal", t.getDiagonal(),12.020815280171307);

       t = new Rectangle();
       s = "no-arg test ";
       c(s+ "height", t.getHeight(),1);
       c(s+ "width", t.getWidth(),1);
       c(s+ "area", t.getArea(),1);
       double m = Math.sqrt(2.0);
       c(s+ "diagonal", t.getDiagonal(),m);

       t = new Rectangle(3,4);
       s = "basic 3X4 Test ";
       c(s+ "height", t.getHeight(),3);
       c(s+ "width", t.getWidth(),4);
       c(s+ "area", t.getArea(),12);
       c(s+ "diagonal", t.getDiagonal(),5);

       t = new Rectangle(3,4);
       s = "setHeight Test ";
       w = 17.5;
       t.setHeight(w);
       pp("After setting height to 17.5" ,t);
       c(s+ "height", t.getHeight(),w);
       c(s+ "width", t.getWidth(),4);
       c(s+ "area", t.getArea(),70);
       c(s+ "diagonal", t.getDiagonal(),17.95132307101624);

       s = "setWidth Test ";
       w = 22.5;
       t.setWidth(w);
       pp("After setting height to 22.5" ,t);

       c(s+ "height", t.getHeight(),17.5);
       c(s+ "width", t.getWidth(),22.5);
       c(s+ "area", t.getArea(),393.75);
       c(s+ "diagonal", t.getDiagonal(),28.50438562747845);
          
       q = new Rectangle(3,4);
       t = new Rectangle(q);
       s = "Rectangle constructor Test ";
       c(s+ "height", t.getHeight(),3);
       c(s+ "width", t.getWidth(),4);
       c(s+ "area", t.getArea(),12);
       c(s+ "diagonal", t.getDiagonal(),5);
      
       q = new Rectangle(3,4);
       t = q.copy();
       s = "Rectangle .copy Test ";
       c(s+ "height", t.getHeight(),3);
       c(s+ "width", t.getWidth(),4);
       c(s+ "area", t.getArea(),12);
       c(s+ "diagonal", t.getDiagonal(),5);
  
       t = new Rectangle(3,4);
       pp("b4 rotate",t);
       t.rotate();
       pp("after rotate",t);
       s = "Rotate Test ";
       c(s+ "height", t.getHeight(),4);
       c(s+ "width", t.getWidth(),3);
       c(s+ "area", t.getArea(),12);
       c(s+ "diagonal", t.getDiagonal(),5);
      
       s = "doubleIt Test ";
       t.doubleIt();
       m = Math.sqrt(2.0);
       c(s+ "height", t.getHeight(),4*m);
       c(s+ "width", t.getWidth(),3*m);
       c(s+ "area", t.getArea(),12*2);
       c(s+ "diagonal", t.getDiagonal(),5*m);

       t = new Rectangle(3,4);
       m = 7.0;
       t.scale(m);
       s = "scale Test(7) ";
       c(s+ "height", t.getHeight(),3*m);
       c(s+ "width", t.getWidth(),4*m);
       c(s+ "area", t.getArea(),12*m*m);
       c(s+ "diagonal", t.getDiagonal(),5*m);

       t = new Rectangle(3,4);
       q = new Rectangle(4.01,3.01);
       s = "Equals test, nudged";
       eqt(s,t,q, true);

       q = new Rectangle(3.01,3.99);
       s = "Equals test, rotated and nudged";

       q = new Rectangle(3.01,3.99);
       eqt(s,t,q,true);

       s = "Equals test, should fail";
       q = new Rectangle(3.51,3.99);
       eqt(s,t,q,false);

       p("All done, " + fails + " failures.");

   }
   public static void eqt(String s, Rectangle a, Rectangle b, boolean expected) {
       if (a.equals(b) == expected) {
           p("OK: " + s);
       } else {
           f("Fail on: " + s);
       }
   }
   public static void c(String s, double x, double y) {
       if (almostEquals(x,y)) {
           p("OK: " + s);
       } else {
           f("Fail on: " + s);
       }
   }
   public static boolean almostEquals(double x, double y) {
       return Math.abs(x-y) < 0.000001*Math.abs(x);
   }
   public static void p(String g) {
       System.out.println(g);
   }
   public static void pp(String s, Rectangle r) {
       String a = s;
       a += " height: " + r.getHeight();
       a += " Width: " + r.getWidth();
       a += " area: " + r.getArea();
       a += " diagonal: " + r.getDiagonal();
       p(a);

   }
   public static void f(String g){
       fails++;
       p(g);

   }
}


Okay, here is my code, I am up for any advice on fixing it. Right now it doesn't work. Kinda confused in general


public class Rectangle {

   //private variables
   private double width;
   private double height;
   private double x=0;
   private double y=0;


   public Rectangle(){
       height=1;
       width=1;
      
   }
   public Rectangle(int n){
       width=n;
       height=n;
   }
   public Rectangle(double w,double h){
       width=w;
       height=h;
   }
   public double getHeight(){
       return height;
   }
   public double getWidth(){
       return width;
   }
   public double getArea(){
       return width*height;
   }
   public double getDiagonal(){
       return Math.sqrt(width*width+height*height);
   }
  
   public void rotate(){
       double var =width;
       width=height;
       height=var;
   }
   public void doubleIt(){
       //not sure how to doubleIt. Tried below, but i know its wrong
       //getArea()=2*getArea();
       //I want to double the area and
      
      
   }
   public boolean equals(Rectangle r){
       if (r.getHeight()==height && r.getWidth()==width) return true;
       return false;
   }
       public Rectangle copy(){
           Rectangle r= new Rectangle(width, height);
           return r;
          
          
       }
   public void scale(double theScale){
       width=width*theScale;
       height=height*theScale;
  
          
          
       }
   }
  

Explanation / Answer

public class Rectangle {

    //private variables
    private double width;
    private double height;
    private double x=0;
    private double y=0;


    public Rectangle(){
        height=1;
        width=1;
      
    }
    public Rectangle(double n){
        width=n;
        height=n;
    }
    public Rectangle(Rectangle q)
    {
        width=q.width;
        height=q.height;
    }
    public Rectangle(double h,double w){
        width=w;
        height=h;
    }
    public double getHeight(){
        return height;
    }
    public double getWidth(){
        return width;
    }
    public void setHeight(double x)
    {
        height=x;
    }
    public void setWidth(double x)
    {
        width=x;
    }
    public double getArea(){
        return width*height;
    }
    public double getDiagonal(){
        return Math.sqrt(width*width+height*height);
    }
  
    public void rotate(){
        double var =width;
        width=height;
        height=var;
    }
    public void doubleIt(){
        double m=Math.sqrt(2.0);
        width=width*m;
        height=height*m;
    }

    public boolean equals(Rectangle r){
        if (r.getHeight()==height && r.getWidth()==width) return true;
        return false;
    }
        public Rectangle copy(){
            Rectangle r= new Rectangle(height, width);
            return r;
        }
    public void scale(double theScale){
        width=width*theScale;
        height=height*theScale;
  
        }
    }

// I fixed up everything except the equals method. So you get only two errors now.

// I can't figure out when you actually consider two rectangles to be equal. I can help you if you can

// specify the exact criteria on the basis of which we decide whether two rectangles are equal.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote