
Write a java program that calculates the area of rectangular, square,
triangular, and circle.
Your program must have the following:
Two classes named "Area" and "AreaTest".
Class "Area" has the following:
a. Void Square Method with one parameter which is length of
side.
b. Void Rectangle Method has two parameters which are
width and height.
c. Void Triangle Method has two parameters which are base
and vertical height.
d. Void Circle Method has one parameter which is radius.
You must use Math.PI and Math.pow() function for your
calculation.
e. One Void Display Method to print out the calculation result
for all methods (Square, Rectangle, Triangle, and Circle). For
example, if I want to print the area of rectangle, you have to
pass the result as a parameter to Display Method to print it
out. There is one System.out.printf() in this method and
must be as following format:
System.out.printf("The Area of %s is %s" ,str,result);
str = any of these words: Square, Rectangle, Triangle ,or
Circle.
Result = the result of the method calculation.
f. Do not use Instance Variable for your program.
a. Switch statement that maintains accessing each of the
method in (a),(b), (c), and (d) stated above in class "Area"
b. 'For statement loop' to keep the user running your program
until he enters number '5' to exit the program.

The given code calculates the area of square, rectangle, triangle and circle. It allow the user to enter their choice. It then takes the user request and display the result accordingly.
import java.util.*;
class Area
{
public static double square(double side){
double area=side*side;
return area;
}
public static double rectangle(double width,double height){
double area=width*height;
return area;
}
public static double triangle(double base,double height){
double area=(base*height)/2;
return area;
}
public static double circle(double radius){
double area=(Math.PI)*(Math.pow(radius,2));
return area;
}
public static void display(String str,double result){
System.out.println("The Area of "+str+" is: "+result);
}
}
class AreaTest{
public static void main(String[] args)
{
boolean exit=false;
String str=" ";
Scanner input=new Scanner(System.in);
do{
System.out.println("1 Square");
System.out.println("2 Rectangle");
System.out.println("3 Triangle");
System.out.println("4 Circle");
System.out.println("5 Exit");
System.out.print("Enter your choice: ");
int choice=input.nextInt();
switch(choice){
case 1:
System.out.print("Enter side: ");
double s=input.nextDouble();
str="Square";
Area.display(str,Area.square(s));
break;
case 2:
System.out.print("Enter width: ");
double w=input.nextDouble();
System.out.print("Enter height: ");
double h=input.nextDouble();
str="Rectangle";
Area.display(str,Area.rectangle(w,h));
break;
case 3:
System.out.print("Enter base: ");
double b=input.nextDouble();
System.out.print("Enter height: ");
double ht=input.nextDouble();
str="Triangle";
Area.display(str,Area.triangle(b,ht));
break;
case 4:
System.out.print("Enter radius: ");
double r=input.nextDouble();
str="Circle";
Area.display(str,Area.circle(r));
break;
case 5:
exit=true;
System.exit(0);
}
}
while(!exit);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.