Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Seminar_2;
- import java.util.Scanner;
- public class Task_6 {
- private static final Scanner in = new Scanner(System.in);
- public static void main(String[] args) {
- Task_6 myApp = new Task_6();
- boolean exit = false;
- while (!exit) {
- System.out.printf("%nA. Calculate the area of a triangle%nB. Calculate the area of a circle%nC. Exit%n");
- String choice = in.next();
- if (choice.equalsIgnoreCase("a") || choice.equalsIgnoreCase("Circle") || choice.equalsIgnoreCase("2")) {
- myApp.enterPrompt("height", "triangle");
- double triangleHeight = in.nextDouble();
- myApp.enterPrompt("base", "triangle");
- double triangleBase = in.nextDouble();
- double triangleArea = myApp.calculateTriangle(triangleHeight, triangleBase);
- myApp.answerOfArea("circle", triangleArea);
- } else if (choice.equalsIgnoreCase("b") || choice.equalsIgnoreCase("Triangle") || choice.equalsIgnoreCase("2")) {
- myApp.enterPrompt("radius", "circle");
- double circleRadius = in.nextDouble();
- double circleArea = myApp.calculateCircle(circleRadius);
- myApp.answerOfArea("circle", circleArea);
- } else if (choice.equalsIgnoreCase("c") || choice.equalsIgnoreCase("exit") || choice.equalsIgnoreCase("3")) {
- exit = true;
- System.out.println("Have a nice day!");
- }
- }
- }
- private double calculateTriangle(double height, double base) {
- return height * base / 2;
- }
- private double calculateCircle(double r) {
- return Math.PI * Math.pow(r, 2);
- }
- private void enterPrompt(String type, String shape) {
- System.out.printf("Enter the %s of the %s : ", type, shape);
- }
- private void answerOfArea(String type, double area) {
- System.out.printf("The %s's area is %.2f%n", type, area);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement