Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //You are given two values of the same type as input.
- // The values can be of type int, char of String.
- // Create a method getMax() that returns the greater of the two values:
- package Lab;
- import java.util.Scanner;
- public class GreaterOfTwoValues {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String inputType = scanner.nextLine();
- if (inputType.equals("int")) {
- int first = Integer.parseInt(scanner.nextLine());
- int second = Integer.parseInt(scanner.nextLine());
- System.out.println(getMax(first, second));
- } else if (inputType.equals("char")) {
- char first = scanner.next().charAt(0);
- char second = scanner.next().charAt(0);
- System.out.println(getMax(first, second));
- } else if (inputType.equals("string")) {
- String first = scanner.nextLine();
- String second = scanner.nextLine();
- System.out.println(getMax(first, second));
- }
- }
- static int getMax(int first, int second) {
- if (first > second) {
- return first;
- }
- return second;
- }
- static String getMax(String first, String second) {
- if (first.compareTo(second)>=0) {
- return first;
- }
- return second;
- }
- static char getMax(char first, char second) {
- if (first > second) {
- return first;
- }
- return second;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment