Text Excel B Student Guide
Text Excel B Student Guide
Introduction
In Part A we laid the foundation for a fully-functional spreadsheet program. Now, we get
to make it sing. Your starting point is the Text Excel A you completed; you will continue
to add more code to this project to evaluate formulas.
Getting Started
You must incorporate the new TextExcel B tests into your project. You may do this in
one of two ways.
The easy way: Start with the TextExcel you submitted in Q2. Then just grab
TestsALL.java from the Brightspace Dropbox, and copy/paste its contents into the
TestsALL.java in your project.
The hard way: You probably only need to do this if you fear your prior project’s build
path or other settings got messed up somehow, and you want to create a clean new
project to start with. Rename your old TextExcel project you submitted in Q2 to
something else. Import the new TextExcel_B starter project. It already comes with the
new TestsALL.java file. So just copy your .java files from your old TextExcel A project
into this new project. Note: The TextExcel_B starter project does not contain any
TextExcel_A code. It is your responsibility to continue to using the code you created
first semester.
Tests
When you run tests, you will see two new categories: B_Checkpoint1 and B_Final.
B_Checkpoint1 must pass for your checkpoint 1 submission, and both B_Checkpoint1
and B_Final must pass for your final submission. Of course, all the “A_” tests must
continue to pass in all B submissions.
There are also some new extra credit tests. All extra credit tests are optional, and are
only relevant if you are attempting the extra credit.
Formulas
In Part A, you created FormulaCells, which would print their expression during cell
inspection (fullCellText()), but which could print anything inside the spreadsheet itself
(abbreviatedCellText()). For Part B, you will fix abbreviatedCellText so that it will call
getDoubleValue() (on the FormulaCell), which will actually evaluate the expression.
getDoubleValue
Arithmetic Formulas
The formula may be an arithmetic formula, as in
A1 = ( 1 + B5 + 3 )
An arithmetic formula is an expression involving real (Java’s double) constants, cell
references, and the operators +, -, *, and /. Numbers in formulas can only be doubles
(not fractions). Order of operations may simply be left-to-right (no operator precedence)
for full credit. If you are feeling brave, you may instead follow standard operator
precedence rules (first */ and then + -) for extra-credit. However, we strongly
recommend you get left-to-right working first, and make a safe copy of that project
before attempting any extra credit.
If a formula contains a cell inside it, that is called a “cell reference”, because that is how
the formula “references” another cell. For example, consider these commands:
B5 = 1
A1 = ( 1 + B5 + 3 )
When a cell that is referenced by a formula is changed, the change will affect the result
displayed in the formula’s cell. In the above example, A1 is displayed as 5.0. However,
if the user later types this:
B5 = 1.5
then A1 would automatically change to display itself as 5.5. Circular references with
formulas are not permitted and will not be tested (except for the circular reference extra
credit).
Method Formulas
A formula may also be a method formula, as in
A1 = ( AVG A2-A5 )
A method formula can only contain one method (either SUM or AVG) and cannot
contain arithmetic operations. The formula includes a cell range, which is a rectangular
block of cells on which the method is calculated. For example, cell range B5-E7 has
been highlighted as an illustration:
|A |B |C |D |E |F
1 |3% | | | | |
2 | | |hello | | |
3 | | |2.0 |4.0 | |3.0
4 | | | | | |
5 | | | | | |
6 | | | | | |
7 | | | | | |
As shown above, a cell range contains two cell names, separated with a dash (‘-‘). The
two cells identify opposite corners of a rectangle. The first cell will always be the upper-
left corner, and the second cell will always be the lower-right corner.
Examples
Input Example Action
B3 = ( 4 * 6 + 3 ) Assigns formula to cell B3, which displays 27.0 when evaluated.
A9 = ( B3 * 6 + 3 ) Assigns formula to cell A9, which when evaluated retrieves the
value of cell B3, multiplies the retrieved value by 6, and adds 3.
A1 = 5% A2’s getDoubleValue will need to call A1’s getDoubleValue()
A2 = ( 50 + A1 ) which will return 0.05. Therefore, A2’s getDoubleValue returns
50.05 (because it is 50 + 0.05).
A1 = ( B5 + 3 ) Notice that A1 is a formula, and it references B5, which is also a
B5 = ( C2 * 2 ) formula. This is allowed: formulas may reference formulas
C2 = 4 which reference other formulas, and so on (though we will not
test circular references).
When you parse formulas entered by the user, note that formulas always start with a left
parenthesis followed by a space, and that all operators and operands are separated from
each other with a space, and that the formula ends with a space and then a right
parenthesis. Although the formula is surrounded by parentheses, we will not allow
parentheses inside the formula, and they will not be tested.
Checkpoints
Note that the checkpoints are cumulative. When you submit each checkpoint, the
submitted program should pass the details in the current checkpoint as well as all
objectives in previous checkpoints. All TextExcel_A tests must continue to pass for both
the checkpoint and final submission.
Note: Do not store the result of evaluating the expression into a field to use for later.
(That would set you up for failure with the final submission.) Instead, you should
evaluate the expression each time getDoubleValue is called.
For this checkpoint, you may assume that the expression stored in the FormulaCell does
NOT have cell references. For example, you will need to evaluate a formula like: ( 4 - 5 *
2 / 4 ), but not like ( 4 - A1 * 3 ) or ( AVG A2:A5 ).
Remember that formulas can be as long as the user likes. Unlike FracCalc, which only
allowed one operator, there could theoretically be 50 or 100 or even more operators in a
formula.
Hint:
Use the split method on the String class to help you parse your formulas.
A1 = ( A2 + 1 ) and A2 = ( A1 + 1 ).
Furthermore, the AVG and SUM methods need to work, such as L14 = ( SUM B6-C12 )
and C12 = ( AVG A1-A5 )
To complete this submission, you will finish your FormulaCell class’s getDoubleValue()
method to support these formulas.
Hints
Believe it or not, there is not much more code required to support arithmetic formulas
that refer to other cells (e.g. ( A1 + 2 * A2 / A3 )). When you encounter a cell identifier
(such as A1), you simply need to get the value of the cell referred to by A1. To do this,
you will need to ask your spreadsheet to give you the Cell at that location (using the
Spreadsheet’s getCell method), cast the resulting Cell to a RealCell, and call its
getDoubleValue method to get the cell’s value.
If after you finish supporting constant-valued formulas, you find that you are writing
radically different (or long) code to handle formulas that refer to other cells, you are
likely doing it wrong. Please ask for help in class or in tutorial.
For FormulaCells, do not store the calculated formula result in the FormulaCell as a field.
Instead, every time your getDoubleValue() method is called, it should parse that string,
calculate the double value (whether it is a single value or formula), and return it. Please
trust us on this – students who do not follow this hint will likely spend much more time
getting their program to work correctly.
The reason for this is because for formula cells, the resulting value may change if other
Your final submission should include any extra credit features you completed.
Extra Credit
There are several opportunities for extra credit. You can attempt any or all of them. It is
recommended that you do the rest of the project first, and that you save a version of your
project without the extra credit (in case doing the extra credit makes the rest of your
program not work correctly).
If the user enters an invalid command, an error (e.g. “ERROR: Invalid command.”)
should be printed to the display (and returned from processCommand()), no other action
should be taken, and the program should wait for the next command. Make sure to
consider all cases such as a user entering a command that does not exist, entering a cell
that is outside of the spreadsheet, making a syntactic error in a formula, or otherwise not
following the specified command format.
You do not have to handle well-formed formulas that refer to non-real-valued cells.
(That is addressed in the “Evaluation error handling” extra credit below.)
You must handle evaluation errors that are introduced or resolved by updating the values
in transitively referenced cells. For example, if A1 = 3, A2 = ( A1 + 1 ), A3 = ( A2 + 2 ),
the values of A1, A2, and A3 would be 3, 4, and 6 respectively. The user should then be
able to do something like A1 = “hello” to result in getting hello in A1, #ERROR in A2,
and #ERROR in A3. Then, if the user does A1 = 0 you should get 0 in A1, 1 in A2, and
3 in A3.
Rubric
Requirement Worth
TextExcel_A Functionality 5
Checkpoint 1 10
Total 50