Question
Im having trouble understanding and making the java code for this. I need to follow this general algorithm. So, the general algorithm for our program
Im having trouble understanding and making the java code for this. I need to follow this general algorithm.
So, the general algorithm for our program is::
1. Find the index of the first space using indexOf() on the user inputted string.
2. Use substring(0, index) to grab the first part of the string, before the space.
3. Find the index of the second space using indexOf(), but with a second argument to tell indexOf() where to start (see slides/text for the parameters indexOf() can accept).
4. Use substring(index+1, index2) to grab the second part of the string, after the first space and before the next space.
5. Use substring(index2+1) to get the last part of the string after the last space.
Lab 3: Strings and If Word Calculator Purpose - Practice using if - Practice using common string methods, like substring, indexOf, and equals. - Practice using printf for formatting. Description of Task In this lab we will be creating a calculator that can add, subtract, multiply, or divide two numbers. It can also raise the first number to the second number, as an exponent. Here is a general outline of how to structure your program. Step 1: Set up the boiler plate code for your lab. You'll need a Scanner to get user input, two ints to represent each number, a String for the operator, and a double for the result. You may also need two additional Strings for each number, to store them before they are converted to an int. Step 2: Get a string command from the user using Scanner's "nextLine()" method. Do not use "next" here. The format of the command should be: num1 SPACE operator SPACE num2 So, for example: five plus two The valid operators are as follows: Step 3: Now, we need to take the line we got from the user and parse out the valuable information: the first number, the operator, then the second number. Use indexOf() to find the location of the two spaces. If indexOf() returns 1, then a space couldn't be found, and therefore the command is invalid. Print an error to the user, and then call System.exit(1) to immediately abort the program. After both spaces are found, use substring() to put each part of the command into a variable for use later. At this point, you should have three Strings of valuable information: one for each number, and one for the operator. Step 4: Do conversions. Convert the operator variable to lower case, to make checking for equality easier. Convert the num1 and num2 strings to an int. The textbook and slides have examples on how to perform these conversions. Step 5: At this point, you should have two ints representing each number, and a String of the operator in lower case. Now, create an if-elseif-else structure to determine what the operator is, and perform the correct operation. Store the result as a double. Then, output the calculation to the user. Output should utilize printf to be well formatted. Output decimal places to 3 places. See example output for details. For division, make sure you cast one of the numbers to a (double) so that you get the correct decimal places. Step 6: Test your program, ensuring that all of the following error conditions are handled: - User enters a command with no spaces - User enters a command with only one space - User enters an invalid operators You can assume that the user will enter a valid integer number. Example Script Output: >>java Calculator Enter a command: 1 plus 3 1 plus 3 equals 4.000 >>java Calculator Enter a command: 1divide3 Error: command does not have a first space! >>ava Calculator Enter a command: 1 divide3 Error: command does not have a second space! >>ava Calculator Enter a command: 1 donkey 5 Error: invalid command! >>ava Calculator Enter a command: 1 divide 234 1 divide 234 equals 0.004Step 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