plamenavylcheva

Untitled

Apr 26th, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1.  
  2. //You are given two values of the same type as input.
  3. // The values can be of type int, char of String.
  4. // Create a method getMax() that returns the greater of the two values:
  5.  
  6. package Lab;
  7.  
  8. import java.util.Scanner;
  9.  
  10. public class GreaterOfTwoValues {
  11. public static void main(String[] args) {
  12. Scanner scanner = new Scanner(System.in);
  13. String inputType = scanner.nextLine();
  14. if (inputType.equals("int")) {
  15. int first = Integer.parseInt(scanner.nextLine());
  16. int second = Integer.parseInt(scanner.nextLine());
  17. System.out.println(getMax(first, second));
  18. } else if (inputType.equals("char")) {
  19. char first = scanner.next().charAt(0);
  20. char second = scanner.next().charAt(0);
  21. System.out.println(getMax(first, second));
  22. } else if (inputType.equals("string")) {
  23. String first = scanner.nextLine();
  24. String second = scanner.nextLine();
  25. System.out.println(getMax(first, second));
  26. }
  27.  
  28. }
  29.  
  30. static int getMax(int first, int second) {
  31. if (first > second) {
  32. return first;
  33. }
  34. return second;
  35. }
  36.  
  37. static String getMax(String first, String second) {
  38. if (first.compareTo(second)>=0) {
  39. return first;
  40.  
  41. }
  42. return second;
  43. }
  44.  
  45. static char getMax(char first, char second) {
  46. if (first > second) {
  47. return first;
  48. }
  49. return second;
  50. }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment