Advertisement
CoineTre

JF-LabMethods09.Greater of Two Values

Feb 5th, 2021
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Lab9GreaterOfTwoValues {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String type = scanner.nextLine();
  7.         String firstInput = scanner.nextLine();
  8.         String secondInput = scanner.nextLine();
  9.         switch (type){
  10.             case"int":
  11.                 System.out.println(getMax(Integer.parseInt(firstInput),Integer.parseInt(secondInput)));
  12.                 break;
  13.             case"char":
  14.                 System.out.println(getMax(firstInput.charAt(0),secondInput.charAt(0)));
  15.                 break;
  16.             case"string":
  17.                 System.out.println(getMax(firstInput,secondInput));
  18.                 break;
  19.         }
  20.  
  21.     }
  22.     private static int getMax(int firstInput, int secondInput) {
  23.        return Math.max(firstInput,secondInput);
  24.     }
  25.     private static char getMax(char firstInput,char secondInput){
  26.         if (firstInput > secondInput){
  27.             return firstInput;
  28.         }
  29.         return secondInput;
  30.     }
  31.     private static String getMax( String firstInput,String secondInput){
  32.         if (firstInput.compareTo(secondInput)>0){
  33.             return firstInput;
  34.         }
  35.         return secondInput;
  36.     }
  37. }
  38. /*You are given two values of the same type as input. The values can be of type int, char of String.
  39.  Create a method getMax() that returns the greater of the two values: */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement