Basic Java Program
Framework/.DS_Store
__MACOSX/Framework/._.DS_Store
Framework/Main.java
Framework/Main.java
package
test
;
import
calculation
.
*
;
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
MyStack
[]
stacks
=
new
MyStack
[
5
];
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
stacks
[
i
]
=
new
MyStack
();
}
//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
=
new
MyStack
(
"5 + 2 + 32 * 10"
);
System
.
out
.
println
(
"s2 has "
+
s2
.
getSize
()
+
" item(s)"
);
s2
.
showItems
();
ProcessStack
.
parseMyStack
(
s2
);
//testing s3
System
.
out
.
println
(
"****************** Case 3 *********************"
);
MyStack
s3
=
new
MyStack
(
"5+2+32*10"
);
System
.
out
.
println
(
"s3 has "
+
s3
.
getSize
()
+
" item(s)"
);
s3
.
showItems
();
//testing s4
System
.
out
.
println
(
"******************* Case 4 ********************"
);
MyStack
s4
=
new
MyStack
(
"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
=
new
MyStack
(
"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
=
new
MyStack
();
s6
.
addItem
(
"d5.3.6 c/0.215+ 22.5+a31.2*1%21b"
);
System
.
out
.
println
(
"s6 has "
+
s6
.
getSize
()
+
" item(s)"
);
s6
.
showItems
();
ProcessStack
.
parseMyStack
(
s6
);
//testing s7
System
.
out
.
println
(
"******************* Case 7 ********************"
);
MyStack
s7
=
new
MyStack
();
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
=
new
MyStack
(
"1-5+32-2"
);
s8
.
showItems
();
ProcessStack
.
parseMyStack
(
s8
);
}
}
__MACOSX/Framework/._Main.java
Framework/MyStack.java
Framework/MyStack.java
package
calculation
;
import
java
.
util
.
*
;
public
class
MyStack
{
/*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;
*
* 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 object has ID = 0;
* The second created MyStack Object has ID = 1, and so forth
*
* Data member 3: is called "stack", which is the Java supported Stack.
* This is the main variable to store the expression.
*
* For these three data members, you should determine whether 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 "Source" 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 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 increase it by one every time when a
* new object is created
*
* Constructor 1: this constructor has no input arguments. You need to update the
* related variables as mentioned above. Also print out a message, 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 string type into the
* stack data member by simply calling addItem() member function.
* */
//Do the step 2 here
/*Step 3: complete the functions below*/
/*remove the top item (String) from the stack data member*/
public
void
removeItem
()
{
//Implement here
}
/*Print out all the items of the stack by printing each one in a new 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 access the items in the
* order from the top to the bottom. So to print them out in the order as the items
* are inserted, you need to use the Iterator class, which is returned from the stack.
* You need to look it up online on how to use Iterator class*/
public
void
showItems
()
{
//Implement here
}
/* Return the top character of the stack, you can't remove the top item but just read it*/
public
String
getTopItem
()
{
}
//Return how many items are there in the stack
public
int
getSize
()
{
//Implement here here
}
/*Step 4: as described in the instruction. This is the most important function for this class
* The role is to process the input String, and store them into the stack as items*/
public
void
addItem
(
String
exp
)
{
//Implement here
}
}
__MACOSX/Framework/._MyStack.java
Framework/ProcessStack.java
Framework/ProcessStack.java
package
calculation
;
//This Message class is just to used as a return type to show whether the stored items of the stack
//can be executable or valid. There are two data member involved, 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 "result" is meaningful. It shows the
// result of the expression. Otherwise for the case success == 0 or success == 1, we can assume
// result = 0.
class
Message
{
private
int
success
;
//valid indicator 0 - empty expression; 1 - invalid expression; 2 - valid expression
private
double
result
;
private
String
expression
;
//Constructor 1
public
Message
()
{
success
=
0
;
//by default, assume the expression is 0
result
=
0.0
;
}
public
Message
(
int
s
,
double
r
)
{
success
=
s
;
result
=
r
;
}
//below are basic setter and getter
public
int
getSuccess
()
{
return
success
;
}
public
void
setSuccess
(
int
success
)
{
this
.
success
=
success
;
}
public
double
getResult
()
{
return
result
;
}
public
void
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 implementing 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*/
class
Display
{
public
static
void
showMessage
(
Message
m
)
{
if
(
m
.
getSuccess
()
==
0
)
{
System
.
out
.
println
(
"No any input from the stack"
);
}
else
if
(
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 */
public
class
ProcessStack
{
public
ProcessStack
()
{
}
/*This method call the the calculate() and print out the corresponding message*/
public
static
void
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 description 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 cases: empty expression, invalid expression,
* and valid expression. If it is a valid expression, calculate it. According to the three
* cases, return different messages.
* */
public
static
Message
calculate
(
MyStack
ms
)
{
//if ms stores correct expression, return new Message(2, result);
//if ms has empty expression, return new Message(0, 0.0);
//if ms has invalid expression, return new Message(1, 0.0)
}
}