Advertisement
SotirovG

Methods_GreaterOfTwoValues_09

Feb 7th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. package JavaFundamentals.MethodsLab;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class GreaterOfTwoValues_09 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String type = scanner.nextLine();
  10.  
  11.         switch (type) {
  12.             case "int":
  13.                 int number1 = Integer.parseInt(scanner.nextLine());
  14.                 int number2 = Integer.parseInt(scanner.nextLine());
  15.                 int maxInt = getMaxInt(number1, number2);
  16.                 System.out.println(maxInt);
  17.                 break;
  18.  
  19.             case "char":
  20.                 char symbol1 = scanner.nextLine().charAt(0);
  21.                 char symbol2 = scanner.nextLine().charAt(0);
  22.                 char maxChar = getMaxChar(symbol1, symbol2);
  23.                 System.out.println(maxChar);
  24.                 break;
  25.  
  26.             case "String":
  27.                 String wordA = scanner.nextLine();
  28.                 String wordB = scanner.nextLine();
  29.                 String maxWord = getMaxString(wordA, wordB);
  30.                 System.out.println(maxWord);
  31.  
  32.                 break;
  33.  
  34.         }
  35.  
  36.  
  37.     }
  38.  
  39.  
  40.     private static int getMaxInt(int number1, int number2) {
  41.         int biggerNumber = Math.max(number1, number2);
  42.         return biggerNumber;
  43.     }
  44.  
  45.     private static char getMaxChar(char symbol1, char symbol2) {
  46.         return (char) Math.max(symbol1, symbol2);
  47.     }
  48.  
  49.     private static String getMaxString(String wordA, String wordB) {
  50.         if (wordA.compareTo(wordB) >= 0) {
  51.             return wordA;
  52.         } else {
  53.             return wordB;
  54.         }
  55.  
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement