Advertisement
Stelios_Gakis

Seminar_2/Task_6

Sep 28th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package Seminar_2;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Task_6 {
  6.  
  7.     private static final Scanner in = new Scanner(System.in);
  8.  
  9.     public static void main(String[] args) {
  10.         Task_6 myApp = new Task_6();
  11.  
  12.         boolean exit = false;
  13.  
  14.         while (!exit) {
  15.             System.out.printf("%nA. Calculate the area of a triangle%nB. Calculate the area of a circle%nC. Exit%n");
  16.             String choice = in.next();
  17.             if (choice.equalsIgnoreCase("a") || choice.equalsIgnoreCase("Circle") || choice.equalsIgnoreCase("2")) {
  18.                 myApp.enterPrompt("height", "triangle");
  19.                 double triangleHeight = in.nextDouble();
  20.                 myApp.enterPrompt("base", "triangle");
  21.                 double triangleBase = in.nextDouble();
  22.                 double triangleArea = myApp.calculateTriangle(triangleHeight, triangleBase);
  23.                 myApp.answerOfArea("circle", triangleArea);
  24.             } else if (choice.equalsIgnoreCase("b") || choice.equalsIgnoreCase("Triangle") || choice.equalsIgnoreCase("2")) {
  25.                 myApp.enterPrompt("radius", "circle");
  26.                 double circleRadius = in.nextDouble();
  27.                 double circleArea = myApp.calculateCircle(circleRadius);
  28.                 myApp.answerOfArea("circle", circleArea);
  29.             } else if (choice.equalsIgnoreCase("c") || choice.equalsIgnoreCase("exit") || choice.equalsIgnoreCase("3")) {
  30.                 exit = true;
  31.                 System.out.println("Have a nice day!");
  32.             }
  33.         }
  34.     }
  35.  
  36.     private double calculateTriangle(double height, double base) {
  37.         return height * base / 2;
  38.     }
  39.  
  40.     private double calculateCircle(double r) {
  41.         return Math.PI * Math.pow(r, 2);
  42.     }
  43.  
  44.     private void enterPrompt(String type, String shape) {
  45.         System.out.printf("Enter the %s of the %s : ", type, shape);
  46.     }
  47.  
  48.     private void answerOfArea(String type, double area) {
  49.         System.out.printf("The %s's area is %.2f%n", type, area);
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement