Design and implement a Java program (name it ComputeAreas) that defines ALL four
ID: 3596300 • Letter: D
Question
Design and implement a Java program (name it ComputeAreas) that defines ALL 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.0
Explanation / Answer
Please find my implementation.
import java.util.Scanner;
public class ComputeAreas {
public static double squareArea (double side) {
return side*side;
}
public static double rectangleArea (double width, double length) {
return width*length;
}
public static double circleArea (double radius) {
return Math.PI*radius*radius;
}
public static double triangleArea (double base, double height) {
return 0.5*base*height;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char c = 'y';
while(c == 'y' || c == 'Y') {
System.out.print("Square Side = ");
double side = sc.nextDouble();
System.out.println("Square Area = "+squareArea(side));
System.out.print("Rectangle Width =");
double width = sc.nextDouble();
System.out.print("Rectangle Length = ");
double length = sc.nextDouble();
System.out.println("Rectangle Area = "+rectangleArea(width, length));
System.out.print("Circle Radius = ");
double radius = sc.nextDouble();
System.out.println("Circle Area = "+circleArea(radius));
System.out.print("Triangle Base = ");
double base = sc.nextDouble();
System.out.print("Triangle Height = ");
double height = sc.nextDouble();
System.out.println("Triangle Area = "+triangleArea(base, height));
System.out.println(" Do you want to run again (y/n) ? ");
c = sc.next().charAt(0);
}
sc.close();
}
}
/*
Sample run:
Square Side = 3.4
Square Area = 11.559999999999999
Rectangle Width =4
Rectangle Length = 3
Rectangle Area = 12.0
Circle Radius = 5.4
Circle Area = 91.60884177867837
Triangle Base = 5
Triangle Height = 6
Triangle Area = 15.0
Do you want to run again (y/n) ?
n
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.