Question

In: Computer Science

/* 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 Cirlce.");
System.out.println("2. Calculate the Area of a Rectangle.");
System.out.println("3. Calculate the Area of a Triangle.");
System.out.println("4. QUIT");
System.out.print("Please make a selection: ");
selection = get.next().charAt(0);
//checking if selection is between 1 and 4
while (selection < 1 || selection > 4)
{
System.out.println("Try again. Please make a selection (1-4)");
selection = get.next().charAt(0);
}
  
  
//output of selection made
switch (selection)
{
case '1': //getting area of circle
System.out.println("Please enter the circle's radius: ");
double r = get.nextDouble();
double areac = 3.14159 * r * r;
System.out.printf("Area of Circle is: " + twoDigits.format(areac));
break;
case'2': //getting area of rectangle
System.out.printf("Please enter the length: ");
double l = get.nextDouble();
System.out.printf("Please enter the width: ");
double w = get.nextDouble();
  
double arear = l * w;
  
System.out.printf("Area of Rectangle is: " + twoDigits.format(arear));
break;
  
  
case '3': //getting area of triangle
System.out.printf("Please enter the base: ");
double b = get.nextDouble();
System.out.printf("Please enter height: ");
double h = get.nextDouble();
  
double areat = b * h * 0.5;
  
System.out.printf("Area of Triangle is: " + twoDigits.format(areat));
break;
  
  
case '4': //no option
System.out.printf("QUIT");
break;
  
}
}   
  
}

trying to run it and ive been editing back and forth. still no compiling what i need.

Solutions

Expert Solution

Below is your working code: -

/*
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
       boolean done = false;
       // 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 Cirlce.");
       System.out.println("2. Calculate the Area of a Rectangle.");
       System.out.println("3. Calculate the Area of a Triangle.");
       System.out.println("4. QUIT");
       boolean error = false;

       // checking if selection is between 1 and 4
       while (!done) {
          
           if(!error) {
               System.out.println("Please make a selection: ");
           }
           selection = get.next().charAt(0);
           // output of selection made
           switch (selection) {
           case '1': // getting area of circle
               error = false;
               System.out.println("Please enter the circle's radius: ");
               double r = get.nextDouble();
               double areac = 3.14159 * r * r;
               System.out.printf("Area of Circle is: "
                       + twoDigits.format(areac) + "\n");
               break;
           case '2': // getting area of rectangle
               error = false;
               System.out.printf("Please enter the length: ");
               double l = get.nextDouble();
               System.out.printf("Please enter the width: ");
               double w = get.nextDouble();

               double arear = l * w;

               System.out.printf("Area of Rectangle is: "
                       + twoDigits.format(arear) + "\n");
               break;

           case '3': // getting area of triangle
               error = false;
               System.out.printf("Please enter the base: ");
               double b = get.nextDouble();
               System.out.printf("Please enter height: ");
               double h = get.nextDouble();

               double areat = b * h * 0.5;

               System.out.printf("Area of Triangle is: "
                       + twoDigits.format(areat) + "\n");
               break;

           case '4': // no option
               System.out.printf("QUIT");
               error = false;
               done = true;
               break;
           default:
               error = true;
               System.out.println("Try again. Please make a selection (1-4)");
               break;
           }
       }
   }

}

Output

Geometry Calculator
Please select from the following menu:
1. Calculate the Area of a Cirlce.
2. Calculate the Area of a Rectangle.
3. Calculate the Area of a Triangle.
4. QUIT
Please make a selection:
7
Try again. Please make a selection (1-4)
-2
Try again. Please make a selection (1-4)
1
Please enter the circle's radius:
2
Area of Circle is: 12.57
Please make a selection:
2
Please enter the length: 2
Please enter the width: 5
Area of Rectangle is: 10.00
Please make a selection:
3
Please enter the base: 5
Please enter height: 2
Area of Triangle is: 5.00
Please make a selection:
4
QUIT


Related Solutions

/* calculating area of a circle, calculating area of a rectangle, calculating area of a triangle,...
/* calculating area of a circle, calculating area of a rectangle, calculating area of a triangle, and quit. */ import java.util.Scanner; public class GeoCalculator {    public static void main(String arg[]) { int geoCalc; //number selection of user Scanner get = new Scanner(System.in); //display section. System.out.println("Geometry Calculator"); System.out.println("Please select from the following menu:"); System.out.println("1. Calculate the Area of a Cirlce."); System.out.println("2. Calculate the Area of a Rectangle."); System.out.println("3. Calculate the Area of a Triangle."); System.out.println("4. QUIT"); System.out.println("Please make a selection:...
Write a java program that contains 3 overloaded static methods for calculating area of a circle,...
Write a java program that contains 3 overloaded static methods for calculating area of a circle, area of a cylinder and volume of a cylinder. Also create an output method which uses JOptionPaneto display instance field(s) and the result of the computing. Then code a driver class which will run and test calling each of these overloaded methods with hard-coded data and display the data and the result of the calculation by calling output method. Thanks!!
(Display a Circle and Its Attributes) Write a program that displays a circle of random size...
(Display a Circle and Its Attributes) Write a program that displays a circle of random size and calculates and displays the area, radius, diameter and circumference. Use the following equations: diameter = 2 × radius, area = π × radius2, circumference = 2 × π × radius. Use the constant Math.PI for pi (π). All drawing should be done on a subclass of JPanel, and the results of the calculations should be displayed in a read-only JTextArea.
Write a program in C that computes the area of a circle (Area = pi *...
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
Using Java, The program you will be writing displays a weekly payroll report. A loop in...
Using Java, The program you will be writing displays a weekly payroll report. A loop in the program should ask the user for the employee’s number, employee’s last name, number of hours worked, hourly pay rate, state tax, and federal tax rate. After the data is entered and the user hits the enter key, the program will calculate gross and net pay then displays employee’s payroll information as follows and asks for the next employees’ information. if the user works...
Write a program that calculates the area and circumference of a circle. It should ask the...
Write a program that calculates the area and circumference of a circle. It should ask the user first to enter the number of circles n, then reads the radius of each circle and stores it in an array. The program then finds the area and the circumference, as in the following equations, and prints them in a tabular format as shown in the sample output. Area = πr2 , Circumference = 2πr, π = 3.14159 Sample Output: Enter the number...
Area of Circle
Luther has a circular garden of radius 1.6 cm . If she plans to put artificial grass on it, How much area does she need to cover?
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The program to present the user with a menu where one of the shapes can be selected. Based on the selection made, the user enters the proper input, the program validates the input (i.e all entries must be greater than zero). Once the input is entered and validated, the intended area is calculated and the entered information along with the area are displayed. Area of...
C++ Write a program that displays the follow menu: Geometry Calculator    1. Calculate the Area...
C++ Write a program that displays the follow menu: Geometry Calculator    1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle    3. Calculate the Area of a Triangle 4. Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle then display it's area using the following formula: area = PIr2 Use 3,14159 for PI and the radius of the circle for r. If the...
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). 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT