Question: Implement methods according to given structural and behavioral specifications Write code that uses a stack Compose a larger program out of smaller methods Problem Description
Implement methods according to given structural and behavioral specifications Write code that uses a stack Compose a larger program out of smaller methods Problem Description For this project, you must implement a program that can evaluate simple math expressions like: Note that to keep things simple, the math expressions will always have a single space before and after each operator and parenthesis. You will complete this project in a series of steps. Read and follow these instructions carefully. Part Parsing Step Write a method stub for evaluateExpression Our ultimate goal is a method named evaluateExpression, so let's start by stubbing this method. evaluateExpressionString : double Write a public static method that takes a String argument containing the math expression and returns the value that the expression evaluates to as a double Step Write a body for the evaluateExpression method Our code will need to parse the expression String. This means breaking the expression String down into tokens. Tokens are the individual meaningful units of the expression. For a math expression, the tokens are numbers like and and operators like and So we need to be able to extract from the expression String each token each number and each operator A Scanner is an excellent tool for parsing, because it allows us to extract pieces from a Stream or a String one piece at a time, by using the Scanner methods like next and nextDouble. We can attach a Scanner to a String in the same way that we can attach a Scanner to System.in the keyboard In the evaluateExpression method declare and instantiate a Scanner attached to the argument String. Then write a loop that will use the Scanner to continue to extract the next token from the argument String until there are no more tokens in the String. Note that because we decided that the math expressions will always have a single space before and after each operator, we can simply use the Scanner hasNext method to determine if there is another token in the String and the next method to extract a token. For testing purposes, your evaluateExpression method should print out each token inside a pair of square brackets. For example, when called like this: evaluateExpression; Your evaluateExpression method should print out: In this way, we can verify that our code is able to parse the expression String correctly. Step Write a method named getNextToken that returns the next token Since getting the next token in the expression String is something we will want to do multiple times, let's write a method to do that for us getNextTokenScanner : String Write a public static method that takes a Scanner as an argument and returns a String. When called and passed a Scanner, this method should check to see if there is another token available, and if so then it should extract and return that token. Otherwise, it should return null. Remember that the most powerful feature of a highlevel programming language like Java is the ability to name things. Here we are naming the code that gets the next token, so that any time we want to get the next token, we can simply say getNextTokenmyScanner Never underestimate the power of breaking programs down into simple methods and giving your methods descriptive names that say what they do Now refactor your evaluateExpression method to use the getNextToken method. Then run and test your code to ensure that it still behaves as before. Step Write methods to determine the token type Note that each token in one of our math expressions is either a number or an operator. For each token in the expression, we need to be able to determine its type number or operator so that we can process the token correctly. For this, we will write two methods. isNumberString : boolean Write a public static method that will take a single String argument a token and it will return true if the token is a number, otherwise, it will return false. One of the simplest ways to do this in Java is to try and parse the String token to a double using the Double.parseDouble method. This method will throw a NumberFormatException if the parse fails. We can use try and catch blocks to catch the exception if it is thrown. So if we try to Double.parseDoubletheToken and a NumberFormatException is thrown, we can catch the exception and we can simply return false because the token cannot be a number if it could not be parsed to a double. On the other hand, if no exception is thrown, then the token is a number, because the parse succeeded, and we can return true. isOperatorString : boolean Now write a public static method that will take a single String argument a token and it will return true if the token is an operator and otherwise, it will return false. One of the s
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
