Java !!! Overview This task involves creating a class that will simulate a simple terminal / command interpreter. I.e. the program interprets and executes commands that the user enters into the program. The purpose of the laboratory is to get acquainted with different control flow constructions, how to compare strings with each other, and how strings are read in from the keyboard. Task Create a class whose task is to simulate a simple command interpreter that reads commands from the user and then executes them. Commands from the user should be loaded from the keyboard. The application is intended to function in a similar way as a terminal / command interpreter where we can write commands to e.g. compile javacode (javac) or list files in a directory (dir). When the application is started, a "prompt" must be printed (eg input>) whereupon the user can enter a command. The prompt is just a text that is printed while waiting for the user to enter a command. For example. in the command prompt in Windows, the prompt corresponds to the directory we are currently in. be H: \ java \ lab4> or C: \> (if using Windows). The command that is entered should then be interpreted and executed by your program. If an incorrect command is specified, an error message, as well as the commands that can be interpreted, must be printed. After the command is executed, the prompt should reappear and a new command can be entered by the user. This is repeated until the exit command is entered. The commands that can be interpreted are: • help: prints a help text similar to this • q: exits • quit: same as q • hello: Prints the text HelloWorld! • odd: determines whether a number is odd or not • repeat: prints any text x number of times Calc: performs a simple mathematical calculation • calculate: same as calc You must write a class named CommandPrompt which contains a class constant (public static final) and whose value is "input>". This is the prompt to be printed before each new command entry. Each command must be handled in its own private method in the class. See design suggestions on the right of the class diagram (more members need to be added) When an object of CommandPrompt is created, the start method should be called which then prints the prompt and waits for the user to enter a command. When a command is entered, the method should examine which it is and call the corresponding private method which in turn executes the command itself (see requirements below for what should happen for each command). When the command is executed, the prompt should be reprinted and a new command should be able to be entered by the user. This continues until the user enters a command to exit. Requirement • help - should print a help text of which commands can be interpreted and what each command does. • q or quit - should end the program. • hello - will print the text HelloWorld! on the screen. Odd - should ask the user for an integer and check if the number is odd or even. A printout that clearly shows whether the number is even or odd should be made. • repeat - should ask the user for an integer and a text. Then this text should be printed as many times as the integer requested. Calc or calculate - should ask the user for two numbers and a calculation method (+, -, / or *). Check what the calculation method is and then calculate the result and show it to the user. • All commands must be executed in their own methods, i.e. there should be a method that handles the command hello, one for odd, one for repeat, etc. • All screen prints should be well-structured and easy to understand. Tip • Use some form of loop in the start () method to load the command and interpret it. • To convert a string to e.g. a double, you must use the Double wrapper class. Example: String piAsStrings = "3.14"; double pi = Double.parseDouble (piAsString); • To compare one string with another string, call the equals () method on one of the string objects. Example: String s = "hello"; if (s.equals ("hello")) {// The strings are equal} else {// The strings are not equal} It is wrong to compare strings using ==. • To end the execution somewhere in the code, use System.exit (0); • Do not forget to import the correct class to be able to read from the keyboard. Declare the object, which reads from the keyboard, as an instance variable in the class so that the object can be used throughout the class. Examples of driving: in the attached pic
Java !!!
Overview
This task involves creating a class that will simulate a simple terminal / command interpreter. I.e. the program interprets and executes commands that the user enters into the program. The purpose of the laboratory is to get acquainted with different control flow constructions, how to compare strings with each other, and how strings are read in from the keyboard.
Task
Create a class whose task is to simulate a simple command interpreter that reads commands from the user and then executes them. Commands from the user should be loaded from the keyboard. The application is intended to function in a similar way as a terminal / command interpreter where we can write commands to e.g. compile javacode (javac) or list files in a directory (dir).
When the application is started, a "prompt" must be printed (eg input>) whereupon the user can enter a command. The prompt is just a text that is printed while waiting for the user to enter a command. For example. in the command prompt in Windows, the prompt corresponds to the directory we are currently in. be H: \ java \ lab4> or C: \> (if using Windows).
The command that is entered should then be interpreted and executed by your program. If an incorrect command is specified, an error message, as well as the commands that can be interpreted, must be printed. After the command is executed, the prompt should reappear and a new command can be entered by the user. This is repeated until the exit command is entered.
The commands that can be interpreted are:
• help: prints a help text similar to this
• q: exits
• quit: same as q
• hello: Prints the text HelloWorld!
• odd: determines whether a number is odd or not
• repeat: prints any text x number of times
Calc: performs a simple mathematical calculation
• calculate: same as calc
You must write a class named CommandPrompt which contains a class constant (public static final) and whose value is "input>". This is the prompt to be printed before each new command entry. Each command must be handled in its own private method in the class. See design suggestions on the right of the class diagram (more members need to be added)
When an object of CommandPrompt is created, the start method should be called which then prints the prompt and waits for the user to enter a command.
When a command is entered, the method should examine which it is and call the corresponding private method which in turn executes the command itself (see requirements below for what should happen for each command). When the command is executed, the prompt should be reprinted and a new command should be able to be entered by the user. This continues until the user enters a command to exit.
Requirement
• help - should print a help text of which commands can be interpreted and what each command does.
• q or quit - should end the program.
• hello - will print the text HelloWorld! on the screen.
Odd - should ask the user for an integer and check if the number is odd or even. A printout that clearly shows whether the number is even or odd should be made.
• repeat - should ask the user for an integer and a text. Then this text should be printed as many times as the integer requested.
Calc or calculate - should ask the user for two numbers and a calculation method (+, -, / or *). Check what the calculation method is and then calculate the result and show it to the user.
• All commands must be executed in their own methods, i.e. there should be a method that handles the command hello, one for odd, one for repeat, etc.
• All screen prints should be well-structured and easy to understand.
Tip
• Use some form of loop in the start () method to load the command and interpret it.
• To convert a string to e.g. a double, you must use the Double wrapper class. Example: String piAsStrings = "3.14"; double pi = Double.parseDouble (piAsString);
• To compare one string with another string, call the equals () method on one of the string objects. Example: String s = "hello"; if (s.equals ("hello")) {// The strings are equal} else {// The strings are not equal} It is wrong to compare strings using ==.
• To end the execution somewhere in the code, use System.exit (0);
• Do not forget to import the correct class to be able to read from the keyboard.
Declare the object, which reads from the keyboard, as an instance variable in the class so that the object can be used throughout the class.
Examples of driving: in the attached pic
Step by step
Solved in 4 steps with 4 images