Question
In the code below, the if statement evaluates the condition x < 10 and assigns the variable color either the value red or blue. The
In the code below, the if statement evaluates the condition x < 10 and assigns the variable color either the value "red" or "blue". The condition is first examined and the corresponding alternative is taken. The strategy in this code is to wait until we know exactly which alternative to take before assigning a color. String color = ""; if (x < 10) { color = "red"; } else { color = "blue"; } Often an alternate strategy (lets call it act first, decide later) can be used to simplify the logic. If the actions in the true and false statements are reversible, we can go ahead and execute the false (or true) statement and then code an if statement that determines whether that action was correct. If the action was incorrect we can reverse it. Basically, assume something is true and use an if statement to correct the assumption. We solve the problem posed above using this alternative strategy: String color = "blue"; if (x < 10) { color = "red"; } We act first by assuming blue is the right color to assign to the variable color. We correct it if that assumption was wrong. The logic is simpler and involves coding one less alternative in the if statement. 1) Write a program that creates three strings variables, str1, str2, and str3. Set them equal to "abcd", "wxyz", and "pqrst", respectively. 2) Compare the three String objects lexicographically (using the compareTo method in the String class) and print the middle-valued string. 3) Alter the program to prompt the user to enter three strings into variables str1, str2, and str3, and repeat the test. For example, if the three strings were "abcd", "wxyz", and "pqrst", the program would print "pqrs". Limit yourself to simple, nested if statements that dont use the Boolean operators && or ||. Be sure and test your code by giving it input data that tests every path through your code. Make a list of values for str1, str2, and str3 that would thoroughly test the code.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started