Topics

Working with Objects

Description

Write a program to compute the perimeter and the area of two different rectangles. The user will input the length and the width of the two rectangles and the program will display the perimeter and area of each of the two rectangles.

Do this assignment using objects.

Implementation

Create a project asRectangle.

In the above project, do the following:

Create a class Rectangle not containing the main method. In the class, create private instance variables for length (double) and width (double). Provide a public constructor for initializing length and width. Also provide public instance methods for computing perimeter and area. Additionally, provide public accessor (get) methods for accessing length and width. [Provide getLength( ) and getWidth ( ) methods].

Create a class TestRectangle containing the method main.

In the method main method of the above class, do the following:

Input the length and the width of the first rectangle. Create an object of type Rectangle and initialize its values via the constructor with the length and the width supplied by the user.

Input the length and the width of the second rectangle. Create an object of type Rectangle and initialize its values via the constructor with the length and the width supplied by the user for the second rectangle.

Call the object's methods of the first object to compute its perimeter and area. Also call the objects accessor (get) methods to retrieve length and width values.

Call the object's methods of the second object to compute its perimeter and area. Also call the objects accessor (get) methods to retrieve length and width values of this object.

Display in a single dialog box the following:
(You can do that by first creating a string variable string. Then building the whole output string in this variable using string concatenation. Then display that string. See Sample Code section below.)

The values of length, width, perimeter and area of the first rectangle.
The values of length, width, perimeter and area of the first rectangle.

Input and Output

Use JOptionPane.showInputDialog ( ) for input and JOptionPane.showMessageDialog ( ) for output. For this purpose, import javax.swing.JOptionPane. (For importing, hover the cursor on JOptionPane line till a pop-up menu shows up, Select import in the pop-up menu, Eclipse will automatically insert the import line at the proper place in the code).

Test Data

Test Input

Use the following length and width values for the first rectangle:
30, 20
Use the following length and width values for the second rectangle:
60, 40

Test Output:
(For your output, it is ok to have the same values with or without decimal points.)

First Rectangle:
Length: 30
Width: 20
Area: 600
Perimeter: 100

Second Rectangle:
Length: 60
Width: 40
Area: 2400
Perimeter: 200

Sample Code

public class Rectangle
{
//instance variables
private double length;
private double width;

//Constructor
public Rectangle (double l, double w)
{
//initialize instance variables above using the parameters received in the constructor
length = l;
width = w;
}

//accessor methods
public double getLength ( )
{
return length;
}
public double getWidth ( )
{
return length;
}

//other instance methods
public double compArea ( )
{
double a;
//write the code below to compute area and store area in a

//end code
return a;
}

public double compPerimeter ( )
{
//local variables
double p;
//write the code below to compute perimeter and store perimeter in p

//end code
return p;

}

}


public class TestRectangle
{
public static void main (String [] args)
{
String in, out;
double l, w;
double area, perim;
//input length and width values from the user for first object
//input values will be in text form
//they will be converted in a double form
in=JOptionPane.showInputDialog ("Enter length");
l=Double.parseDouble (in);
in=JOptionPane.showInputDialog ("Enter width");
w=Double.parseDouble (in);

//create the Rectangle object
Rectangle r1 = new Rectangle (l, w);

//input length and width values from the user for second object



//create the second Rectangle object
Rectangle r2 = new Rectangle (l, w);

//Call methods of the first Rectangle objects
l = r1.getLength ( );
w= r1.getWidth ( );
area= r1.getArea ( );
perim= r1.getPerim ( );

//build output string
out="First Rectangle:n";
out=out + "Length: " + length + "n";
out= out + "Width: " + width + "n";
out= out + "Area: " + area + "n";
out= out + "Perimeter: " + perim + "n";

//Call methods of the second Rectangle objects




//continue building output string
//concatenate the value for second object
out=out+"nSecond Rectangle:n";






//display the whole out in a single dialog box
JOptionPane.showMessageDialog (null, out);

}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.