Implements adts stack and queue for a calculator application

Assignment Help Data Structure & Algorithms
Reference no: EM131395598

Assignment

1. Purpose

This assignment implements ADTs Stack and Queue for a calculator application.

2. Description

In this project, we implement the "=" key for the calculator by using postfix expression as figure 1 shows. After user enters the expression "5+3*12-206" and hits the "=" key, it will display the arithmetic result on the screen "-165" (figure 1(b)). To achieve this goal, you need:

(1) To implement the MyStack class
(2) To implement a queue structure, called MyQueue class
(3) To apply the above two structures to convert infix expression to postfix expression
(4) To calculate the final result based on the postfix expression

(a) (b) (c)

(d) (e) (f) (g)

Figure 1: (a) user enters an arithmetic expression. (b) the calculation result after hitting "=". (c) after getting the result -165, user continues entering "+", "5". (d) the result of user hitting "=" again. (e) the result of user hitting "<". (f) the result of user entering "+", "2", "*", "5". (g) the result of user hitting "=" again.

From the demos in Figure 1(c) to (g), you can see the calculator allows user to continue entering new expressions based on the previous calculation result. In other word, the output of "=" can be directly used as the input for subsequent expression in the next round. This feature offers user with extra convenience for multiple steps of calculations.

You don't need to implement everything from scratch. The GUI part is already implemented. Now, you just need to focus on the "=" button implementation.

The provided Framework

Download the "framework.zip" file and unzip it. There are four source code files for this project:

(1) Calculator.java (2) MyStack.java (3) MyQueue.java (4) application.css

Again the "calculator.java" and "application.css" are both completed that you do not need to write anything there. You need to work on the "MyStack.java" and "MyQueue.java". When you run the framework for the first time, the "=" key just clears the calculator screen instead of giving any final result. So it is your turn to empower the "=" key with calculation ability and deliver the result to the screen.

Requirements

You are required to complete all the functions that have the comment "Implementation here: ..." in the "MyStack.java" and "MyQueue.java" files. The functions inside "MyQueue.java" are based on the Queue ADT that allows data to be stored in a "First-In-First-Out" manner. This container is needed to generate infix and postfix expressions.

In addition to the calculation ability for the "=" key as described above, we also require the "=" is robust enough that if user enters incomplete expression (i.e. extra operator at the end without an operand followed), it can automatically drop the extra operation. For example, if the entered expression is

"5 + 2 * 3 - 9 * 2 +"

the last "+" is not valid. So the calculator should ignore the last "+" during calculation. The good news is this validation check is already implemented in the "getInfixFromString()" function. So no any extra coding is neede. However, you should be aware of it. Because later on after you implement the MyQueue (as described below), you may encounter some testing cases with the missing last operator. So "No Upset" about the automatic removal of the last invalid operator.

Steps of implementation

(1) You should start programming from the "MyQueue.java" file, which has three important functions to complete "isEmpty()", "enqueue()", and "dequeue ()".

(2) The MyQueue class is implemented by using array, which requires an initial size. To let MyQueue dynamically grow or shrink its size, the "enqueue()" and "dequeue()" should have the ability to resize the array when it is necessary. Here is the dynamic size changing policy:

- "enqueue()" increases the capacity to twice of the current size if the array becomes full.

- "dequeue()" decreases the capacity to half of the current size if the number of elements is less than ¼ of the total capacity.

(3) After completing the "MyQueue.java", you can test it by using the testing code below:

MyQueue copy_infix_queue = new MyQueue();
while(!infix_queue.isEmpty())
{
String token = (String)infix_queue.dequeue(); System.out.println(token); copy_infix_queue.enqueue(token);
}
infix_queue = copy_infix_queue;

You need to copy and paste the above piece of code to the "MyStack.java" at line 285 if you have not done any code in this file yet (at the bottom of the "getInfixFromString()" function). After you have finished the MyQueue class successfully, this piece of testing code will print out all the "operand" and "operator" separately at the console. For example, in figure 2, after entering "95+23*5", then hit the "=" key. There are 5 tokens extracted "95", "+", "23", "*", "5". They are stored in an infix queue. This token extraction task is performed by the "getInfixFromString()", which is already implemented.

(a) (b)

Figure 2: testing result: (a) what user has entered in the calculator (b) An infix expression output to the console after hitting "=" key.

(5) After user hits the "=" key, it will trigger the "computeExp()" function of MyStack class, which involves five steps (read the comments in the source code). Three out of the five steps are already completed. The only two remaining steps are encapsulated in the two functions: "infix2postfix()" and "processPostfix()". They are the two major tasks for you to accomplish. The first function aims to convert an input infix queue into a postfix queue and output it. The second function will use the postfix queue as the input and calculate the final result. So the relationships of these two functions and the one above "getInfixFromString()" are illustrated in figure 3:

Figure 3: Functions relationships: from left to right, each function's output is the input for its right neighbor.

(6) After you have finished implementing "infix2postfix()", you can add the testing code below. Similar to the way you tested "getInfixFromString()" in step (3), you need to copy and paste the following code to the "MyStack.java" at the bottom of "infix2postfix()" function right before the line "return postfix_queue;":

MyQueue copy_postfix_queue = new MyQueue();
while(!postfix_queue.isEmpty())
{
String token = (String) postfix_queue.dequeue(); System.out.println(token); copy_postfix_queue.enqueue(token);
}
postfix_queue = copy_postfix_queue;

Run the code. Then you should be able to see a list of tokens are output to the console in a postfix order as Figure 4 shows.

Figure 4: testing result: (a) what user has entered in the calculator (b) A postfix expression output to the console after hitting "=" key.

Below are some additional examples about infix and postfix conversion result

Infix Expression

Postfix Expression

5+2*3-6+18

5, 2, 3, *, +, 6, -, 18, +

-5*2+95-5

-5, 2, *, 95, +, 5, -

52*3-9+17

52, 3, *, 9, -, 17, +

2-6-5+10*3+4

2, 6, -, 5, -, 10, 3, *, +, 4, +

(7) For the "processPostfix()" function, it calculates the final result according to the input postfix queue. Here you need to create a stack variable of MyStack to store all the operands.

Attentions: (a) when you push a node into MyStack, you should call the "pushNode()" function instead of "push()". This is because, the "push()" is a special function for the calculator use only for keyboard input. But this function can NOT be used as the general push action for stack. So I put a general push function, called "pushNode()" in the class. It is already implemented. You just need to use it. (b) After you compute the final value from the postfix expression, you need to convert it into a String, which is the return type for the function "processPostfix()". If you have successfully implemented this function, you should be able see the final result on the calculator's screen.

3. Grading Notes

• Receive zero points if it has any compilation error
• Successfully complete the function "isEmpty()"of MyQueue class (10%)
• Successfully complete the function "enqueue()" of MyQueue class (15%)
• Successfully complete the function "dequeue()" of MyQueue class (15%)
• Successfully complete the function "infix2postfix()" of MyStack class (30%)
• Successfully complete the function "processPostfix()" of MyStack class (30%)

4. Submission

Your project should be submitted through ISIDORE. You just need to turn in your updated version of the source codes "MyStack.java" and "MyQueue.java", which can be zipped into a single folder.

Attachment:- Attachments.rar

Reference no: EM131395598

Questions Cloud

Dispute between brian - elaine - penny - sam : Determine whether the following disputes are verbal, factual, or some combination of the two. If verbal, discuss whether the dispute arises from vagueness or ambiguity.
How hormones are involved and relate to the behavior : Write a 1,050-word critique that includes the following: A concise summary of the article, including hypothesis and research methods used and An evaluation of how hormones are involved and relate to the behavior
Discuss the requirements for remote administration resource : Discuss the requirements for remote administration, resource management and SLA management. It may be useful to consider Morad and Dalbhanjan's operational checklists for Child Protection 's data file exchange and payroll services. This section shoul..
Topic - advantages of personality assessments : What benefit would there be to an employer if they were to pre-screen all job applicants with a combination of personality assessments and instruments measuring Emotional Intelligence levels
Implements adts stack and queue for a calculator application : CPS 350- This assignment implements ADTs Stack and Queue for a calculator application. In this project, we implement the "=" key for the calculator by using postfix expression as figure 1 shows.
Dispute between mindy and karen : Determine whether the following disputes are verbal, factual, or some combination of the two. If verbal, discuss whether the dispute arises from vagueness or ambiguity.
Describe one problem related to internal validity : Briefly summarize the research. Describe one problem related to internal validity. Describe one problem related to external validity
Is the modulus of elasticity of steel an extensive property : Is the modulus of elasticity of steel an extensive or intensive property? Explain why.Is the caloric content (i.e., number of calories) of a Big Al's milkshake an extensive or intensive property? Explain why.The temperature difference between a hot w..
Dispute between anthony and cindy : Determine whether the following disputes are verbal, factual, or some combination of the two. If verbal, discuss whether the dispute arises from vagueness or ambiguity.

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Write a program that allows cindy to input the number

One metric ton is approximately 2205 pounds. Write a program that prompts the user to input the amount of rice, in pounds, in a bag. The program outputs the number of bags needed to store one metric ton of rice.

  Give and compare the heights of the two trees

- First insert them sequentially into a binary search tree. - Next reinsert them into an empty AVL tree, restoring the AVL property after each insertion. Show the AVL tree which results after each insertion.

  Find running time of heap sort input sorted-ascending order

Determine the running time of Heap Sort if input is sorted in ascending order. Determine the running time of Heap Sort if input is sorted in descending order.

  Include methods to set and get values for each data field

Design a class named MagazineSubscription that has fields for a subscriber's name, the magazine name, and number of months remaining in the subscription. Include methods to set and get the values for each data field.

  Algorithm to decide whether there are 2 integers sum equal x

Note that there is no restriction on integers in set P and integer x, that is, we are not restricting ourselves to positive or negative integers.

  Important java questions

Add a method addText to the Question class, and provide a different implementation of Choice Question that calls add Text rather than storing an array list of selections.

  Analyzing network problem

Assume you are the Systems Analyst at a producing corporation in Seattle, WA. A Systems Analyst in your company's New York office sends you a trace file to examine.

  Compute the memory required by array

Compute the memory required by array and linked list to store exactly 10 nodes. Note that the linked list requires head pointer too.

  Give a recursive algorithm for finding the number of one''s

Give a recursive algorithm for finding the number of one's in a bit string, name the algorothm count-ones.

  An infix expression is one in which operators are located

an infix expression is one in which operators are located between their operands. this is how we are accustomed to

  Write algorithm to decide which commute is cheaper

Write working algorithm in pseudo code to decide which commute is cheaper: You wish to decide whether you must drive your car to work or take train. You know one-way distance

  Write a method that finds the average age of the students

Write a method that finds the average age of the students stored in the data structure and some Java code that could be used in a test program to display the value returned by the method on the console or command prompt.

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