6) Copy and run the following program. Explain how the program compares the two strings. How can you modify the program so that str2 and str3 are equal when they are compared?
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");
System.out.println("The strings are not equal");
7) Here is a table that describes the planets in our solar system numbered according to their distance from the sun:
Use the code below to input an integer from the user. Write a switch statement that examines the integer and prints out the name of the corresponding planet. Print an error message if an invalid integer is entered.
System.out.print("Enter a planet number from the sun: ");
int planet = scan.nextInt();
// Your code goes here …
8) Remember the childhood game “Rock, Paper, Scissors”? It is a two-player game in which each person simultaneously chooses either rock, paper, or scissors. Rock beats scissors but loses to paper, paper beats rock but loses to scissors, and scissors beats paper but loses to rock. The following code prompts player 1 and player 2 to each enter a string: rock, paper, or scissors. Finish the code by adding nested if statements to appropriately report “Player 1 wins”, “Player 2 wins”, or “It is a tie.”
System.out.println("Player 1: Choose rock, scissors, or paper:");
String player1 = scan.next().toLowerCase();
System.out.println("Player 2: Choose rock, scissors, or paper:");
String player2 = scan.next().toLowerCase();
(your code goes here…)
9) The program in Lab 5.8 was heavily nested with if statements, and it can be difficult to follow the logic of any program that is heavily nested in this way. By constructing complex conditions with the && operator, it is possible to simplify the code and remove some of the else alternatives. Rewrite the program in Lab 5.8 using complex conditions and omitting the else construct.