Basic Java Program
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, “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 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; …. } 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 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 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 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:
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 [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 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]: *
[11]: 10 [12]: + [13]: / [14]: 8 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]: * [11]: 10 [12]: + [13]: / [14]: 8 [15]: 502 [16]: + [17]: 123 [18]: - [19]: - [20]: * [21]: 0.5 [22]: 0.5 [23]: 0.7 [24]: - 1-5+32-2 A stack with id 15 is created. The items in stack #15 [0]: 1 [1]: - [2]: 5 [3]: + [4]: 32 [5]: - [6]: 2 A stack with id 16 is created. The expression result is: 26.0