Section 5. Programming Exercise 1 We commonly write arithmetic expressions in the so-called infix form. That...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Section 5. Programming Exercise 1 We commonly write arithmetic expressions in the so-called infix form. That is, with each operator placed between its operand, as below (5+6)*(4/3) Although we are comfortable writing expressions in this form, infix form has the disadvantage that parentheses must be used to indicate the order in which operators are to be evaluated. These parentheses, in turn, complicate the evaluation process. N Evaluation is much easier if we can simply evaluate operators from left to right. Unfortunately, this evaluation will not work with the infix form of arithmetic expressions. However, it will work if the expression is in postfix form. In the postfix form of an arithmetic expression, each operator is placed immediately after its operands. The expression above is written in postfix form as 56+43/* Note that both forms place the numbers in the same order (reading from left to right). The order of the operators is different, however, because the operators in the postfix form are positioned in the order that they are evaluated. The resulting postfix expression is hard to read at first, but it is easy to evaluate programmatically. We will do so with stacks. Suppose you have an arithmetic expression in postfix form that consists of a sequence of single digit, nonnegative integers and the four basic arithmetic operators (+..."./). This expression can be evaluated using the following algorithm in conjuction with a stack of floating-point numbers. Part One: Create a program that reads the postfix form of an arithmetic expression, evaluates it, and outputs the result. Assume that the expression consists of single-digit, nonnegative integers ('0' to '9') and the 4 basic arithmetic operators (+,-, *, /'). Further assume that the arithmetic expression is input from the keyboards with all the characters separated by white space on one line. Save your program in a file called postfix.java. In order to complete this step, you will need to use the following: o Scanner sc = new Scanner (System.in); o Char[] c = sc.next.toCharArray(); This will return an array of characters with the postfix operation. You must change the numeric numbers from their char representation to Float objects to push them on stack. To be able to do this, use the following: In addition, use the unboxing functions of Float to change from object Float to primitive data type float: o Float floatOperand= operandObj.floatValue() Part Two: Using only a single stack object, solve the postfix arithmetic expression. Part Three: Test you program by running the following expressions and verifying the output. Test Case One operator o int operand = Character.getNumeric Value(c[0]); o Float operandObj = Float.valueOf(operand); Nested operators Uneven nesting All operators at end Zero dividend Single-digit number Arithmetic Expression 34+ 34+52/* 93*2+1- 4675-+* 02/ 7 Section 5. Programming Exercise 1 We commonly write arithmetic expressions in the so-called infix form. That is, with each operator placed between its operand, as below (5+6)*(4/3) Although we are comfortable writing expressions in this form, infix form has the disadvantage that parentheses must be used to indicate the order in which operators are to be evaluated. These parentheses, in turn, complicate the evaluation process. N Evaluation is much easier if we can simply evaluate operators from left to right. Unfortunately, this evaluation will not work with the infix form of arithmetic expressions. However, it will work if the expression is in postfix form. In the postfix form of an arithmetic expression, each operator is placed immediately after its operands. The expression above is written in postfix form as 56+43/* Note that both forms place the numbers in the same order (reading from left to right). The order of the operators is different, however, because the operators in the postfix form are positioned in the order that they are evaluated. The resulting postfix expression is hard to read at first, but it is easy to evaluate programmatically. We will do so with stacks. Suppose you have an arithmetic expression in postfix form that consists of a sequence of single digit, nonnegative integers and the four basic arithmetic operators (+..."./). This expression can be evaluated using the following algorithm in conjuction with a stack of floating-point numbers. Section 5. Programming Exercise 1 We commonly write arithmetic expressions in the so-called infix form. That is, with each operator placed between its operand, as below (5+6)*(4/3) Although we are comfortable writing expressions in this form, infix form has the disadvantage that parentheses must be used to indicate the order in which operators are to be evaluated. These parentheses, in turn, complicate the evaluation process. N Evaluation is much easier if we can simply evaluate operators from left to right. Unfortunately, this evaluation will not work with the infix form of arithmetic expressions. However, it will work if the expression is in postfix form. In the postfix form of an arithmetic expression, each operator is placed immediately after its operands. The expression above is written in postfix form as 56+43/* Note that both forms place the numbers in the same order (reading from left to right). The order of the operators is different, however, because the operators in the postfix form are positioned in the order that they are evaluated. The resulting postfix expression is hard to read at first, but it is easy to evaluate programmatically. We will do so with stacks. Suppose you have an arithmetic expression in postfix form that consists of a sequence of single digit, nonnegative integers and the four basic arithmetic operators (+..."./). This expression can be evaluated using the following algorithm in conjuction with a stack of floating-point numbers. Part One: Create a program that reads the postfix form of an arithmetic expression, evaluates it, and outputs the result. Assume that the expression consists of single-digit, nonnegative integers ('0' to '9') and the 4 basic arithmetic operators (+,-, *, /'). Further assume that the arithmetic expression is input from the keyboards with all the characters separated by white space on one line. Save your program in a file called postfix.java. In order to complete this step, you will need to use the following: o Scanner sc = new Scanner (System.in); o Char[] c = sc.next.toCharArray(); This will return an array of characters with the postfix operation. You must change the numeric numbers from their char representation to Float objects to push them on stack. To be able to do this, use the following: In addition, use the unboxing functions of Float to change from object Float to primitive data type float: o Float floatOperand= operandObj.floatValue() Part Two: Using only a single stack object, solve the postfix arithmetic expression. Part Three: Test you program by running the following expressions and verifying the output. Test Case One operator o int operand = Character.getNumeric Value(c[0]); o Float operandObj = Float.valueOf(operand); Nested operators Uneven nesting All operators at end Zero dividend Single-digit number Arithmetic Expression 34+ 34+52/* 93*2+1- 4675-+* 02/ 7 Part One: Create a program that reads the postfix form of an arithmetic expression, evaluates it, and outputs the result. Assume that the expression consists of single-digit, nonnegative integers ('0' to '9') and the 4 basic arithmetic operators (+,-, *, /'). Further assume that the arithmetic expression is input from the keyboards with all the characters separated by white space on one line. Save your program in a file called postfix.java. In order to complete this step, you will need to use the following: o Scanner sc = new Scanner (System.in); o Char[] c = sc.next.toCharArray(); This will return an array of characters with the postfix operation. You must change the numeric numbers from their char representation to Float objects to push them on stack. To be able to do this, use the following: In addition, use the unboxing functions of Float to change from object Float to primitive data type float: o Float floatOperand= operandObj.floatValue() Part Two: Using only a single stack object, solve the postfix arithmetic expression. Part Three: Test you program by running the following expressions and verifying the output. Test Case One operator o int operand = Character.getNumeric Value(c[0]); o Float operandObj = Float.valueOf(operand); Nested operators Uneven nesting All operators at end Zero dividend Single-digit number Arithmetic Expression 34+ 34+52/* 93*2+1- 4675-+* 02/ 7
Expert Answer:
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
1.what is value engineering ?2.what is value engineering importance ?3.what is the aim value of engineering ?4.what are the five 5 key principles of value engineering ?5.immediate benefits of value...
-
The number of chocolate chips in an 18-ounce bag of Chips Ahoy! chocolate chip cookies is approximately normally distributed, with a mean standard deviation of 118 chips, according to a study by...
-
Case Study: Quick Fix Dental Practice Technology requirements Application must be built using Visual Studio 2019 or Visual Studio 2017, professional or enterprise. The community edition is not...
-
Terminology Key: Key Word List Describe Explain What to do In bulleted, numbered or paragraph form, provide a number of consecutive items-if paragraph form, use commas (,) to separate items In...
-
In this exercise, we justify the use of "elementary column operations" to compute determinants. Prove that (a) Adding a scalar multiple of one column to another does not change the determinant; (b)...
-
(a) Ratio spread consists of an unequal number of options at different strike prices are purchased and written. For example, a put ratio spread can be created by buying at-the- money put option with...
-
Traditional management theory was designed to provide control and structure to which types of organizations? A. Hospitals. B. Scientific laboratories. C. Manufacturing industry. D. Institutions of...
-
The trial balance for Lindor Corporation, a manufacturing company, for the year ended December 31, 2011, included the following income accounts: The trial balance does not include the accrual for...
-
Nathan died testate. His will contained the following specific gifts: -He left his 2012 Chevy Equinox to his daughter, Bethany; -He left $5,000 to his grandson, Alexander; and -He left his gun...
-
A rotary worktable is driven by a Geneva mechanism with five slots. The driver rotates at 48 rev/min. Determine (a) The cycle time, (b) Available process time, and (c) Indexing time each cycle.
-
What is the most critical part of the meets - and - bounds legal description method?
-
Explain the significance of emotional intelligence in organizational behavior. How does it affect leadership effectiveness, conflict resolution, and team dynamics ?
-
A horsepower is 550 ft-lb/s. A watt is 1 N-m/s. Determine how many watts are generated by the engines of the passenger jet if they are producing 7000 horsepower.
-
What role does globalization play in the transnational flows of deviant practices, such as organized crime, terrorism, and cyber deviance, and in the global governance of deviant behaviors ?
-
Bridge City Consulting bought a building and the land on which it is located for $ 1 8 2 , 0 0 0 cash. The land is estimated to represent 7 0 percent of the purchase price. The company paid $ 2 2 , 0...
-
Clear Channel and CBS Radio were both far too large to accomplish the reverse Morris Trust structure, but they could have bid for the ABC Radio assets with cash. Suppose that Disney had sold the...
-
Double variable numSeconds is read from input. Type cast numSeconds to an integer. Ex: If the input is 11.5, then the output is: 11 public class IntegerNumberConverter { public static void...
-
(a) Water flows through the nozzle of a garden hose. Find an expression for m in terms of line pressure P 1 , ambient pressure P 2 , inside hose diameter D 1 , and nozzle outlet diameter D 2 . Assume...
-
Sherry Hopson owns a retail family clothing store. Her store is located at 4321 Circle Drive, Houston, TX 77001. Her employer identification number is 95- 1234321 and her Social Security number is...
-
Robert Ramos (age 36) is a single taxpayer, living at 8765 Bay Dr., Monterey, CA 93940. His Social Security number is 976-23-5132. Robert's earnings and income tax withholding as the manager of a...
-
Charlie's Green Lawn Care is a cash basis taxpayer. Charlie Adame, the sole proprietor, is considering delaying some of his December 2012 customer billings for lawn care into the next year. In...
-
Something may be rare, but if it is not _________ it is not scarce.
-
Capital includes human capital, the _________ people receive from _________.
-
_________ goods include fairness, friendship, knowledge, security, and health.
Study smarter with the SolutionInn App