Here are the key steps to implement this calculator project using stacks:
1. Create MyStack class with:
- A static variable to track number of stacks created
- An id variable for each stack
- A Stack member variable to store expression items
2. Implement addItem() method in MyStack to:
- Parse string into numbers and operators
- Push items onto stack member variable
3. Create ProcessStack class with:
- A calculate() static method to process a MyStack
4. In calculate():
- Check stack is not empty
- Pop and evaluate items based on operator precedence
- Return success/error message
5. Create Main class to:
- Generate test MyStack
A Critique of the Proposed National Education Policy Reform
Alcohol Awareness Special Lecture ReflectionAlcohol is among the.docx
1. Alcohol Awareness Special Lecture Reflection
Alcohol is among the most commonly used and abused
substances in today's society, and is of particular interest in the
college aged population as there are many habits and tendencies
of college students in regards to alcohol use that do not extend
past those 4 years (i.e. pre-gaming, drinking games, drinking to
get "blacked out", etc.).
Miami University Peer Hawks is an amazing group of YOUR
PEERS who are dedicated to increasing awareness about safe
alcohol use, as well as many other health behaviors (see
the Peer Hawks Website (Links to an external site.)Links to an
external site. for more information, and check out their video
below). Specifically, they have created an Alcohol Skills
Training Program that is specialized for YOU.
For this assignment, I'd like you to review the Alcohol Skills
Training Program (ASTP) PowerPoint, and write a 2 page
reflection. In this reflection, you may discuss anything that
you'd like in regards to the ASTP lecture. In addition, I'd like
you to consider the following questions:
1. What information, if any, was new to you? If so, were you
surprised by any of the information presented? Explain.
2. How have you seen the impact of alcohol use in your life?
Either personally, or generally (i.e. on campus, in your
hometown, etc.)?
3. What is your major take away point from this special lecture?
Why?
**NOTES**
· Your paper must be in 12 point Times New Roman font,
double spaced, with 1 inch margins on all sides.
· At the top of page 1, please use only your name in the header
at the top of the page (i.e. do not put the date, my name, the
class name, the assignment name, etc.)
· This ASTP PowerPoint can also be found in the Modules Tab.
2. Framework/.DS_Store
__MACOSX/Framework/._.DS_Store
Framework/Main.javaFramework/Main.javapackage test;
import calculation.*;
publicclassMain{
publicstaticvoid main(String[] args)
{
MyStack[] stacks =newMyStack[5];
for(int i =0; i <5; i++){
stacks[i]=newMyStack();
}
//testing stacks[2]
System.out.println("***************** Case 1 ************
**********");
stacks[2].addItem("1");
System.out.println("stacks[2] has "+ stacks[2].getSize()+" item(
s)");
stacks[2].showItems();
//testing s2
System.out.println("***************** Case 2 ************
**********");
MyStack s2 =newMyStack("5 + 2 + 32 * 10");
System.out.println("s2 has "+ s2.getSize()+" item(s)");
s2.showItems();
3. ProcessStack.parseMyStack(s2);
//testing s3
System.out.println("****************** Case 3 ***********
**********");
MyStack s3 =newMyStack("5+2+32*10");
System.out.println("s3 has "+ s3.getSize()+" item(s)");
s3.showItems();
//testing s4
System.out.println("******************* Case 4 **********
**********");
MyStack s4 =newMyStack("d5 ~c+ 2$+a32*10b");
System.out.println("s4 has "+ s4.getSize()+" item(s)");
s4.showItems();
ProcessStack.parseMyStack(s4);
//testing s5
System.out.println("******************* Case 5 **********
**********");
MyStack s5 =newMyStack("d5.3. c+ 22.5^+a31.2*10b");
System.out.println("s5 has "+ s5.getSize()+" item(s)");
s5.showItems();
ProcessStack.parseMyStack(s5);
//testing s6
System.out.println("******************* Case 6 **********
**********");
MyStack s6 =newMyStack();
s6.addItem("d5.3.6 c/0.215+ 22.5+a31.2*1%21b");
System.out.println("s6 has "+ s6.getSize()+" item(s)");
4. s6.showItems();
ProcessStack.parseMyStack(s6);
//testing s7
System.out.println("******************* Case 7 **********
**********");
MyStack s7 =newMyStack();
s7.addItem("d5.3.6 c/0.2 15+ 22. 5+a31.2 *10b +./8");
System.out.println("s6 has "+ s7.getSize()+" item(s)");
s7.showItems();
s7.addItem("502 + 123 -- *0.5ba.5.7-");
s7.showItems();
MyStack s8 =newMyStack("1-5+32-2");
s8.showItems();
ProcessStack.parseMyStack(s8);
}
}
__MACOSX/Framework/._Main.java
Framework/MyStack.javaFramework/MyStack.javapackage calc
ulation;
import java.util.*;
publicclassMyStack{
/*Step 1: Define three data members for this class
* Data member 1: is called "total_stacks" which is used
* to record how many MyStack objects have been created;
*
5. * Data member 2: is called "id" represents the id number
* the current MyStack object. Each created MyStack object
* should have one unique id number, which is assigned at
* the time when the object is created. You can use the total_
stacks
* as a reference. For example, the first created MyStack obje
ct has ID = 0;
* The second created MyStack Object has ID = 1, and so fort
h
*
* Data member 3: is called "stack", which is the Java suppor
ted Stack.
* This is the main variable to store the expression.
*
* For these three data members, you should determine wheth
er it is a
* "static" or "non-
static" according to its role as mentioned above
*
* Try to use the "Generate Setters and Getters" tool in the "S
ource" menu to
* create the three pairs of setters and getters automatically*/
//Do the step 1 here
/*Step 2: Create two constructors of MyStack(). For both of the
6. constructors, you
* need to make sure to assign the ID for the created object.
Meanwhile, maintain
* the number of "total_stacks" globally, which means to incr
ease it by one every time when a
* new object is created
*
* Constructor 1: this constructor has no input arguments. Yo
u need to update the
* related variables as mentioned above. Also print out a mes
sage, e.g. "A stack with
* the id #5 is created" (do this at the end of the constructor)
*
* Constructor 2: this constructor has one input argument -
"String exp". In addition to finishing the
* tasks as constructor 1 does, it also push the expression as s
tring type into the
* stack data member by simply calling addItem() member fu
nction.
* */
//Do the step 2 here
/*Step 3: complete the functions below*/
/*remove the top item (String) from the stack data member*/
7. publicvoid removeItem()
{
//Implement here
}
/*Print out all the items of the stack by printing each one in a ne
w line
* For example, for an expression 5 * 18 + 21
* [0] 5
* [1] *
* [2] 18
* [3] +
* [5] 21
* If you use the stack API directly, you probably can only ac
cess the items in the
* order from the top to the bottom. So to print them out in th
e order as the items
* are inserted, you need to use the Iterator class, which is ret
urned from the stack.
* You need to look it up online on how to use Iterator class*
/
publicvoid showItems()
{
//Implement here
}
/* Return the top character of the stack, you can't remove the to
p item but just read it*/
publicString getTopItem()
{
}
//Return how many items are there in the stack
8. publicint getSize()
{
//Implement here here
}
/*Step 4: as described in the instruction. This is the most import
ant function for this class
* The role is to process the input String, and store them into
the stack as items*/
publicvoid addItem(String exp)
{
//Implement here
}
}
__MACOSX/Framework/._MyStack.java
Framework/ProcessStack.javaFramework/ProcessStack.javapack
age calculation;
//This Message class is just to used as a return type to show whe
ther the stored items of the stack
//can be executable or valid. There are two data member involve
d, the first one "success" is
// to indicate whether the expression is valid, 0 -
empty expression; 1 - invalid expression; 2 - valid expression
// Actually only when success == 2, the second parameter "resul
t" is meaningful. It shows the
// result of the expression. Otherwise for the case success == 0
9. or success == 1, we can assume
// result = 0.
classMessage
{
privateint success;//valid indicator 0 - empty expression; 1 -
invalid expression; 2 - valid expression
privatedouble result;
privateString expression;
//Constructor 1
publicMessage()
{
success =0;//by default, assume the expression is 0
result =0.0;
}
publicMessage(int s,double r)
{
success = s;
result = r;
}
//below are basic setter and getter
publicint getSuccess(){
return success;
}
publicvoid setSuccess(int success){
this.success = success;
}
publicdouble getResult(){
return result;
}
10. publicvoid setResult(double result){
this.result = result;
}
}
/*This is a display class that is used to show message. In other
word, for the class ProcessStack below, it use
* this Display class to print out message information.
*
* The reason I design it in this way is to let you practice imple
menting different tasks in
* different modules. So this Display class is to take care of the
output message. The ProcessStack
* class is to take care the logic*/
classDisplay{
publicstaticvoid showMessage(Message m)
{
if(m.getSuccess()==0)
{
System.out.println("No any input from the stack");
}
elseif(m.getSuccess()==1)
{
System.out.println("The expression is wrong!");
}
else
{
System.out.println("The expression result is: "+ m.getResult());
}
}
}
/*The most important class in this file, which is used to process
11. */
publicclassProcessStack{
publicProcessStack()
{
}
/*This method call the the calculate() and print out the correspo
nding message*/
publicstaticvoid parseMyStack(MyStack s)
{
Message m = calculate(s);
Display.showMessage(m);
}
/*Step 5: implement the following function
* This is the most important function of this file. The descri
ption for this function
* is already defined in the instructions. Basecially, you will
check the stored item of
* the MyStack input parameter. There are three possible case
s: empty expression, invalid expression,
* and valid expression. If it is a valid expression, calculate i
t. According to the three
* cases, return different messages.
* */
publicstaticMessage calculate(MyStack ms)
{
//if ms stores correct expression, return new Message(2, result);
//if ms has empty expression, return new Message(0, 0.0);
12. //if ms has invalid expression, return new Message(1, 0.0)
}
}
__MACOSX/Framework/._ProcessStack.java
__MACOSX/._Framework
Computing Project
1. Project Description:
For this project, you will implement a core part of a calculator
that for a given sequence
of computation (e.g. “5 + 3 * 12 / 25”), your program is able to
analyze the expression
string by extracting out numbers or operators, and finally
compute the result if the input
is a valid expression. To achieve this goal, you will use the
“Stack” class to store those
data and process them properly. The detailed instructions are
given as below.
2. Project Goals:
• To warm up with Java classes and packages
• To practice on static and non-static members inside a class
• To learn to use Eclipse IDE to generate “Setters and Getters”
• To get familiar about commonly used container class in Java,
13. “Stack”
• To learn to look up Java APIs online, such as how to use
String, Stack, Double
3. Project Basic Classes:
There are three main classes involved: “MyStack”,
“ProcessStack”, and “Main”. The first
two classes are in a package called “calculation”; the “Main”
class is in another package
“test”. The basic roles for these classes are:
• MyStack class
A Stack data structure class is used to hold the extracted
numbers or operators from the
input string expression and store them into a stack. So each item
of the stack is a String.
• ProcessStack class
This class is responsible to process an object of MyStack. If the
MyStack stored
expression is valid, the ProcessStack will compute the results;
otherwise, it reports an
error.
• Main class
This is a basic test class. You really do NOT need to implement
anything here. It is
basically for testing purpose on the two above classes. I will
provide you some code in
the Main class. Then you can use it as a basic test. You are free
to have more testing code
here.
4. Two Important Functions:
For this project, there are several functions you need to
14. implement. The two most
important functions are “addItem()” and “calculate()”. The first
one is from
the “MyStack” class; the second one is from the “ProcessStack”
class. The details about
these two functions are as below:
Function 1: public void addItem(String exp)
This function is to analyze the input String “exp” and store the
extracted items into a
stack. For example, if the input is “5 + 2 * 12 - 24”. It will be
stored into a stack as:
stack: 24
–
12
*
2
+
5
As this function is inside the “MyStack” class, so it operates on
a stack variable, which is
a data member of the MyStack class. This variable is defined by
the Java supported class
"Stack”. In other words, the “MyStack” class is a kind of
wrapper of the Java Stack class.
Here is a piece of code of the MyStack class:
public class MyStack {
...
private Stack stack;
15. ….
}
Also, the function addItem(String exp) should be able to
distinguish invalid characters.
For example, if the input expression is “5 a+b2 *12-f24”, it can
filter out those invalid
characters, such as letters ‘a’, ‘b’, ‘f’, and re-store the right
expression “5 + 2 * 12 - 24”
into the stack.
The general principle for this character analysis is: you use a
loop to analyze each
character of the String (there is a good function of the Java
String class; please look it
up). If the current character is one of the ‘+’, ‘–’, ‘*’, ‘/’
characters, push it into stack
directly. If the current character is a number, you should keep
moving forward to the
next character. Because a number can be made of more than one
characters, e.g 329,
which has three characters. You need to keep moving until the
next one is not a number
or a ‘.’. Here ‘.’ is a special and challenging case. Because for a
floating number, e.g.
52.8, you need to consider the ‘.’ as part of a number. But the
‘.’ can show at most ONCE
in a number. For example, you never have a number looks like
32.57.65, which can not
be a number. For this example, we consider it as two numbers:
32.57 and 0.65. We allow
users to use a casual way to represent “0.65” by “.65”. If the
current character is
16. neither a number (including ‘.”) nor an operator, you just
simply ignore this character.
Here are an example of correctly extracted results:
Input String: “62d – 23.5 ba 5+2 12 – 3 * a +zer/6
+.52.3*2.6%$”
Extracted results: “62”, “–”, “23.5”, “5”, “+”, “2”, “12”, “–”,
“3”, “*”, “a”, “+”, “/”, “6”,
“+”, “0.52”, “0.3”, “*”, “2.6”.
In the example above all letters, such as ‘a’, ‘z’, will be
automatically filtered out.
Function 2: public static Message calculate(MyStack ms)
This function is to execute the expression from the input
parameter of MyStack. To be
exact, it actually reads the items from the “stack" member of
MyStack and computes the
result of the stored items if it is a valid expression. Here,
someone may wonder, since the
“addItem()” function already filters those invalid characters,
why it is still necessary to
check potential invalid expressions. This is because, the
function “addItem()” can only
detect invalid characters. But it cannot check whether it is a
valid expression. For
example: “5 + 6 * / 2 – 12 8”, all of the characters are valid, but
it is not a valid
expression. This is the responsibility for “calculate()” to
identify its correctness. If it is a
valid expression, the function should calculate the result and
return it in the data type of a
Message. Otherwise, it returns an error.
Attention: to make this task simpler, when calculate the result,
you don’t need to consider
17. the priority between “+”, “–”, “*”, “/”, we can assume the left
go first. For example: if the
expression is 5 + 2 * 3, if we ignore the operator priority, the
answer is 21. As the “+” is
on the left, it goes first.
5. Steps to finish:
For this project, you are provided with the basic code
framework, which contains three
“.java” files. So what you need to do is to implement some
functions as required. The
steps details are put inside the comments of the project. There
are 5 steps involved:
“MyStack.java”: steps (1) – (4)
- Step (1): define data members
- Step (2): create constructors
- Step (3): complete some easy functions
- Step (4): finish one important function
“ProcessStack.java”: step (5)
- Step (5): finish another important function
6. To start:
First, you are suggested to create a project from a scratch with
the project name
“Project1”. Then create two packages with the same names as
shown below and create
the corresponding three “.java” files. Copy the code of the
18. provided files into the
corresponding “.java” directly. Simply copy all the code to each
file (Ctrl + A, Ctrl + C,
Ctrl + P). The project structure is shown as the figure below:
When you first copy all the code, you may find many
compilation errors. Don’t worry.
This is because some of the data members or functions are not
implemented yet. As you
finish those steps, the errors will disappear. But you can
comment out all the code in the
“Main.java” first if you feel those errors are not very
comfortable.
Try to finish the Step (1) – (4) first. You can then uncomment
some of the code in the
“Main.java” to test it. Such as constructors, or addItem(). You
can also write down your
own test code inside the “Main.java” to test your current code
as you proceed. Finally,
you can work on the Step(5).
6. Rubrics:
- Successfully finish Step (1) (15%)
- Successfully finish Step (2) (20%)
- Successfully finish Step (3) (20%)
- Successfully finish Step (4) (25%)
- Successfully finish Step (5) (30%)
7. Some testing results:
19. Here are some result according to the provided testing code in
the “Main.java”. You can
also try to use your own code to further test your
implementation.
_________________________Testing
Results_________________________________
A stack with id 0 is created.
A stack with id 1 is created.
A stack with id 2 is created.
A stack with id 3 is created.
A stack with id 4 is created.
***************** Case 1 **********************
stacks[2] has 1 item(s)
The items in stack #2
[0]: 1
***************** Case 2 **********************
5 + 2 + 32 * 10
A stack with id 5 is created.
s2 has 7 item(s)
The items in stack #5
[0]: 5
[1]: +
[2]: 2
[3]: +
[4]: 32
[5]: *
[6]: 10
A stack with id 6 is created.
The expression result is: 390.0
****************** Case 3 *********************
5+2+32*10
A stack with id 7 is created.
s3 has 7 item(s)
The items in stack #7
20. [0]: 5
[1]: +
[2]: 2
[3]: +
[4]: 32
[5]: *
[6]: 10
******************* Case 4 ********************
d5 ~c+ 2$+a32*10b
A stack with id 8 is created.
s4 has 7 item(s)
The items in stack #8
[0]: 5
[1]: +
[2]: 2
[3]: +
[4]: 32
[5]: *
[6]: 10
A stack with id 9 is created.
The expression result is: 390.0
******************* Case 5 ********************
d5.3. c+ 22.5^+a31.2*10b
A stack with id 10 is created.
s5 has 7 item(s)
The items in stack #10
[0]: 5.3
[1]: +
[2]: 22.5
[3]: +
[4]: 31.2
[5]: *
[6]: 10
21. A stack with id 11 is created.
The expression result is: 590.0
******************* Case 6 ********************
A stack with id 12 is created.
s6 has 11 item(s)
The items in stack #12
[0]: 5.3
[1]: 0.6
[2]: /
[3]: 0.215
[4]: +
[5]: 22.5
[6]: +
[7]: 31.2
[8]: *
[9]: 1
[10]: 21
A stack with id 13 is created.
The expression is wrong!
******************* Case 7 ********************
A stack with id 14 is created.
s6 has 15 item(s)
The items in stack #14
[0]: 5.3
[1]: 0.6
[2]: /
[3]: 0.2
[4]: 15
[5]: +
[6]: 22
[7]: 5
[8]: +
[9]: 31.2
[10]: *