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

I am wanting to write a java class that can be tested in my teachers program tha

ID: 665865 • Letter: I

Question

I am wanting to write a java class that can be tested in my teachers program that he has already created. It must fit his requirements with methods and constructors. I have been playing around with writing code to fit his tester in my class "Rectangle", but I am confused and kinda lost in general about what he wants. Can anyone give me some direction. Here is his specifications for my class:

This will contain these methods:

Data:

double height, width

Constructors:

One no-arg constructor that constructs a rectangle 1X1

One one-arg constructor that takes one double argument, and constructs a square that size

One two-arg constructor that takes two double arguments (height and width, in that order) , and constructs a rectangle that size OK: I forgot to specify that order, so assignments turned in before 2/21, I'll accept two-arg constructors that take them in either order.

One one-arg constructor that takes a Rectangle as an argument, and constructs as new Rectangle of identical size.

Other Methods:

getters and setters for width and height using standard conventions.

a ".equals()" method that accepts another Rectangle as argument, and returns TRUE if the other rectangle has an area within 1% of the instance rectangle.

A ".rotate()" method that reverses height and width of the instance rectangle. (void)

A .getArea()" method that returns the area of the rectangle as a double.

A ".getDiagonal()" method that returns the diagonal of the rectangleas a double.

A ".copy()" method that returns a Rectangle of identical size.

A ".scale(double theScale)" method that accepts a single double argument, and scales the instance rectangle by that amount. For example, a 4X3 rectangle with .scale(2.0) applied to it should become 8X6 in size. Should be void.

A ".doubleIt()" method which should double the area of the rectangle, without changing its aspect ratio (the ratio between the height and width). Should be void.

Here is what I have, but not sure what I should be doing.....

public class Rectangle {

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


   public Rectangle(double h, double w){
       height=h;
       width=w;
      
   }
   public Rectangle(){
   }
   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);
   }
   //not sure how to rotate it yet
   //public double rotate(){
   //   return
   //}
   public double doubleIt= 2*getArea();
  
  
   }

Explanation / Answer

public class Rectangle {
   private double width;
   private double height;
   private double x = 0;
   private double y = 0;
   public Rectangle(){
       width = 1;
       height = 1;
   }
   public Rectangle(int n){
       width = n;
       height = n;
   }
   public Rectangle(int w,int 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(){
       int temp = width;
       width = height;
       height = temp;
   }
   public void 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 val){
       width = width*val;
       height = height*val;
   }
}

class main{
   public static void main(String[] args){
       Rectangle r_0 = new Rectangle();
       Rectangle r_1 = new Rectangle(5);
       Rectangle r_2 = new Rectangle(3,4);
   }
}

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