The tokens should be placed in an array of strings

Assignment Help History
Reference no: EM13892878

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: EM13892878

Questions Cloud

Publications of criminal convictions primarily overseas : Does the Clean Slate Act apply to publications of criminal convictions primarily overseas?
Determine the actual cost of a good or service : Describe three issues - problems that a company could encounter when trying to determine the actual cost of a good or service to be used in the cost of goods sold. For each of your issues, provide an example of a company or industry where these is..
Lack of expertise in one product or service : Brand extension can endanger brands because their lack of expertise in one product or service. A company can be good at some things or focus on one thing and be great and build a large loyal customer base.
What type of arthritis does sandy have : What type of arthritis does Sandy have? What signs/symptoms support this diagnosis rather than a different type? What lab tests indicate this type of arthritis
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..
Statement of owner''s equity : Use the following adjusted trial balance of Wilson Trucking Company to prepare the
How do state laws vary with regards to laws : How are LGBT individuals are treated different from heterosexual individuals with respect to the issue you selected? How do state laws vary with regards to laws or policies on the selected issue? What, if any, are recent changes in the rights of LGBT..
Read a list of values from the standard inpu : Java program that can be used to read a list of values from the standard input and display the following information based on the values read:1. All of the positive numbers and the count.
Business decisions as compared to macroeconomics? : business decisions as compared to macroeconomics?

Reviews

Write a Review

History Questions & Answers

  History of western art

In a critically insightful response, support and defend these theses on how these artwork/sculpture sources prove that: In the history of Western Art, the depiction of human form was initially flat and unrealistic,

  What is normative islam-scripturalism-sufism-philosophy

What is ‘normative’ Islam? Discuss the different threads that came to be involved in evolving concepts of Sunni orthodoxy (hint: scripturalism, Sufism, philosophy)?

  Changing the course of war

How did this happen? How did the Union policy towards slavery and enslaved people change over the course of the war?

  Accomplishments during american civil war

The solution offers a brief biography of Clara Barton. It includes a brief summary of her early life and her accomplishments during the American Civil War.

  Explain effects of the cuban embargo on cuba and america

Explain Effects of the Cuban Embargo on Cuba and America.

  Write a paper on the movies robin hood and the seventh seal

Write a paper on the movies Henry V, The Lion in Winter, A Man for All Seasons, The Messenger: The Story of Joan of Arc, Ivanhoe, A Knight's Tale, Robin Hood and The Seventh Seal.

  Write a historical research paper on the barbary wars

Write a historical research paper on the barbary wars. Search a topic or time period. For example, if you are a sports fan, you can search "baseball" or "flappers" if you are interested in the 1920s.

  What role did eisenhower play during the coldwar

what role did eisenhower play during the coldwar during the communist victory in china on american foreign policy in the 1950s

  Discuss the major battles and campaigns of the civil war

Discuss the major battles and campaigns of the Civil War from 1861 to the end of 1862.

  Purpose and function of african artwork

Use examples to explain why it is especially important to know the purpose and function of African artwork in order to understand it. How is pattern used to enhance cultural identity at Nankani compound, Sirigu, Ghana? What kinds of patterns are used..

  What was one tragic failure in american history

What was one tragic failure in american history looking from early colonial times through the mid-1800s ?be sure to defend what you say with facts,good ideas and analysis using own ideas.

  You will write a short position paper a full page or more

you will write a short position paper a full page or more use pen only that addresses the question what is

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