Read the following code. The purpose is to test if str2 is equal to str3. public class StringEqual { public static void main(String[] args) { String str1 = "abcd"; String str2 = "abcdefg"; String str3 = str1 + "efg"; System.out.println("str2 = " + str2); System.out.println("str3 = " + str3); if (str2 == str3) System.out.println("The strings are equal"); else System.out.println("The strings are not equal"); } } 1. str2 and str3 should be the same. Run the program. If you did not get the expected output, find the logic error and fix it. 2. Add code to str1 and str2. If str1 comes before str2 in dictionary order, print “str1 is less than str2”; if they are same, print “Same”; otherwise, print “str1 is bigger than str2”. ****Use compareTo() method.****
Read the following code. The purpose is to test if str2 is equal to str3.
public class StringEqual
{
public static void main(String[] args)
{
String str1 = "abcd";
String str2 = "abcdefg";
String str3 = str1 + "efg";
System.out.println("str2 = " + str2);
System.out.println("str3 = " + str3);
if (str2 == str3)
System.out.println("The strings are equal");
else
System.out.println("The strings are not equal");
}
}
1. str2 and str3 should be the same. Run the program. If you did not get the expected output, find the logic error and fix it.
2. Add code to str1 and str2. If str1 comes before str2 in dictionary order, print “str1 is less than str2”; if they are same, print “Same”; otherwise, print “str1 is bigger than str2”.
****Use compareTo() method.****
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images