Question

In: Computer Science

Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area...

Writing a Java Code

Requirements of the JAVA program:

Your task is to calculate geometric area for 3 shapes(square, rectangle and circle).

  1. You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input
    1. Invalid input should throw a message for the user. Example: Invalid input, please try again
  2. Each options should ask users for relevant data. HINT: use scanner object to take in length for square, length/width for rectangle and radius for circle
    1. At the class level store a constant variable for PI that cannot be changed. You can use 3.14 for the value
  3. Each option should call relevant method that is created at the class level. Methods should take in input received from the users in the step above and return values. HINT: one method for square area calculation, one for rectangle area calculation and one for circle area calculation. Square and rectangle should return int, circle should return double

Expected output:


***Area Calculator***

S  --  Square

R  --  Rectangle

C  --  Circle

S

Please enter length of the square:

2

Area of your square is: 4

***Area Calculator***

S  --  Square

R  --  Rectangle

C  --  Circle

R

Please enter length of the rectangle:

2

Please enter length of the rectangle:

3

Area of your rectangle is: 6

***Area Calculator***

S  --  Square

R  --  Rectangle

C  --  Circle

C

Please enter the redius of the circle:

3

Area of your circle is: 28.26

Solutions

Expert Solution

package add;
import static java.lang.System.exit;
import java.util.*;

public class Geometry {
    static float pi=(float) 3.14;
     float squareArea(float x)
    {
        return x*x;
    }
    float rectangleArea(float x, float y)
    {
        return x*y;
    }
    float circleArea(float r)
    {
        return (float) (pi*r*r);
    }
    public static void main(String args[]){
        Geometry obj = new Geometry();
       
        Scanner s=new Scanner(System.in);
        char sc;
        float n,n1,r,a;
        while(true)
        {
            System.out.println("\n***Area Calculator***");
            System.out.println("S  --  Square");
            System.out.println("R  --  Rectangle");
            System.out.println("C  --  Circle");
            sc=s.next().charAt(0);
            switch(sc)
            {
                case 'S':
                    System.out.println("Please enter length of the square:");
                     n=s.nextFloat();
                    a=obj.squareArea(n);
                    System.out.print("Area of your square is:"+a);
                    break;
                case 'R':
                    System.out.println("Please enter length of the rectangle:");
                    n=s.nextFloat();
                    System.out.println("Please enter breadth of the rectangle:");
                    n1=s.nextFloat();
                    a=obj.rectangleArea(n,n1);
                    System.out.print("Area of your rectangle is:"+a);
                    break;
                case 'C':
                    System.out.println("Please enter the redius of the circle:");
                    r=s.nextFloat();
                    a=obj.circleArea(r);
                    System.out.print("Area of your circle is:"+a);
                    break;
                default:System.out.println("Wrong choice");
                    exit(1);
            }
        }
    }
    
}

OUTPUT:-


***Area Calculator***
S -- Square
R -- Rectangle
C -- Circle
S
Please enter length of the square:
2
Area of your square is:4.0
***Area Calculator***
S -- Square
R -- Rectangle
C -- Circle
R
Please enter length of the rectangle:
2
Please enter breadth of the rectangle:
3
Area of your rectangle is:6.0
***Area Calculator***
S -- Square
R -- Rectangle
C -- Circle
C
Please enter the redius of the circle:
3
Area of your circle is:28.26
***Area Calculator***
S -- Square
R -- Rectangle
C -- Circle
k
Wrong choice
Java Result: 1


Related Solutions

REQUIREMENTS OF THE JAVA PROGRAM: Your task is to calculate geometric area for 3 shapes(square, rectangle...
REQUIREMENTS OF THE JAVA PROGRAM: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). 1. You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input a. Invalid input should throw a message for the user. Example: Invalid input, please try again 2. Each options should ask users for relevant data....
In java, implement a program that returns a value of the area of the geometric objects...
In java, implement a program that returns a value of the area of the geometric objects listed below. Use the technique of method overloading concept and also uses the interfaces for dynamic method invocation. Triangle: √?(? − ?)(? − ?)(? − ?) where s=(a+b+c)/2 a,b,c are the sides of the triangle. Square: A^2 where A is the side Rectangle: (a*b) where a ,b are sides Circle: ? = ??2 where r is the radius Cube: 6a^2 where a is the...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant data. HINT: use scanner object to take in length for square,...
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
write this program in java... don't forget to put comments. You are writing code for a...
write this program in java... don't forget to put comments. You are writing code for a calendar app, and need to determine the end time of a meeting. Write a method that takes a String for a time in H:MM or HH:MM format (the program must accept times that look like 9:21, 10:52, and 09:35) and prints the time 25 minutes later. For example, if the user enters 9:21 the method should output 9:46 and if the user enters 10:52...
1) Write a functional program in Java that can calculate the volume and surface area of...
1) Write a functional program in Java that can calculate the volume and surface area of a sphere and a cube 2) Write a procedural program in Java that can calculate the volume and surface area of a sphere and a cube 3) Write an Object Oriented Program in Java that can find the volume and surface area of a sphere and cube
Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is...
Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is to fill in the code marked as "...your code here...". A detailed explanation follows the code. import java.util.*; public class OddManOut {    public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("How many random Integers to produce?"); int num = sc.nextInt();    ArrayList<Integer> randomInts = createRandomList(num); System.out.println("The random list is: "); System.out.println(randomInts);    removeOdds( randomInts ); System.out.println("The random list with only...
/* Writing a program that displays the following: calculating area of a circle, calculating area of...
/* Writing a program that displays the following: calculating area of a circle, calculating area of a rectangle, calculating area of a triangle, and quit. */ import java.text.DecimalFormat; import java.util.Scanner; public class GeoCalculator {    public static void main(String arg[]) { char selection; Scanner get = new Scanner(System.in); //having user input DecimalFormat twoDigits = new DecimalFormat("0.00"); //formatting area to two decimal places //display choice of selection System.out.println("Geometry Calculator"); System.out.println("Please select from the following menu:"); System.out.println("1. Calculate the Area of a...
WRITE CODE IN JAVA it is now your turn to create a program of your choosing....
WRITE CODE IN JAVA it is now your turn to create a program of your choosing. If you are not sure where to begin, think of a task that you repeat often in your major that would benefit from a program. For example, chemistry unit conversions, finding the area for geometric shapes, etc. You can also create an interactive story that changes based on the user input/decisions. The possibilities are endless. The program must include instructions for the user. Be...
CODE IN PYTHON: Your task is to write a simple program that would allow a user...
CODE IN PYTHON: Your task is to write a simple program that would allow a user to compute the cost of a road trip with a car. User will enter the total distance to be traveled in miles along with the miles per gallon (MPG) information of the car he drives and the per gallon cost of gas. Using these 3 pieces of information you can compute the gas cost of the trip. User will also enter the number of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT