TheBulgarianWolf

getMax Overloading

Apr 2nd, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. package softuni;
  2.  
  3. import java.text.DecimalFormat;
  4. import java.util.Scanner;
  5.  
  6.  
  7.  
  8. public class SoftUni {
  9.  
  10.     static int getMax(int num1,int num2){
  11.         if(num1 > num2){
  12.             return num1;
  13.         }
  14.         else if(num1 < num2){
  15.             return num2;
  16.         }
  17.         else{
  18.             return num1;
  19.         }
  20.     }
  21.    
  22.     static char getMax(char one,char two){
  23.         if(one > two){
  24.             return one;
  25.         }
  26.         else if(two > one){
  27.             return two;
  28.         }
  29.         else{
  30.             return two;
  31.         }
  32.     }
  33.    
  34.     static String getMax(String first,String second){
  35.         if(first.compareTo(second) >= 0){
  36.             return first;
  37.         }
  38.         else{
  39.             return second;
  40.         }
  41.        
  42.     }
  43.    
  44.     public static void main(String[] args){
  45.         Scanner sc = new Scanner(System.in);
  46.         System.out.print("Enter the value type: ");
  47.         String type = sc.nextLine();
  48.         if(type.equals("int")){
  49.             System.out.print("Enter the first number: ");
  50.             int first = Integer.parseInt(sc.nextLine());
  51.             System.out.print("Enter the second number: ");
  52.             int second = Integer.parseInt(sc.nextLine());
  53.             int result = getMax(first,second);
  54.            
  55.             System.out.println("The bigger is: " + result);
  56.         }
  57.         else if(type.equals("char")){
  58.             System.out.print("Enter the first character: ");
  59.             char first = sc.next().charAt(0);
  60.             System.out.print("Enter the secodn character: ");
  61.             char second = sc.next().charAt(0);
  62.             char result = getMax(first,second);
  63.             System.out.println("The bigger is: " + result);
  64.         }
  65.         else if(type.equals("string")){
  66.             System.out.print("Enter the first word: ");
  67.             String first = sc.nextLine();
  68.             System.out.print("Enter the second word: ");
  69.             String second = sc.nextLine();
  70.             String result = getMax(first,second);
  71.             System.out.println("The bigger is: " + result);
  72.         }
  73.         else{
  74.             System.out.println("Wrong type!");
  75.         }
  76.     }
  77.    
  78. }
Add Comment
Please, Sign In to add comment