In: Computer Science
Write a Java program such that it consists of 2 classes:
1. a class that serves as the driver (contains main())
2. a class that contains multiple private methods that compute and display
a. area of a triangle (need base and height)
b area of a circle (use named constant for PI) (need radius)
c. area of rectangle (width and length)
d. area of a square (side)
e. surface area of a solid cylinder (height and radius of base)
N.B. You will also need an accessor public method (interface) that is called from the driver. This method should:
a. Display a menu of options to the user. (Suggest use switch statement)
b. Accept input from the user (keyboard) representing parameter values needed.
c. Invoke other [private] methods in the class to perform the necessary area computations
Add comments throughout your code as you deem appropriate.
shape.java :
//import package
import java.util.Scanner;
//Java class
public class shape {
// Object of Scanner class
Scanner sc = new Scanner(System.in);
//private methods that compute and display area of a
triangle
private void triangleArea(double base, double height)
{
// compute and print area of
triangle
System.out.println(
"Area of triangle with base " + base + " and
height " + height + " is : " + (base * height) / 2);
}
// private methods that compute and display area of
a circle
private void circleArea(double radius) {
// compute and print area of
circle
System.out.println("Area of circle
with radius " + radius + " is : " + (Math.PI * radius *
radius));
}
// private methods that compute and display area of
rectangle (width and length)
private void rectangleArea(double width, double
length) {
// compute and print area of
rectangle
System.out.println(
"Area of rectangle with width " + width + " and
length " + length + " is : " + (width * length));
}
// private methods that compute and display area of
a square (side)
private void squareArea(double side) {
// compute and print area of
square
System.out.println("Area of square
with side " + side + " is : " + (side * side));
}
// private methods that compute and display surface
area of a solid cylinder
// (height and radius of base)
private void cylinderSurafceArea(double height, double
radius) {
// compute and print surface area
of a solid cylinder (height and radius of
// base)
System.out.println("Area of solid
cylinder with height " + height + " and radius " + radius + " is :
"
+ (2.0 * Math.PI * radius * height + 2.0 *
Math.PI * radius * radius));
}
// This method will display menu to the user
public void displayMenu() {
System.out.println("Select Shape to
calculate area : ");
System.out.println("1.Area of a
triangle");
System.out.println("2.Area of a
circle ");
System.out.println("3.Area of a
rectangle ");
System.out.println("4.Area of a
square ");
System.out.println("5.Area of a
solid cylinder ");
// asking user choice
System.out.print("Select choice :
");
// reading choice
int choice = sc.nextInt();
// using switch statement
switch (choice) {
case 1:// when area of triangle is
selected
// asking user base
System.out.print("Enter base : ");
double base =
sc.nextDouble();// reading base
// asking user
height
System.out.print("Enter height : ");
double height =
sc.nextDouble();// reading height
// call method
and pass base and height
triangleArea(base, height);
break;
case 2:// when area of circle is
selected
// asking user radius
System.out.print("Enter radius : ");
double radius =
sc.nextDouble();// reading radius
// call method
and pass radius
circleArea(radius);
break;
case 3:// when area of rectangle is
selected
// asking user width
System.out.print("Enter width : ");
double width =
sc.nextDouble();// reading width
// asking user
length
System.out.print("Enter length : ");
double length =
sc.nextDouble();// reading length
// call method
and pass width and length
rectangleArea(width, length);
break;
case 4:// when area of square is
selected
// asking user side
System.out.print("Enter side : ");
double side =
sc.nextDouble();// reading side
// call method
and pass side
squareArea(side);
break;
case 5:// when area of solid
cylinder is selected
// asking user width
System.out.print("Enter height : ");
double height1 =
sc.nextDouble();// reading height
// asking user
radius of base
System.out.print("Enter radius of base : ");
double
radiusOfBase = sc.nextDouble();// reading radius of base
// call method
and pass height and radius
cylinderSurafceArea(height1, radiusOfBase);
break;
default ://when invalid choice is
entered
System.out.println("Enter valid choice.");
}
}
}
***********************************
shapeTest.java :
//import package
import java.util.*;
//Java class
public class shapeTest {
// main() method
public static void main(String[] args) {
// creating obejct of Shape
class
shape s = new shape();
// calling method to display
menu
s.displayMenu();
}
}
=========================================
Screen showing area of triangle :
Screen showing area of side :