Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

write a method max that has two string parameters and returns the large…

Question

write a method max that has two string parameters and returns the larger (i.e., lexically greater) of the two.

Explanation:

Step1: Define the method

public class StringMax {
    public static String max(String str1, String str2) {

Step2: Compare the strings

        if (str1.compareTo(str2) > 0) {
            return str1;
        } else {
            return str2;
        }
    }
}

Answer:

public class StringMax {
    public static String max(String str1, String str2) {
        if (str1.compareTo(str2) > 0) {
            return str1;
        } else {
            return str2;
        }
    }
}