The tokens should be placed in an array of strings

Assignment Help JAVA Programming
Reference no: EM13889568

In this lab you will write a program to read in and evaluate apostfix arithmetic expressionusing a stack, as outlined in class.

Step 1. Create a public class Postfix1 inside Postfix1.java and add a main method. Now write a static method to read in a line of text that is purportedly a postfix arithmetic expression from the console (without prompting), and split it into tokens eliminating white space. The tokens should be placed in an array of Strings. At this stage do not verify that this is a genuine expression. Your method is in effect just reading in a line of tokens and placing them in an array no matter what they are. Your method should have the following signature,

String[] readExpr()

and should return an array whose length is the number of tokens on the line. Now write a method to write the selfsame expression out on one line by itself given the array, with one space between each token. The signature of this method should be as follows

void writeExpr(String[] expr)

Test your methods together to be sure they work.

Now write a method to determine if a string is one of the operators + - * permitted in expressions in this lab. Your method should have the following signature.

boolean isOperator(String s)

Now write a method to check the syntax of a postfix expression represented as an array of Strings. Your method should have the following signature.

boolean checkSyntax(String[] expr)

and should silently return true if all array entries are either valid operators (tested with the isOperator method) or can be converted to doubles. Otherwise an error message of the form exemplified as follows should be output and false returned. Use writeExpr to output the expression in such a message.

1.0 2.0 + 3.2 9.1 / 15 *                  ^

Not a number or valid operator.

The caret should indicate the first character of the offending token. Write a main method that reads in an expression and runs checkSyntax on it, and if the syntax is correct prints out the expression using writeExpr followed by a second line of "Syntax correct".

Step 2.Copy Postfix1.java to Postfix2.java, renaming the public class suitably. Write a (not public) class DoubleStack in Postfix2.java to represent a stack of numbers of type double. You may assume that not more that 1000 numbers will be placed upon a DoubleStack. Your stack should be implemented as discussed at length in class, as an integer indicating how many numbers are currently on the stack, along with an array the initial part of which contains the numbers on the stack. Your stack should implement the usual methods for a stack with the following signatures.

DoubleStack() //constructor for a new empty stack

boolean empty() //whether the stack is empty

double pop() //remove and return the top element; throw an error if empty

void push(double x) //add x to the top of the stack

Now modify the main method in Postfix2.java to read a postfix expression from the console on one line, and if its syntax is incorrect act as in step 1. Otherwise either print out its value on a line by itself, or one of the error messages below as appropriate. Note that the message "Syntax correct" is not wanted in this step. Your new main method should use a DoubleStack along with the machinery you created in step 1 to do this.

Specifically, the expression should be evaluated as discussed in class, by iterating through the sequence of numbers and operators; when a number is encountered push it onto the stack and when an operator is encountered, first pop its right operand off the stack, then pop its left operand off the stack, combine these two with the operator, and push the result back on the stack. The result should be a single number on the stack.

If an operation is attempted and there are not at least two numbers on the stack to combine then print out a message in the following form.

Too few operands for +

1.0 2.0 * + 4.3 +

The caret should indicate the position of the offending operator inside the entire expression. If the stack contains more than one number when expression evaluation is complete then print out the following message.

Too few operators to produce a single result.

Reference no: EM13889568

Questions Cloud

The types and role of distributed software architecture : the types and role of distributed software architecture.
What is the problem for the characters in the book : Plot:Retell the main events in the book. Problem in the book -Conflict (What is the problem for the characters in the book?) Be specific. Resolution-Solutiontotheproblem(Howistheconflictsolved)
Can a person who has a criminal conviction : Can a person who has a criminal conviction open a business as a sole trader?
How do sufis use music to onnect with god : Following the viewing of the film: Sufi Soul -- how do Sufis use music to onnect with God? How is this different from the reading of Quranic Scripture or following the 5 pillars of Islam
The tokens should be placed in an array of strings : Create a public class Postfix1 inside Postfix1.java and add a main method. Now write a static method to read in a line of text that is purportedly a postfix arithmetic expression from the console (without prompting), and split it into tokens eliminat..
Explain the importance of the critical path in project : Explain the importance of the critical path in project management
Case study on ethics in business : Who are the stakeholders in this case? What expectations should they have? And, what are the ethical obligations of ACAS and its employees to the stakeholders?
Why was the storm that hit galveston : Why was the storm that hit Galveston on 8 September 1900 so devastating? Explain in full and support your response with examples from "Isaac's Storm: A Man, a Time, and the Deadliest Hurricane in History"
Law to the facts identified : WaterJet Ferries is a successful ferry operator providing roll-on/roll-off car and truck ferry services to the larger islands off the coast using vessels that are propelled by water-jets powered by diesel engines. Over the last twelve months the comp..

Reviews

Write a Review

JAVA Programming Questions & Answers

  Mining information from sales data is a common task in both

mining information from sales data is a common task in both traditional and online stores. past sales information can

  Write a java program that finds the temperature

Write a JAVA program that finds the temperature that is the same in both Celsius and Fahrenheit.

  The williamsburg women''s club

The Williamsburg Women's Club offers scholarships to local high school students who meet any of several criteria. Write an application that prompts the user for a student's numeric high school grade point average (for example, 3.2), the student's num..

  Display random numbers to simulate rolling a die

1. Display random numbers to simulate rolling a die2. Create a java program that simulate a casino game. (rollin a die)

  Example of a problematic programming situation or scenario

Suggest one (1) example of a problematic programming situation or scenario that the use or implementation of a sequence structure could resolve

  Implement avl trees that allows both iterative traversal

implement avl trees that allows both iterative traversal and recursive traversal.iterative traversal is fairly easy if

  Method splitstack that takes a stack

Write a method splitStack that takes a stack of integers as a parameter and splits it into negatives and non-negatives. The numbers in the stack should be rearranged so that all the negatives appear on the bottom of the stack and all the non-negat..

  Which method will a web browser call on a new applet

Which method will a web browser call on a new applet? What would happen if "String[]args" is not included as argument in the main method

  Write a program to play a variation of the game

Roll two dice. Each die has six faces representing values 1, 1, ..., and 6. Check the sum of the two dice. If the sum is 2, 3, or 12 you lose; if the sum is 7 or 11, you win. If the sum is another value (4,5,6,8,9, or 10) a point is established.

  Create a class called employee

Create a class called Employee that has the following attributes: Employee ID of type String, First name of type String, Last name of type String

  Compute the correct number of square feet needed

The Carpet House owner wants to put a carpet calculator on his website so that visitors can estimate the amount of carpet they will need to purchase.

  What is a java archive

What type of output does the javadoc command generate and how do you view the output and what is a Java archive?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd