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