COSC 122 Programming II
In this lab you will create a simple class hierarchy of arithmetic expression classes based on a specification given in a Unified Modeling Language diagram.
- Teams
You are encouraged to work with a partner for this lab. You and your partner should begin thinking about the problems and begin writing the code before lab time.
You may work by yourself if you prefer.
- Create a new project called ArithmeticExpressions.
- Implement in Java the class hierarchy shown in the following UML
diagram:
Some important points:- The numeric type represented in these expressions is int.
- Remember that italics in a UML class diagram corresponds to abstract.
- Remember that elements missing in a subclass in a UML diagram that appear in the superclass are inherited as is.
- The fields in BinaryExpression should be protected, not private (subclass methods must be able to get to them).
- The constructors for AdditionExpression and MultiplicationExpression should delegate all work to their superclass constructor.
- Except for constructors, do not add any additional fields and do not add any additional methods to your Java classes; faithfully implement the UML diagram.
- Place all the classes in this hierarchy in a Java package named arithmeticExpressions. In Eclipse you right click the appropriate project in the Package Explorer pane and choose New → Package to add a new package to your project. Name it arithmeticExpressions. When you wish to add a new expression class right click on the arithmeticExpressions package in the Package Explorer pane, and select New → Class. Eclipse will add the new class to the arithmeticExpressions package rather than the default package.
- If any of your methods contain more than one statement (except for one constructor, that contains two simple assignment statements), you probably are doing something wrong!
- You may perform some simple tests of your classes with the
client class found here:
TestExpressions.java should produce output exactly like this:
(4 + 3) = 7 (4 * (5 + 11)) = 64 25 = 25
This test program is very simple; you may add more code to it to better test your arithmetic expression classes.
For more comprehensive testing, you may use the program found in here:
This file contains code that allows a user to interactively enter arbitrarily complex arithmetic expressions involving integers with multiplication, addition, and parentheses. Once your expression classes behave correctly with TestExpressions.java, see how they behave with ArithExpressionParser.java. A sample run reveals:
Expression: 2 + 2 (2 + 2) = 4 ----------------- Expression: 2 + 3 * 4 (2 + (3 * 4)) = 14 ----------------- Expression: 2 * 3 + 4 ((2 * 3) + 4) = 10 ----------------- Expression: (25 + 5)*(10 + 2) ((25 + 5) * (10 + 2)) = 360 ----------------- Expression: 2 * ((3 + 5) + (8 + 2)) * (9 + 3) ((2 * ((3 + 5) + (8 + 2))) * (9 + 3)) = 432 -----------------
- Check out
Your finished program will be evaluated for correctness and compliance.