Description

Write a program to compute area and perimeter of a rectangle. The program will input length and width of the rectangle from the user, compute area and perimeter, and display length, width, area, and perimeter.

Do this assignment using procedural programming.

Implementation

Create a project asRectangleProc.

In the above project, create a class RectangleProc with a main method in it.

Write a static method computeArea in the above class that has two parameters double len and double wid to receive length and width values and returns area as a double.

Write a static method computePerim in the above class that has two parameters double len and double wid to receive length and width values and returns perimeter as a double.

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

Input length and width values from the user.
Call static methods computeArea and computePerim to calculate area and perimeter.
Display the complete output in a single message dialog box.

Method Headers

Here are the proposed method headers for computeArea and computePerim:

public static double computeArea (double len, double wid)

public static double computePerim (double len, double wid)

Test Data

Test Input

Test the program for the following length and width values:
30, 20

Test Output:

For the above test input, the output should look as below:

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

Sample Code

import javax.swing.JOptionPane;

public class RectangleProc
{
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);

//call method to find area
area = computeArea (l, w);
//call method to find perimeter
perim= computePerim (l, w);

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

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

}

public static double computeArea (double len, double wid)
{
double a;
a = len * wid;
return a;
}

public static double computePerim (double len, double wid)
{
//write this method like the previous one and return perimeter

return 0;
}
}
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.