Write a program that will calculate either the area of a rectangle, a circle, or
ID: 645974 • Letter: W
Question
Write a program that will calculate either the area of a rectangle, a circle, or a triangle based on the user's selection from a menu.
The main method of the program must:
Display a welcome/usage message to the user, such as:
Display a menu of choices to the user as shown below.
Read the choice entered by the user, which should be an integer value of 1, 2 or 3.If the user chooses 1, then the program should call a method that will:
Prompt and read the length and width of the rectangle.
Calculate the area of the rectangle.
Return the value that was calculated.
if the user chooses 2, then the program should call a method that will:
Prompt and read the radius of a circle.
Calculate the area of the circle.
Return the value that was calculated.
if the user chooses 3, then the program should call a method that will:
Prompt and read the base and height of a triangle.
Calculate the area of the triangle.
Return the value that was triangle.
For entries of 1, 2 or 3, when the method returns the area to the calling statement of method main, the value should be printed.
if the user chooses 4, then the program should print a good bye message and exit.
if the user enters any number besides 1, 2, 3 or 4, the program should print an error message.
The program should repeatedly display the menu, read & process the user's choice until the choice is 4.
Example output: Note that anything in red denotes a value entered by the user. :
Following method main, three static methods for calculating the areas must be written using the following specifications:
a method named rectangleArea that will interactively read values for width and length, and then calculate and return the area.
a method named circleArea that will interactively read a value for radius and then calculate and return the area.
a method named triangleArea that will interactively read values for base and height, and then calculate and return the area.
Project Requirements:
Name the class AreaCalculatorXYZ, where XYZ are your three initials.
Explanation / Answer
import java.io.*;
class area
{
void findarea(int a, int b)
{
System.out.println( " Area of rectangle with breadth "+a+" and lenght " +b+ " is :" + a*b);
}
void findarea(int a)
{
System.out.println( " Area of circle with radius " +a+ " is :" + 3.14 * a);
}
void findarea(int a, int b, int c)
{
double temp = (a + b + c);
double s= temp/2;
double triarea = Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println( " Area of triangle with lenght of sides "+a+"," +b+ " and " +c+" is : "+ triarea);
}
public static void main(String p[]) throws IOException
{
area d = new area();
BufferedReader Br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(" Find area of 1 . Rectangle 2 . Triangle 3 . Circle Select a choice : ");
int choice =Integer.parseInt(Br.readLine());
switch(choice)
{
case 1:
System.out.print(" Enter the breadth : ");
int a =Integer.parseInt(Br.readLine());
System.out.print(" Enter the lenght : ");
int b=Integer.parseInt(Br.readLine());
d.findarea(a,b);
break;
case 2:
System.out.print(" Enter the lenght of first side : ");
int x =Integer.parseInt(Br.readLine());
System.out.print(" Enter the lenght of second side : ");
int y=Integer.parseInt(Br.readLine());
System.out.print(" Enter the lenght of third side : ");
int z =Integer.parseInt(Br.readLine());
d.findarea(x,y,z);
break;
case 3:
System.out.print(" Enter the radius : ");
int r =Integer.parseInt(Br.readLine());
d.findarea(r);
break;
default:
System.out.println("Invalid choice");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.