Advertisement
eranseg

Workbook Exercises

Aug 31st, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.71 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class JavaExercises {
  4.     public static void main(String[] args) {
  5.  
  6.         ///////////////////// Section 1 - Building Blocks /////////////////////
  7.         //----------------Ex1------------------
  8.         /*
  9.         Scanner sc = new Scanner(System.in);
  10.         int radius = 0;
  11.         System.out.println("Please enter a circle radius: ");
  12.         radius = sc.nextInt();
  13.         if (radius < 0) {
  14.             radius = 0;
  15.             System.out.println("Radius cannot be negative! Bad Input!");
  16.         } else {
  17.             System.out.printf("A circle with radius of: %dm has a perimeter of: %fm and an area of: %fm^2"
  18.                               , radius, Math.PI*radius*radius, 2*radius*Math.PI);
  19.         }
  20.         */
  21.  
  22.         //----------------Ex2------------------
  23.         /*
  24.         Scanner sc = new Scanner(System.in);
  25.         final int TOAST = 12, CHEAP_EXTRAS = 2, EXP_EXTRAS = 3;
  26.         int toastTotalPrice = TOAST;
  27.         char c = ' ';
  28.         while(true) {
  29.             System.out.println("Please enter the desired extras on the toast. M - Mushrooms, O - Olives, C - Corn, " +
  30.                                "B - Bulgarian cheese, E - Extra cheese, X - Go to checkout");
  31.             c = sc.next().charAt(0);
  32.             if(c == 'X' || c == 'x') {
  33.                 break;
  34.             }
  35.             switch (c) {
  36.                 case 'M' :
  37.                 case 'm' :
  38.                 case 'O' :
  39.                 case 'o' :
  40.                 case 'C' :
  41.                 case 'c' :
  42.                     toastTotalPrice += CHEAP_EXTRAS;
  43.                     break;
  44.                 case 'B' :
  45.                 case 'b' :
  46.                 case 'E' :
  47.                 case 'e' :
  48.                     toastTotalPrice += EXP_EXTRAS;
  49.                     break;
  50.                 default:
  51.                     System.out.println("Invalid Input! Try again");
  52.             }
  53.         }
  54.         System.out.printf("Total toast price is: %d", toastTotalPrice);
  55.         */
  56.  
  57.         //----------------Ex2------------------
  58.         /*
  59.         Scanner sc = new Scanner(System.in);
  60.         final int PRICE_FOR_ONE_KM_TRANS = 5;
  61.         final int FLOOR_PRICE_FOR_EACH_KG = 1;
  62.         int initPrice = 0;
  63.         int weight = 0;
  64.         int floor = 0;
  65.         int dist = 0;
  66.         System.out.println("Please enter the item's price, its weight, what floor you leave in and the distance from the store: ");
  67.         initPrice = sc.nextInt();
  68.         weight = sc.nextInt();
  69.         floor = sc.nextInt();
  70.         dist = sc.nextInt();
  71.         sc.nextLine();
  72.         sc.close();
  73.         System.out.printf("Total transport price, the item's price included, is: %f",
  74.                            initPrice + weight*floor*FLOOR_PRICE_FOR_EACH_KG + PRICE_FOR_ONE_KM_TRANS*dist + initPrice*0.1);
  75.         */
  76.  
  77.         ///////////////////// Section 2 - Language //////////////////
  78.         //----------------Ex1------------------
  79.         /*
  80.         Scanner sc = new Scanner(System.in);
  81.         int age;
  82.         System.out.println("Please enter your age: ");
  83.         age = sc.nextInt();
  84.         if(age < 18) {
  85.             System.out.println("You are not adult!");
  86.         } else {
  87.             System.out.println("You are an adult!");
  88.         }
  89.         sc.close();
  90.         */
  91.  
  92.         //----------------Ex2------------------
  93.         /*
  94.         Scanner sc = new Scanner(System.in);
  95.         int age;
  96.         System.out.println("Please enter your age: ");
  97.         age = sc.nextInt();
  98.         if(age < 18) {
  99.             System.out.println("You are not adult!");
  100.         } else if (age < 66){
  101.             System.out.println("You are an adult!");
  102.         } else {
  103.             System.out.println("You are on pension!");
  104.         }
  105.         sc.close();
  106.         */
  107.  
  108.         //----------------Ex3------------------
  109.         /*
  110.         Scanner sc = new Scanner(System.in);
  111.         int number;
  112.         System.out.println("Please enter a 2 digit number: ");
  113.         number = sc.nextInt();
  114.         sc.close();
  115.         if(number < 10 || number > 99) {
  116.             System.out.println("Bad Input!");
  117.         } else {
  118.             if(number/10 == number%10) {
  119.                 System.out.println("Digits are identical!");
  120.             } else {
  121.                 System.out.println("Digits are not the same!");
  122.             }
  123.         }
  124.         */
  125.  
  126.         //----------------Ex4------------------
  127.         /*
  128.         Scanner sc = new Scanner(System.in);
  129.         int number;
  130.         System.out.println("Please enter a 3 digit number: ");
  131.         number = sc.nextInt();
  132.         sc.close();
  133.         if(number < 100 || number > 999) {
  134.             System.out.println("Bad Input!");
  135.         } else {
  136.             if((number/10 == number%100) && (number/100 == number%10)) {
  137.                 System.out.println("Digits are identical!");
  138.             } else {
  139.                 System.out.println("Digits are not the same!");
  140.             }
  141.         }
  142.         */
  143.  
  144.         //----------------Ex5------------------
  145.         /*
  146.         Scanner sc = new Scanner(System.in);
  147.         int number;
  148.         System.out.println("Please enter a 2 digit number: ");
  149.         number = sc.nextInt();
  150.         sc.close();
  151.         if(number < 10 || number > 99) {
  152.             System.out.println("Bad Input!");
  153.         } else {
  154.             if(number < 90 && number / 10 == number % 10 - 1) {
  155.                 System.out.println("Digits are consecutive!");
  156.             } else {
  157.                 System.out.println("Digits are not consecutive!");
  158.             }
  159.         }
  160.         */
  161.  
  162.         //----------------Ex6------------------
  163.         /*
  164.         Scanner sc = new Scanner(System.in);
  165.         int number;
  166.         System.out.println("Please enter a 3 digit number: ");
  167.         number = sc.nextInt();
  168.         sc.close();
  169.         if(number < 100 || number > 999) {
  170.             System.out.println("Bad Input!");
  171.         } else {
  172.             if(number < 900 && number / 100 == (number % 100 / 10) - 1 && (number % 100 / 10) == number % 10 - 1) {
  173.                 System.out.println("Digits are consecutive!");
  174.             } else {
  175.                 System.out.println("Digits are not consecutive!");
  176.             }
  177.         }
  178.         */
  179.  
  180.         //----------------Ex7------------------
  181.         //
  182.         Scanner sc = new Scanner(System.in);
  183.         int maxFuel, currentFuel;
  184.         System.out.println("Please enter size of fuel tank and current fuel in the car: ");
  185.         maxFuel = sc.nextInt();
  186.         currentFuel = sc.nextInt();
  187.         sc.close();
  188.         if(currentFuel < maxFuel * 0.15) {
  189.             System.out.println("Put some gaz on your vehicle!");
  190.         } else {
  191.             System.out.println("Your'e doing just fine!");
  192.         }
  193.         //
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement