Question

Write a Java program that prompts user to calculate area of five different shapes using java...

Write a Java program that prompts user to calculate area of five different shapes using java library, and at least 2 classes - more of OOP, also use divide and conquer methods.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.util.*;

class AreaDemo
{
double areaCircle(double radius)
{
return(Math.PI * Math.pow(radius,2));
}

double areaSphere(double radius)
{
return (4*Math.PI*Math.pow(radius,2));
}

double areaRectangle(double length,double width)
{
return(length * width);
}

double areaCylinder(double radius,double height)
{
return (Math.PI*Math.pow(radius,2)*height);
}

double areaTriangle(double base,double height)
{
return(0.5 * base * height); // 1/2 = 0.5
}
}

class TestArea
{
public static void main(String args[])
{
AreaDemo ad=new AreaDemo();

double radius,len,wid,height,base;

Scanner sc=new Scanner(System.in);
System.out.println("\nMenu Option: ");
System.out.println("---------------------------");
System.out.println("1. Area of Circle");
System.out.println("2. Area of Sphere");
System.out.println("3. Area of Rectangle");
System.out.println("4. Area of Cylinder");
System.out.println("5. Area of Triangle\n");

while(true)
{
System.out.print("\nEnter your choice: ");
int choice=sc.nextInt();

switch(choice)
{
case 1:
System.out.print("\nEnter a radius of a Circle: ");
radius=sc.nextDouble();
System.out.printf("\nArea of Circle: %.2f\n",ad.areaCircle(radius));
break;

case 2:
System.out.print("\nEnter a radius of a Sphere: ");
radius=sc.nextDouble();
System.out.printf("\nArea of Sphere: %.2f\n",ad.areaSphere(radius));
break;

case 3:
System.out.print("\nEnter a length of a Rectangle: ");
len=sc.nextDouble();
System.out.print("Enter a width of a Rectangle: ");
wid=sc.nextDouble();
System.out.printf("\nArea of Reactangle: %.2f\n",ad.areaRectangle(len,wid));
break;

case 4:
System.out.print("\nEnter a radius of Cylinder: ");
radius=sc.nextDouble();
System.out.print("Enter a height of a Cylinder: ");
height=sc.nextDouble();
System.out.printf("\nArea of Cylinder:%.2f ",ad.areaCylinder(radius,height));
break;

case 5:
System.out.print("\nEnter a base of a Triangle: ");
base=sc.nextDouble();
System.out.print("Enter a height of a Triangle: ");
height=sc.nextDouble();
System.out.printf("\nArea of Triangle: %.2f\n",ad.areaTriangle(base,height));
break;

default:
System.out.println("\nInvalid option!!\n");
break;
}
System.out.print("\nDo you want to continue(yes/no): ");
String yesNo=sc.next();
if(yesNo.equalsIgnoreCase("no"))
{
break;
}
}
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a Java program that prompts user to calculate area of five different shapes using java...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle,...

    Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle, Circle, and Parallelogram), then calculate area of the shape,must have input validation and uses more of java library like math class, wrapper class, string methods, use formatted output with the printf method. Finally, create a pseudo-code statement and flowchart

  • Write a program in Java that can calculate and output the area of three different shapes;...

    Write a program in Java that can calculate and output the area of three different shapes; a rectangle, a triangle and a circle. Let the user choose which shape they want by typing the words “rectangle”, “triangle” or “circle” (you may use integers, strings or chars to indicate which shape the user prefers to calculate the area of). Then, have the user enter the values of the dimension(s) appropriate for each shape in the form of doubles.

  • write a java program that prompts a user to enter two different numbers, divide the first...

    write a java program that prompts a user to enter two different numbers, divide the first number by second and prints the result

  • NO LOOPS, write a program, in Java, that prompts the user for a hexadecimal string of...

    NO LOOPS, write a program, in Java, that prompts the user for a hexadecimal string of EXACTLY 4 hex digits, no more, no less, converts the hexadecimal value to its decimal value using string, character parsing, and Math methods learned in this chapter, and then prints the original string and its converted decimal value. NOTE: Hex digits may be in UPPER or lower case.

  • Please help me with this program in Java: Write a Java program that prompts the user...

    Please help me with this program in Java: Write a Java program that prompts the user to enter a password that matches a specific pattern. Your program must approve the user's entry.. Here is the pattern, in this order: 1 or more upper case letters two lower case letters 1 or 2 digits zero or 1 upper case letters any two of this group @#$%^&

  • solve it with Java please Write a Java program that prompts the user to input length...

    solve it with Java please Write a Java program that prompts the user to input length and width of a shape. If length is equal to width, call a method to calculate the area of the shape and send only one side as parameter to this method. This meth return the area of the shape. If length is not equal to width, call a method to calculate the area of the shape and send both width and length as parameters...

  • Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to...

    Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to enter an item’s name for each position in the array. The program then prints uses a loop to output the array. Example 1: Banana 2: Orange 3: Milk 4: Carrot 5: Chips Banana, Orange, Milk, Carrot, Chips Problem 2: Print Characters in String Array    Declare a string array of size 5. Prompt the user enters five strings that are stored in the array....

  • Write a Java console application that prompts the user to enter the radius of a circle,...

    Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. Write a JavaFX GUI application to do the same calculation, and draw the circle. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Write and document your program per class coding conventions. Add an instance variable double radius. Generate...

  • Write a complete java program that prompts the user for their name and two numbers. The...

    Write a complete java program that prompts the user for their name and two numbers. The correct java methods and parameters must be passed to find the length of the name entered and return “TRUE” if the sum of numbers is even, or FALSE if the sum of numbers is odd: Notes included in the program Sample Program Please enter a name and I will tell you the length of the name entered John Then length of the name John...

  • Write a Java program that prompts the user to enter a password that matches a specific...

    Write a Java program that prompts the user to enter a password that matches a specific pattern. Your program must approve the user's entry.. Here is the pattern, in this order: 1 or more upper case letters two lower case letters 1 or 2 digits zero or 1 upper case letters any two of this group @#$%^&

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT