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.
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 }
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
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; }
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.
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(); } } }
A Rectangle object variable named rect has been declared. It will be assigned a Rectangle object in the inputDimensions method.
Rectangle.java
RectangleManager.java
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