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.
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...
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...
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...
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,...
Design a program that displays the following in Java: Enter the grades for an Essay: Grammer...
Design a program that displays the following in Java: Enter the grades for an Essay: Grammer (must be 30 or less): 40 Grammer (must be 30 or less): 26.3 Spelling (must be 20 or less): 17.4 Correct Length (must be 20 or less): 19 Content (must be 30 or less): 23.5 The recorded scores are: Grammer: 26.3 Spelling: 17.4 Correct Length: 19.0 Content: 23.5 Total score: 86.2 The grade for this essay is B
Write a full program that solves the following equation and displays the value for x and...
Write a full program that solves the following equation and displays the value for x and y: 3.4x+50.2y=44.5 2.1x+.55y=5.9
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT