SHARE
TWEET

Lesson001_21_07_19_HomeWork

RexyBadDog Jul 21st, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class Lesson001_21_07_19_HomeWork {
  4.     private static Scanner sc = new Scanner(System.in);
  5.     public static final int CURRENT_YEAR = 2019;
  6.     public static final double START_PRICE = 10.90;
  7.     public static final double KM_PRICE = 1.30;
  8.     public static final double CASE_PRICE = 2.00;
  9.     public static final int STOP_DELAY = 5;
  10.     public static final int MOVE_DELAY = 3;
  11.  
  12.     public static void main(String[] args) {
  13.         int option = homeWorkMenu();
  14.         while (option != 0) {       // while the user didn't put '0', keep pop-up the menu!
  15.             switch (option) {
  16.                 case 1:
  17.                     option = task1();
  18.                     break;
  19.                 case 2:
  20.                     option = task2();
  21.                     break;
  22.                 case 3:
  23.                     option = task3();
  24.                     break;
  25.                 case 4:
  26.                     option = task4();
  27.                     break;
  28.                 case 5:
  29.                     option = task5();
  30.                     break;
  31.                 default:
  32.                     System.out.println(option + " is invalid option! try again...");
  33.             }
  34.         }
  35.     }
  36.  
  37.     public static int homeWorkMenu() {
  38.         System.out.print("...::: MENU :::...\n" +       // A menu for homework tasks
  39.                 "press [1] for task 1: Different input types.\n" +
  40.                 "press [2] for task 2: Work with chars and integers.\n" +
  41.                 "press [3] for task 3: ASCII table\n" +
  42.                 "press [4] for task 4: Taxi drive price.\n" +
  43.                 "press [5] for task 5: Elevator timing.\n" +
  44.                 "press [0] to end this program.\n" +
  45.                 "your option is: ");
  46.         int option = sc.nextInt();
  47.         System.out.println("\n");
  48.         return option;
  49.     }
  50.  
  51.     public static int task1() {
  52.         System.out.println("Task 1 - Different input types");
  53.         int age, id;
  54.         double height;
  55.         char MorF;
  56.         boolean boolMorF;
  57.         System.out.print("Enter year of birth: ");
  58.         age = CURRENT_YEAR - sc.nextInt();
  59.         System.out.print("Enter id: ");
  60.         id = sc.nextInt();
  61.         System.out.print("Enter height: ");
  62.         height = sc.nextInt();
  63.         System.out.print("Enter [M] for male OR [F] for female: ");
  64.         MorF = sc.next().charAt(0);
  65.         System.out.print("Enter 'true' for male OR 'false' for female: ");
  66.         boolMorF = sc.nextBoolean();
  67.         System.out.println("1.1 The age is: " + age + ", the ID is: " + id + " the height is: " + height +
  68.                 "and the sex is: " + MorF);
  69.         System.out.println("1.2 The age is: " + age + ", the ID is: " + id + " the height is: " + height +
  70.                 "and she/he is a Male sex: " + boolMorF + ".\n");
  71.         int opt = 0;
  72.         return opt = homeWorkMenu();
  73.     }
  74.  
  75.     public static int task2() {
  76.         System.out.println("Task 2 - Work with chars and integers");
  77.         char ch1, ch2, ch3;
  78.         int num;
  79.         System.out.print("Enter 3 number characters: ");
  80.         ch1 = sc.next().charAt(0);
  81.         ch2 = sc.next().charAt(0);
  82.         ch3 = sc.next().charAt(0);
  83.         num = (ch1 - 48) * 100 + (ch2 - 48) * 10 + (ch3 - 48);
  84.         System.out.println("The integer created is: " + num + ".\n");
  85.         int opt = 0;
  86.         return opt = homeWorkMenu();
  87.     }
  88.  
  89.     public static int task3() {
  90.         System.out.println("Task 3 - ASCII table");
  91.         char ch1 = 'a';
  92.         char ch2 = 'z';
  93.         int num = ch2 - ch1;
  94.         System.out.println("The number of letters in the alphabet is: " + num + ".\n");
  95.         int opt = 0;
  96.         return opt = homeWorkMenu();
  97.     }
  98.  
  99.     public static int task4() {
  100.         System.out.println("Task 4 - Taxi drive price");
  101.         System.out.print("Please enter a number of kilo-meters to drive: ");
  102.         double km = sc.nextDouble();
  103.         System.out.print("Please enter a number of suit cases: ");
  104.         int cases = sc.nextInt();
  105.         double bill = START_PRICE + (KM_PRICE * km) + (CASE_PRICE * cases);
  106.         System.out.println("Please pay: " + bill + "$.\n");
  107.         int opt = 0;
  108.         return opt = homeWorkMenu();
  109.     }
  110.  
  111.     public static int task5() {
  112.         System.out.println("Task 5 - Elevator timing");
  113.         System.out.print("Please enter ELEVATOR floor number: ");
  114.         int elevator = sc.nextInt();
  115.         System.out.print("Please enter YOUR floor number: ");
  116.         int myFloor = sc.nextInt();
  117.         System.out.print("Please enter DESTINATION floor number: ");
  118.         int dest = sc.nextInt();
  119.         int time = Math.abs(myFloor - elevator) * MOVE_DELAY + STOP_DELAY + Math.abs(myFloor - dest) * MOVE_DELAY;
  120.         System.out.println("It will take " + time + " seconds to get to " + dest + " floor.\n");
  121.         int opt = 0;
  122.         return opt = homeWorkMenu();
  123.     }
  124. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top