Design and implement a Java program (name it ComputeAreas) that defines four met
ID: 3674881 • Letter: D
Question
Design and implement a Java program (name it ComputeAreas) that defines four methods as follows: Method squareArea (double side) determines and returns the area of a square. Method rectangleArea (double width, double length) determines and returns the area of a rectangle. Method circleArea (double radius) determines and returns the area of a circle. Method triangleArea (double base, double height) determines and returns the area of a triangle. Test the methods with different input value read from the user (in the main method). Design the main method of your program such that it allows the user to re-run the program with different inputs (i.e., use a loop structure). Document your code, and organize and space the outputs properly. Use escape characters to organize the outputs. Sample outputs, for shape types selected by the user, are: Square Side = 5.1 Square Area = 26.01 Rectangle Width = 4.0 Rectangle Length = 5.5 Rectangle Area = 22.0 Circle Radius = 2.5 Circle Area = 19.625 Triangle Base = 6.4 Triangle Height - 3.6 Triangle Area - 22.0Explanation / Answer
ComputeAreas.java
//This ComputeAreas class is used to calculate Different shapes areas
package org.students;
public class ComputeAreas {
// This method is used to calculate the Area of the Square.
public double squareArea(double side) {
double squareArea = side * side;
return squareArea;
}
// This method is used to calculate the Area of the rectangle.
public double rectangleArea(double width, double length) {
double rectangleArea = width * length;
return rectangleArea;
}
// This method is used to calculate the Area of the circle.
public double circleArea(double radius) {
double circleArea = 3.14 * radius * radius;
return circleArea;
}
// This method is used to calculate the Area of the traingle.
public double traingleArea(double base, double height) {
double traingleArea = 0.5 * base * height;
return traingleArea;
}
}
-----------------------------------------------------------------------------------------------------
Test.java
package org.students;
import java.util.Scanner;
// This the Test class
public class Test {
public static void main(String[] args) {
while (true) {
//Enter any number other than Zero(0) to run the program.
System.out.print("Enter Zero(0) to exit the program (or) any number to run the program:: ");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
/* if num not equal to 0 then enters into if.
This will continue to execute until we press Zero when asked to exit. */
if (num != 0) {
System.out.println(" ");
//Here we used escape characters for uniform output.
System.out.print("Enter side of the Square value: ");
Scanner sc1 = new Scanner(System.in);
double side = sc1.nextDouble();
ComputeAreas ca1 = new ComputeAreas();
double sarea = ca1.squareArea(side);
//Here we used escape characters for uniform output.
System.out.println("The Area Of the Square is : " + sarea);
System.out.println(" ");
//Here we used escape characters for uniform output.
System.out.print("Enter Width of the rectangle value: ");
Scanner sc2 = new Scanner(System.in);
double width = sc2.nextDouble();
//Here we used escape characters for uniform output.
System.out.print("Enter length of the rectangle value: ");
Scanner sc3 = new Scanner(System.in);
double length = sc3.nextDouble();
ComputeAreas ca2 = new ComputeAreas();
double rarea = ca2.rectangleArea(width, length);
//Here we used escape characters for uniform output.
System.out.println("The Area Of the Rectangle is : " + rarea);
System.out.println(" ");
//Here we used escape characters for uniform output.
System.out.print("Enter radius of the circle value: ");
Scanner sc4 = new Scanner(System.in);
double radius = sc4.nextDouble();
ComputeAreas ca3 = new ComputeAreas();
double carea = ca3.circleArea(radius);
//Here we used escape characters for uniform output.
System.out.println("The Area Of the Circle is : " + carea);
System.out.println(" ");
//Here we used escape characters for uniform output.
System.out.print("Enter base of the triangle value: ");
Scanner sc5 = new Scanner(System.in);
double base = sc5.nextDouble();
//Here we used escape characters for uniform output.
System.out.print("Enter height of the triangle value: ");
Scanner sc6 = new Scanner(System.in);
double height = sc6.nextDouble();
ComputeAreas ca4 = new ComputeAreas();
double tarea = ca4.traingleArea(base, height);
//Here we used escape characters for uniform output.
System.out.println("The Area Of the Triangle is : " + tarea);
System.out.println(" ");
continue;
} else
System.out.println("** Program Exit **");
//This statement is used to exit the program.
System.exit(0);
}
}
}
-------------------------------
output:
Enter Zero(0) to exit the program (or) any number to run the program:: 1
Enter side of the Square value: 12
The Area Of the Square is : 144.0
Enter Width of the rectangle value: 13
Enter length of the rectangle value: 23
The Area Of the Rectangle is : 299.0
Enter radius of the circle value: 33
The Area Of the Circle is : 3419.46
Enter base of the triangle value: 22
Enter height of the triangle value: 22
The Area Of the Triangle is : 242.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.