Advertisement
Stelios_Gakis

Exercise_4/Task_2_3

Sep 25th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Scanner;
  3.  
  4. public class Task_2_3 {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         DecimalFormat df = new DecimalFormat("0.#####");
  9.         Scanner input = new Scanner(System.in);
  10.         Task_2_3 myApp = new Task_2_3();
  11.  
  12.         boolean exit = false;
  13.         System.out.println("--- Welcome to the conversion center ---");
  14.         do {
  15.             System.out.printf("%n1) Kilograms to pounds%n2) Pounds to kilograms%n3) Meters to feet%n4) Feet to meters%n5) Exit%n%n");
  16.             System.out.println("Please enter your choice");
  17.             int choice = input.nextInt();
  18.             if (choice == 1) {
  19.                 System.out.println("Please enter your number in Kilograms");
  20.                 double K = input.nextInt();
  21.                 double res = myApp.KtoP(K);
  22.                 System.out.printf("Your converted number is: ");
  23.                 System.out.print(df.format(res));
  24.                 System.out.printf(" Pounds");
  25.                 try {
  26.                     Thread.sleep(2500);
  27.                 } catch (InterruptedException e) {
  28.                     e.printStackTrace();
  29.                 }
  30.             } else if (choice == 2) {
  31.                 System.out.println("Please enter your number in Pounds");
  32.                 double P = input.nextDouble();
  33.                 double res = myApp.PtoK(P);
  34.                 System.out.printf("Your converted number is: ");
  35.                 System.out.print(df.format(res));
  36.                 System.out.printf(" Kilograms");
  37.                 try {
  38.                     Thread.sleep(2500);
  39.                 } catch (InterruptedException e) {
  40.                     e.printStackTrace();
  41.                 }
  42.             } else if (choice == 3) {
  43.                 System.out.println("Please enter your number in Meters");
  44.                 double M = input.nextDouble();
  45.                 double res = myApp.MtoF(M);
  46.                 System.out.printf("Your converted number is: ");
  47.                 System.out.print(df.format(res));
  48.                 System.out.printf(" Feet");
  49.                 try {
  50.                     Thread.sleep(2500);
  51.                 } catch (InterruptedException e) {
  52.                     e.printStackTrace();
  53.                 }
  54.             } else if (choice == 4) {
  55.                 System.out.println("Please enter your number in Feet");
  56.                 double F = input.nextDouble();
  57.                 double res = myApp.FtoM(F);
  58.                 System.out.printf("Your converted number is: ");
  59.                 System.out.print(df.format(res));
  60.                 System.out.printf(" Meters");
  61.                 try {
  62.                     Thread.sleep(2500);
  63.                 } catch (InterruptedException e) {
  64.                     e.printStackTrace();
  65.                 }
  66.             } else if (choice == 5) {
  67.                 exit = true;
  68.                 System.out.println("Goodbye!");
  69.             } else {
  70.                 System.out.println("Error! Wrong input! Please enter a correct option");
  71.             }
  72.  
  73.  
  74.         } while (!exit);
  75.  
  76.     }
  77.  
  78.     private double MtoF(double a) {
  79.         return a * 3.28084;
  80.     }
  81.  
  82.     private double FtoM(double a) {
  83.         return a * 0.3048;
  84.     }
  85.  
  86.     private double KtoP(double a) {
  87.         return a * 2.20462;
  88.     }
  89.  
  90.     private double PtoK(double a) {
  91.         return a * 0.453592;
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement