**$ 16.00**

# CSE205 Object Oriented and Data Structures Project 3 complete solutions correct answers key

- Vpqnrqhwk
- Rating : 39
- Grade :
**A+** - Questions : 2
- Solutions : 1079
- Blog : 0
- Earned : $18608.77

CSE205 Object Oriented Programming and Data Structures Programming Project 3 complete solutions correct answers key

**1Submission Instructions**

Create a folder named < *asuriteid> *where *asuriteid *is your ASURITE user id (for example, since my ASURITE user id is

*kbu**rger2 *my folder would be named *k**burger2*) and copy all of your *.java *source code files to this folder. Do not copy the

*.class *files or any other files. Next, compress the * *folder creating a **zip archive **file named <*asuriteid>.zip *(mine would be named *kburger2.zip*). Upload <*asuriteid>.zip *to the Project 3 dropbox by the project deadline. Consult the online syllabus for the late and academic integrity policies.

**2 Learning Objectives**

1. Complete all of the learning objects of the previous projects.

2. To use the linked list, stack, and queue classes.

**3 Background**

In the lecture notes and video lectures for *Stacks and Queues : Sections 3 – 7 *we discussed an application that evaluates an arithmetic expression written in infix notation such as:

(-1 - -2) * -(3 / 5)

Infix notation is the usual algebraic notation that we are all familiar with where a binary operator (a binary operator is an operator that has two operands) is written between the left-hand and right-hand operands. For example, in the expression above, the left-hand operand of the subtraction operator is -1 and the right-hand operand is -2. Some operators, such as the negation operator, are unary operators meaning there is only one operand. For negation, the operand is written to the right of the negation operator. Therefore, this expression contains six operators: negation, subtraction, negation, multiplication, negation, and division.

In the algorithm that evaluates the expression, we also treat a left parenthesis as an operator, which it is not, but during the evaluation we have to push left parentheses onto the operator stack.

In an infix arithmetic expression, each operator has a precedence level, which for a left parenthesis, is different when it is on the operator stack as opposed to when it is not (this will become clear when you read the trace of the algorithm for the

above expression; see page 3):

**Op****erator**

**Normal Precedence Level**

**S****t****ack Precedence Level**

(

5

0

–

4

4

* /

3

3

+ –

2

2

)

1

1

Right parentheses really don't have precedence because they are not pushed on the operator stack, but we assign them a precedence level of 1 for consistency. The algorithm discussed in the notes did not handle the negation operator so I have modified it to handle negation. Here is the revised algorithm:

**M****ethod ***evaluate*(**In: ***pExpr *as an infix expression) **Returns ***Double*

Create *operatorStack *-- Stores *Operators*

Create *operandStack *-- Stores *Operands*

**W****h****ile **end of *pExpr *has not been reached **Do**

Scan next *token *in *pExpr *-- The type of *token *is

*T**oken ***If ***token *is an operand **Then**

Convert *token *to *Operand *object named

*nu**m**be**r operandStack*.*push*(*number*)

**E****lseIf ***token *is an **InstanceOf ***LeftParen ***Then**

Convert *token *to *LeftParen *object named

*paren operatorStack*.*push*(*paren*)

**E****lseIf ***token *is an **InstanceOf ***RightParen ***Then**

**W****h****ile not ***operatorStack*.*peek*() is an **InstanceOf ***LeftParen ***Do**

*topEval*() *operatorStack*.*pop*() -- Pops the *LeftParen*

**E****lseIf ***token *is *Negation*, *Addition*, *Subtraction*, *Multiplication*, or *Division ***Then**

Convert *token *to *Operator *object named *operator*

**W****h****ile ***k**eepEvaluating*() returns **True Do ***topEval*()

*operatorStack*.*push*(*op*)

**E****n****d While**

**W****h****ile not ***operatorStack*.*isEmpty*() **Do**

*topEval*() **Return ***operandStack*.*pop*()

**E****n****d Method ***evaluate*

**M****ethod ***k**eepEvaluating*() **Returns True **or **False If**

*operatorStack*.*isEmpty*() **Then Return False**

**E****lse Return ***s**tackPrecedence*(*operatorStack*.*peek*()) ≥

*precedence*(*operator*) **End Method ***keepEvaluating*

**Me****thod ***topEval*() **Returns Nothing ***right *← *operandStack*.*pop*() *operator *← *operatorStack*.*pop*()

**If ***operator *is *Negation ***Then ***operandStack*.*push*(-*r**ight*)

**E****lse ***l**eft *← *operandStack*.*pop*()

**If ***operator *is *Addition ***Then o**

*perandStack*.

*push*(

*left*+

*r*

*ight*)

**ElseIf**

*operator *is *Subtraction ***Then ***operandStack*.*push*(*left *- *right*)

**E****lseIf ***operator *is *Multiplication ***Then ***operandStack*.*push*(*left ** *right*)

**E****lse ***operandStack*.*push*(*left */ *right*)

**E****n****d If**

**E****n****d Method ***topEval*

**M****ethod ***precedence*(**In: ***O**perator pOperator*) **Returns Int If**

*pOperator *is *LeftParen ***Then Return **5

**E****lseIf ***pOperator *is *Negation ***Then Return **4

**ElseIf ***p**O**p**e**rator *is *Multiplication *or *Division ***Then Return **3

**ElseIf ***p**O**p**e**rator *is *Addition *or *Subtraction ***Then Return **2

**E****lse Return **1

**E****n****d Method ***precedence*

**M****ethod ***s**tackPrecedence*(**In: ***O**perator pOperator*) **Returns Int If**

*pOperator *is *LeftParen ***Then Return **0

**E****lseIf ***pOperator *is *Negation ***Then Return **4

**ElseIf ***p**O**p**e**rator *is *Multiplication *or *Division ***Then Return **3

**ElseIf ***p**O**p**e**rator *is *Addition *or *Subtraction ***Then Return **2

**E****lse Return **1

**E****n****d Method ***s**tackPrecedence*

It would be worthwhile to trace the algorithm using the above expression to make sure you understand how it works:

1. Create the operand and operator stacks. Both are empty at the beginning.

2. Scan the first token ( and push it onto the operator stack.

3. Scan the next token – (negation) and push it onto the operator stack.

4. Scan the next token 1 and push it onto the operand stack.

5. Scan the next token – (subtraction). Since the operator on top of the operator stack (negation) has higher precedence than subtraction, evaluate the top (note: negation is a unary operator so there is only one operand to be popped from the operand stack):

a. Pop the top number from the operand stack. Call this *right *= 1.

b. Pop the top operator from the operator stack. Call this *operator *= – (negation). c. Evaluate *operator *and push the result (-1) onto the operand stack.

d. Now push the subtraction operator onto the operator stack.

6. Scan the next token – (negation). Since the operator on top of the stack (subtraction) has precedence less than negation, push the negation operator onto the operator stack.

7. Scan the next token 2 and push it onto the operand stack.

8. Scan the next token ). Pop and evaluate operators from the operator stack until the matching ( is reached. a. The top operator is a unary operator (negation):

Pop the top number from the operand stack. Call this *right *= 2.

Pop the top operator from the operator stack. Call this *operator *= –

(negation). Evaluate *operator *and push the result (-2) onto the operand stack. b. The top operator is a binary operator (subtraction):

Pop the top number from the operand stack. Call this *right *= -2. Pop the top number from the operand stack. Call this *left *= -1.

Pop the top operator from the operator stack. Call this *operator *= –

(subtraction). Evaluate *operator *and push the result (1) onto the operand stack. c. The top operator is ( so pop it.

9. Scan the next token * (multiplication). The operator stack is empty so push *.

10. Scan the next token – (negation). Since negation has higher precedence than the operator on top of the operator stack (multiplication) push the negation operator onto the operator stack.

11. Scan the next token ( and push it onto the operator stack.

12. Scan the next token 3 and push it onto the operand stack.

13. Scan the next token / (division). Since the operator on top of the stack (left parenthesis) has higher precedence than division push / onto the operator stack. Now do you see why the precedence of ( changes when it is on the operator stack?

14. Scan the next token 5 and push it onto the operand stack.

15. Scan the next token ). Pop and evaluate operators from the operator stack until the matching ( is reached. a. The top operator is binary operator (division):

Pop the top number from the operand stack. Call this *right *= 5.

Pop the top number from the operand stack. Call this *left *= 3.

Pop the top operator from the operator stack. Call this *operator *= /. Evaluate *operator *and push the result (0.6) onto the operand stack.

b. The top operator is ( so pop it.

16. The end of the infix expression string has been reached. Pop and evaluate operators from the operator stack until the operator stack is empty.

a. The top operator is a unary operator (negation):

Pop the top number from the operand stack. Call this *right *= 0.6.

Pop the top operator from the operator stack. Call this *operator *= – (negation). Evaluate *operator *and push the result (-0.6) onto the operand stack.

b. The top operator is a binary operator (multiplication):

Pop the top number from the operand stack. Call this *right *= -0.6. Pop the top number from the operand stack. Call this *left *= 1.

Pop the top operator from the operator stack. Call this *operator *= *. Evaluate *operator *and push the result (-0.6) onto the operand stack.

17. The operator stack is empty. Pop the result from the operand stack (-0.6) and return it.

**4 Software Requirements**

The project shall implement a calculator which accepts an input from console, a syntactically correct arithmetic expression written in infix notation and displays the result of evaluating the expression. The program shall meet these requirements.

1. The program should be implemented in such a way that it requires a user input, infix expression.

2. Once the input is given to the program, it evaluates the expression and displays the result.

3. **Note: **you do not have to be concerned with syntactically incorrect infix expressions. We will not test your program with such expressions.

**5 Additional Project Requirements**

1. Format your code neatly. Use proper indentation and spacing. Study the examples in the book and the examples the instructor presents in the lectures and posts on the course website.

2. Put a comment header block at the top of each method formatted thusly:

/**

* ** A brief description of what the method does**. */

3. Put a comment header block at the top of each source code file formatted thusly:

//******************************************************************************************************** // CLASS: ** classname **(

**.java)**

*classname*//

// COURSE AND PROJECT INFO

// CSE205 Object Oriented Programming and Data Structures, *semester and year*

// Project Number: *project-number*

//

// AUTHOR

// ** your-name **(

**)**

*your-email-addr*//********************************************************************************************************

4. You are required to submit a README file, which will describe how to run your code.

5. You are also required to submit a report along-with the project. This report should consists screenshots of your output for the following expression.

a) (-1 - -2) * -(3 / 5)

b) (3 + -4) / (4-1)

c) ((-1 - -2) * -(3 / 5)) + ((3 + -4) / (4-1))

## [Solved] CSE205 Object Oriented and Data Structures Project 3 complete solutions correct answers key

- This Solution has been Purchased 1 time
- Submitted On 19 Jun, 2016 03:00:33

- Vpqnrqhwk
- Rating : 39
- Grade :
**A+** - Questions : 2
- Solutions : 1079
- Blog : 0
- Earned : $18608.77