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

Define the class Rectangle that contains double instance variables named width a

ID: 3852146 • Letter: D

Question

Define the class Rectangle that contains double instance variables named width and height, and a static variable named printChar. Include the following: A no-arg constructor that creates a default rectangle with 1.0 for both width and height. A constructor that creates a rectangle with the specified width, and height. Get and set methods for all the instance variables. A method getArea() that returns the area of the rectangle. A static method setPrintChar() that modifies the static variable A toString() method that returns a String that represents an outline drawing of the rectangle using the character given by printChar, For example, if printChar were 'c' and one Rectangle had a width of 5 and a height of 4, then printing the toString() method to output would give ccccc c c ccccc Write a test program that first sets the printChar for the Rectangle class as '*', then creates two Rectangle Objects of reasonable sizes. Print out each rectangle separately to output using the toString() method, followed by the value of that rectangle's area. Change the printChar to a reasonable (non-white-space) character of your choosing, modify both instance variables for both Rectangle Objects, and then repeat the output process: print out each rectangle separately to output followed by the value of that rectangle's area.

Explanation / Answer

public class Rectangle {
  
   private double x,y; // Center of the rectangle
   private double height, width;
  
   public Rectangle(){
       x=y=0;
       width = height =1;
   }
  
   public Rectangle(double x, double y, double width, double height){
       this.x=x;
       this.y=y;
       this.width=width;
       this.height=height;
   }

   public double getX() {
       return x;
   }

   public void setX(double x) {
       this.x = x;
   }

   public double getY() {
       return y;
   }

   public void setY(double y) {
       this.y = y;
   }

   public double getHeight() {
       return height;
   }

   public void setHeight(double height) {
       this.height = height;
   }

   public double getWidth() {
       return width;
   }

   public void setWidth(double width) {
       this.width = width;
   }
  
   public double getPerimeter(){
       return 2 * (width + height);
   }
  
   public double getArea(){
       return width*height;
   }
  
   public boolean contains(double x, double y){
       return Math.abs(x-this.x) <= width / 2 && Math.abs(y-this.y)<= height / 2;
   }
  
   // overload contains method
   public boolean contains(Rectangle r){
       return contains(r.x - r.width / 2, r.y + r.height / 2)&&
               contains(r.x - r.width / 2, r.y - r.height / 2) &&
               contains(r.x + r.width / 2, r.y + r.height / 2) &&
               contains(r.x + r.width / 2, r.y - r.height / 2);
   }
  
   // why is this here? it's never called.
   /*public boolean overlaps(Rectangle r){
       return Math.abs(this.x - r.x) <= (this.width + r.width) / 2 && Math.abs(this.y - r.y) <=
               (this.height + r.height) / 2;
   }*/

}

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