Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Description

Write a Java 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.

Write a class Rectangle with private instance variables for length and width. Provide a public constructor for initializing length and width. Also provide public methods for computing perimeter and area. Additionally provide public accessor (get) methods for accessing length and width. (For example, provide getLength( ) and getWidth ( ) methods).

Write a class TestRectangle containing the method main. From the method main, prompt the user to 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. Prompt the user to 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.

Then call the objects methods of the first object to compute its perimeter and area. Use the objects accessor (get) methods to retrieve length and width values.

Then call the objects methods of the second object to compute its perimeter and area. Use the objects accessor (get) methods to retrieve length and width values of this object.

Then display values of length, width, perimeter and area of the first rectangle followed by those of the second rectangle.

Instructions

Use JOptionPane.showInputDialog ( ) for entering input values and JOptionPane.showMessageDialog ( ) for displaying the result. Import javax.swing.* package for using these classes.

Test Data

Test the program:

Using the following length and width values for the first object:

30, 20

Using the following length and width values for the first object:

60, 40

Test Output:

The output should look as below:

Values for the first object:

Length: 30

Width: 20

Perimeter: 100

Area: 600

Values for the second object:

Length: 60

Width: 40

Perimeter: 200

Area: 2400

Sample Rectangle class

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;

}

//instance methods

public double getLength ( )

{

return length;

}

public double getWidth ( )

{

return length;

}

public double compArea ( )

{

double a;

//write the code here to compute area and store it in a

//end code

return a;

}

public double compPerimeter ( )

{

//local variables

double p;

//write the code here to compute perimeter and store it in p

//end code

return p;

}

}

public class TestRectangle

{

public static void main (String [] args)

{

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

//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 Rectangle objects to find values of

//display the results

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems 12th Asian Conference ACIIDS 2020 Phuket Thailand March 23 26 2020 Proceedings

Authors: Pawel Sitek ,Marcin Pietranik ,Marek Krotkiewicz ,Chutimet Srinilta

1st Edition

9811533792, 978-9811533792

More Books

Students also viewed these Databases questions

Question

Define and measure service productivity.

Answered: 1 week ago