Create two classes The first class name is Rectangle o It ha

Create two classes:

The first class name is Rectangle.

o      It has two private instance variables, both doubles, named length and width

o      The class has a private helper method named orderLengthAndWidth.   It ensures that the value of the length variable is always greater than or equal to the width. Any time a calling program changes the length or width of this Rectangle object, this method should be invoked to ensure that the length is always the greater of the length and width of the Rectangle object.

o      The class has one constructor with the signature:

Rectangle(double length, double width)

The constructor will ensure that the number stored as the length of the Rectangle is greater than the width by invoking the orderLengthAndWidth method.

o      The class has accessors (getters) and mutators (setters) for the length and width. The mutators also will ensure that the length is greater than the width by invoking the orderLengthandWidth helper method.

o      The class has a private method named computeArea that calculates the area of the rectangle (length x width) and returns it in a double

o      The class has a public method named isSquare that returns true if this Rectangle object is a square (length = width) or false otherwise

o      The toString method will return a String containing a description of the Rectangle that looks something like:

The rectangle’s length is (xx.xx), width is (yy.yy), and area is (zz.zz)

All numbers will be printed to two decimal places.

The next three methods (isLargerThan, isTheSameSize, and equals), are public and take another Rectangle object as a parameter. If the Rectangle passed as a parameter has not been initialized (i.e., is equal to null), then return false.

o      The method named isLargerThan compares the area of this Rectangle with the area of the other rectangle. If the area of this rectangle is larger, then the method returns a boolean value of true, otherwise it returns false

o      The method named isTheSameSize returns true if the area of this Rectangle is equal to the area of the other Rectangle object, otherwise it returns false.

o      The equals method returns a Boolean value true if the length and width of this Rectangle are equal to the length and width of the other Rectangle object.

The second class will be a “driver” program (one that contains main()) to exercise the Rectangle class described on the prior page.

This program will:

1.      Create a Rectangle object, passing an int of 40 for the length and a double of 35.5 as the width to the constructor. Print the details of the Rectangle using the toString() method

2.      Create a second Rectangle using an int of 37 as both the length and width for the constructor. Print the details of the second Rectangle using the toString() method

3.      Print whether the first and second Rectangles have the same area. If they are not the same size, then determine if the first Rectangle is larger or smaller than the second Rectangle.

4.      Print whether the first and second Rectangles are squares.

5.      Create a third Rectangle using doubles of 20.0 as the length and 71.0 as the width for the constructor. Print the details of the third Rectangle using toString()

6.      Print whether the first and third Rectangles have the same area

7.      Print whether the first and third Rectangles are equal

8.      Create a fourth Rectangle using 35.5 as the length and 40 for the width for the constructor. Print the details of the fourth Rectangle

9.      Print whether the third and fourth Rectangles have the same area.

10. Print whether the third and fourth Rectangles are equal.

11. Set the variable for the fourth Rectangle to null.

12. Print whether the third and fourth Rectangles have the same area.

13. Print whether the third and fourth Rectangles are equal.

Solution

import java.util.*;
import java.lang.*;
import java.io.*;

class Rectangle
{
private double length;
private double width;
private void orderLengthAndWidth()                  //check for lrngth >=width
{
  if(length >= width)
   System.out.println(\"valid length and width\");
  else
  System.out.println(\"Invalid length and width\");
  
}
private double ComputeArea()                //compute area
{
  return length*width;
}
public Rectangle(double length, double width)   //constructor
{
  orderLengthAndWidth();
  this.length = length;
  this .width = width;
}
public void setLength(double l)             //set and get functions
{
  orderLengthAndWidth();
  length = l;
}
public double getlength()
{
  return length;
}
public void setWidth(double w)
{
  orderLengthAndWidth();
  width =w;
}
public double getWidth()
{
  return width;
}
public boolean isSquare()          //check if rectangle is square
{
  if(length == width)
  return true;
  else
  return false;
}
public String toString()                    //toString() method
{
  length = Math.round(length * 100.0) / 100.0;
  width = Math.round(width * 100.0) / 100.0;
  double area = Math.round(ComputeArea() * 100.0) / 100.0;
  return \"The rectangle’s length is \"+ length+ \", width is \"+width+\", and area is \"+ area;
}
public boolean isLargerThan(Rectangle r)    //compare rectangles
{
  if(ComputeArea() > r.ComputeArea())
  return true;
  else
  return false;
}
public boolean isTheSameSize(Rectangle r)
{
  if(ComputeArea() == r.ComputeArea())
  return true;
  else
  return false;
}
  
public boolean equals(Rectangle r)
{
  if(length == r.length && width == r.width)
  return true;
  else
  return false;
}
}

class Driver
{
public static void main (String[] args)
{
  Rectangle r1 = new Rectangle(40,35.5);
  System.out.println(r1.toString());
  
  Rectangle r2 = new Rectangle(37,37);
  System.out.println(r2.toString());
  
  if(r1.isTheSameSize(r2))
  System.out.println(\"Rectangle r1 is of same size as Rectangle r2\");
  else if(r1.isLargerThan(r2))
  System.out.println(\"Rectangle r1 is Larger than Rectangle r2\");
  else if(r2.isLargerThan(r1))
  System.out.println(\"Rectangle r2 is Larger than Rectangle r2\");
  
  System.out.println(\"The rectangle r1 is Square: \"+r1.isSquare());
  System.out.println(\"The rectangle r2 is Square: \"+r2.isSquare());
  
  Rectangle r3 = new Rectangle(20.0,71.0);
  System.out.println(r3.toString());
  
  if(r1.isTheSameSize(r3))
  System.out.println(\"Rectangle r1 is of same size as Rectangle r3\");
  else
  System.out.println(\"Rectangle r1 is not of same size as Rectangle r3\");
  
  if(r1.equals(r3))
  System.out.println(\"Rectangle r1 is equal to Rectangle r3\");
  else
  System.out.println(\"Rectangle r1 is equal to Rectangle r3\");
  
  Rectangle r4 = new Rectangle(35.5,40.0);
  System.out.println(r4.toString());
  
  if(r3.isTheSameSize(r4))
  System.out.println(\"Rectangle r3 is of same size as Rectangle r4\");
  else
  System.out.println(\"Rectangle r3 is not of same size as Rectangle r4\");
  
   if(r3.equals(r4))
  System.out.println(\"Rectangle r3 is equal to Rectangle r4\");
  else
  System.out.println(\"Rectangle r3 is not equal to Rectangle r4\");
  
  r4.setWidth(0);
  r4.setLength(0);
  
  if(r3.isTheSameSize(r4))
  System.out.println(\"Rectangle r3 is of same size as Rectangle r4\");
  else
  System.out.println(\"Rectangle r3 is not of same size as Rectangle r4\");
  
   if(r3.equals(r4))
  System.out.println(\"Rectangle r3 is equal to Rectangle r4\");
  else
  System.out.println(\"Rectangle r3 is not equal to Rectangle r4\");
}
}

output:

Success time: 0.05 memory: 711168 signal:0

Create two classes: The first class name is Rectangle. o It has two private instance variables, both doubles, named length and width o The class has a private h
Create two classes: The first class name is Rectangle. o It has two private instance variables, both doubles, named length and width o The class has a private h
Create two classes: The first class name is Rectangle. o It has two private instance variables, both doubles, named length and width o The class has a private h
Create two classes: The first class name is Rectangle. o It has two private instance variables, both doubles, named length and width o The class has a private h

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site