rite a program that will calculate the perimeter and area of a rectangle. Prompt
ID: 3790552 • Letter: R
Question
rite a program that will calculate the perimeter and area of a rectangle. Prompt the user to input the length and width. Calculate the area as length * width. Calculate the perimeter as 2* length + 2* width. Display the area and perimeter.
Please use a proper heading in a comment block (name, date, description of the program). Please use comments in your code.
Run your program twice using different input. Copy the output and place it at the bottom of the .java class file as comments OR copy the output to a Word document and submit with the class.
Explanation / Answer
RectangleAreaTest.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class RectangleAreaTest {
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("Enter the length: ");
double length = scan.nextDouble();//reading length of rectangle
System.out.println("Enter the width: ");
double width = scan.nextDouble();//reading width of rectangle
double area = length * width; //calculating area
double perimeter = 2 * (length + width);//calculating perimter
System.out.println("Area is "+df.format(area));//displaying area
System.out.println("Perimeter is "+df.format(perimeter));//displaying perimeter
}
}
Output:
Enter the length:
2
Enter the width:
5
Area is 10.0
Enter the length:
1.1
Enter the width:
2.2
Area is 2.42
Perimeter is 6.60
Perimeter is 14.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.