Question
I am supposed to reduce redundancy in the code and also make unknown inputs, output unknown. Step 1: Your development manager likes your program but
I am supposed to reduce redundancy in the code and also make unknown inputs, output "unknown".
Step 1: Your development manager likes your program but is concerned about code duplication. Roger left a note on your desk, asking about using a feature for automatically converting strings to enumerations. Change your ToCommand method to employ this JAVA library method. (Hint: Look for a valueOf method).
After converting your ToCommand method, run the program and enter an invalid command (one that does not map to a value in the Commands enumeration). What happens?
Step 2: In the previous step, you learned that the built-in JAVA feature, for converting strings to enumerations, throws an exception if there is no matching value. Thus, while employing the valueOf method dramatically reduces the code required to convert strings to enumerations and allows new enumeration values to be added without changing the ToCommand method, it also poses a problem. Your game cant crash every time the player enters an invalid command or makes a typo!! In this video, Roger shows you how one of the play testers is concerned that the game is crashing when she enters a command.
Figure out a way to handle these exceptions. If the user enters a command that the game does not recognize, have the game display the output Unknown.
CURRENT CODE:
public class Main { public static void main(String[] args) { System.out.print("Welcome to Zork! > "); Scanner scanner = new Scanner(System.in); String inputString = scanner.nextLine(); if ("QUIT".equalsIgnoreCase(inputString)) { // Output string initiated if the user inputs "quit" System.out.println("Thank you for playing!"); } else if ("LOOK".equalsIgnoreCase(inputString)) { // Output string initiated if the user inputs "look" System.out.println("A rubber mat saying " + "Welcome to Zork! lies by the door."); } } }
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