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

I. Write a class definition for a Rectangle class that contains: · Two int field

ID: 3619698 • Letter: I

Question

I. Write a class definition for a Rectangle class that contains:

· Two int fields, length and width.

· Mutator and accessor methods for the length and width fields.

· A Boolean method named isSquare that returns true if the rectangle’s length and width are the same and false otherwise.



II. Write a class definition for a RectangleDriver class that contains a main method that acts as a driver for the Rectangle class of the previous problem. The main method should do this:

· Construct a Rectangle object named rect.

· Use the mutator methods to prompt the user for values to rect’s length and width fields.

· Use the Boolean method to determine if rect is a square.

o If rect is a square, print “Square: ” and then the square’s dimensions. For example:

Square: 4x4

o If rect is not a square, print “Rectangle: ” and then the rectangle’s dimensions. For example:

Rectangle: 6x13

· In printing the above messages, use the accessor methods to retrieve the rectangle’s dimensions.



III. Add a print method to the rectangle class, which prints a rectangle of asterisks corresponding to the dimensions of the rectangle; if the width is greater than 40 or the height is greater than 24 instead of printing the rectangle with asterisks, display the message “too big to print”.


Explanation / Answer

Dear,

I)Rectangle class

public class Rectangle
{
   private double length;
   private double width;

   /**
      The setLength method stores a value in the
      length field.
      @param len The value to store in length.
   */

   public void setLength(double len)
   {
      length = len;
   }

   /**
      The setWidth method stores a value in the
      width field.
      @param w The value to store in width.
   */

   public void setWidth(double w)
   {
      width = w;
   }

   /**
      The getLength method returns a Rectangle
      object's length.
      @return The value in the length field.
   */

   public double getLength()
   {
      return length;
   }

public class RectangleDriver
{
   public static void main(String[] args)
   {
      // Create a Rectangle object.
      Rectangle rect = new Rectangle();

      // Call the object's setLength method, passing 10.0
      // as an argument.
      rect.setLength(16.0);
     
      // Call the object's setWidth method, passing 20.0
      // as an argument.
      rect.setWidth(4.0);
    if(rect.isSquare())
     System.out.println("Square:"+ rect.getLength()+"X"+rect.getWidth());
    else
           System.out.println("Rectangle:"+ rect.getLength()+"X"+rect.getWidth());
      //Exit program
    System.exit(0);
    
   }//end main
}

Hope this will help you..