Rectangle

In this assignment you are going to implement two classes: a Rectangle class and a RectangleManager class. The RectangleManager class will be a client of the Rectangle class. It will also contain the main method for the program.

Directions

Rectangle class

The Rectangle class is going to the store the dimensions of a rectangle and provide methods for calculating its perimeter and area.

Create a file named Rectangle.java and add the following code.

public class Rectangle
{
   // instance variables
   
   // constructors
   
   // accessor methods
   
   // mutator methods
   
   // other methods
   
}

Instance Variables

We will need two variables to store the rectangle's length and width. Add the following code to the instance variables section.

private int length;     // rectangle's length
private int width;      // rectangle's width

Constructors

  1. Create one constructor for the Rectangle class that will allow a client to assign specific values to its length and width variables.

Accessor Methods

  1. Create an accessor method for the length variable and the width variable.

Mutatator Methods

Methods in a class that allow clients to modify an object's instance variables are called mutator methods. In our implementation of the Rectangle class, we are going to define two mutator methods. The first method will give clients the ability to modify the length instance variable. Add the following code to the mutator section.

public void setLength(int l)
{
   length = l;
}

Next we implement a mutator method for the width instance variable.

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

Other Methods

Add two other methods to the Rectangle class that will calculate the perimeter and area of a rectangle. Place them in the other methods section.

  1. Implement a method named getPerimeter. This method should calculate and return the perimeter of the rectangle.
  2. Implement a method named getArea. This method should calculate and return the area of the rectangle.


Client Program - RectangleManager class

The purpose of the RectangleManager class is to test the fuctionality of the Rectangle class. To do this it instantiates a Rectangle object, allows the user to enter the length and width values for a rectangle, calculates the perimeter and area of the given rectangle, and then displays the results. It also provides the ability to change the dimensions of the rectangle.

Create a file named RectangleManager.java and add the following code.

import java.util.*;

public class RectangleManager
{  
   // instance variables
   private Rectangle rect;
   
   public void inputDimensions(Scanner keyboard)
   {
   
   }
   
   public void changeDimensions(Scanner keyboard)
   {
   
   }
   
   public void print()
   {
   
   }
   
   public static void main(String[] args)
   {
       RectangleManager app = new RectangleManager();
       Scanner keyboard = new Scanner(System.in);
       String ans="y";
	   
       app.inputDimensions(keyboard);
       app.print();
	   
       System.out.println();
       System.out.print("Change Dimensions[y/n]-->");
       keyboard.nextLine();  // dummy read
       ans = keyboard.nextLine();
	   
       while(ans.equals("y"))
       {
           app.changeDimensions(keyboard);
           app.print();
		   
           System.out.println();
           System.out.print("Change Dimensions[y/n]-->");
           keyboard.nextLine();  // dummy read
           ans = keyboard.nextLine();
       }    
   }	
}

Instance Variables

A Rectangle object variable named rect has been declared. It will be assigned a Rectangle object in the inputDimensions method.

inputDimensions Method

  1. Implement the inputDimensions method. This method should prompt the user for the dimensions of a rectangle then instantiate a Rectangle object using the values input by the user as parameters to the constructor. The object's reference should be assigned to the variable rect.

changeDimensions Method

  1. Implement the changeDimensions method. This method should prompt the user for the dimensions of a rectangle then use the Rectangle class's mutator methods to update the length and width values of rect.

print Method

  1. Implement the print method so that it will display rect's length, width, perimeter, and area.

Source File

Rectangle.java

RectangleManager.java

Sample Run

Enter length -->10
Enter width -->20

Length    = 10
Width     = 20
Perimeter = 60
Area      = 200

Change Dimensions[y/n]-->y
Enter length -->15
Enter width -->30

Length    = 15
Width     = 30
Perimeter = 90
Area      = 450

Change Dimensions[y/n]-->y
Enter length -->2
Enter width -->2

Length    = 2
Width     = 2
Perimeter = 8
Area      = 4

Change Dimensions[y/n]-->n