Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask History Expert

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.

History, Academics

  • Category:- History
  • Reference No.:- M91566927
  • Price:- $20

Guranteed 24 Hours Delivery, In Price:- $20

Have any Question?


Related Questions in History

Video the trail of tears documentary youtubea peoples

Video : The Trail of Tears Documentary (Youtube) A PEOPLE'S HISTORY OF THE UNITED STATES by Howard Zinn[Chapter 7: AS LONG AS GRASS GROWS OR WATER RUNS] Andrew Jackson (1767 - 1845) was our 7th US President. "Old Hickory ...

Project two website reviewinstructionsfollow the

PROJECT TWO: Website Review Instructions Follow the instructions carefully. Students are required to select one web resource from the Internet, describe its contents, and then write an evaluation according to the criteri ...

First follow the read the articles below you will need your

First, follow the read the articles below; you will need your Saint Leo portal logon information. CQ Researcher Report, "Immigration," 2013 "The American Dream?" by: Barbara D. Krasner.; Cobblestone, May/Jun2013, Vol. 34 ...

Trace the evolution of your profession or another chosen

Trace the evolution of your profession (or another chosen profession), field of study, or branch of service, starting in the Cold War era and continuing all the way to the present, globalized world. As you begin gatherin ...

Suppose the quantity of fish purchased by mr singh family

Suppose the quantity of fish purchased by Mr Singh family is 21 kilos per year when the price is $11.50 per kilo and 17 kilos per year when the price is $20.50 per kilo. Calculate the price elasticity of demand coefficie ...

Consider the market for pizza in middleton ontario whose

Consider the market for pizza in Middleton, Ontario, whose demand and supply schedules are given in the table below. Price of Pizza ($) Quantity Demanded Quantity Supplied 10 0 6 9 1 5 8 2 4 7 3 3 6 4 2 5 5 1 4 6 0 3 7 0 ...

Nationalismreview the contents and create a written

Nationalism Review the contents and create a written response in the form of a paragraph using at least five complete sentences that fully answer the question. After assessing the content of the Nationalism folder, what ...

A daoism summarize what the main understanding of daoism

A. Daoism Summarize what the main understanding of Daoism is. -What is the meaning of Ying and Yang in this religion? -In what ways has Divination been a part of Daoism? Definition of Divination:the practice of attemptin ...

How do employees evaluate the fairness of an authoritys

How do employees evaluate the fairness of an authority's decision making?

Proslavery authors1 how do these authors justify slavery2

Proslavery authors 1. How do these authors justify slavery? 2. According to these authors, what place do slaves have in society? 3. What derisive stereotypes of African Americans are found in these proslavery documents? ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As