Java Lab #7: Clacky v1.0

In this lab you will enhance Clacky to make it into a servicable calculator that can operate on integers of any size. There are three parts to this lab which all entail modifing and integrating code from previous labs. This is very realistic. Many programmers spend a lot of time working with existing code to enhance it, fix it, or otherwise adjust it.

The starting point for this lab is the Clacky v0.1 code in Lab #5 together with the StackOperations in Lab #6. I recommend you create a new project for this lab. Create a package edu.vtc.clacky and put your code from Labs #5 and #6 into that package (you may have to add a package declaration to the top of each file).

Revise the way the stack is printed so each value is on a line by itself. For example:

      0
      0
      42
      7
      > -
    

The above shows the output with two numbers on the stack. Stack level 0 is at the bottom. The command line shows a minus operator as if the user is about to compute 42 - 7.

Proceed as follows

  1. In Clacky replace the array of integers you used for the stack with an ArrayList<Integer>. Let the ArrayList be initially empty. Modify the program so it works again. Don't forget that you need to use the get and set methods to access an ArrayList.

    This change will actually simplify the code in some ways. Since the add method of ArrayList actually inserts an item you can eliminate some of the loops you wrote for Lab #5. Similarly the remove method compacts the ArrayList for you, again allowing you to remove some loops. Notice also that remove returns the item it removes which you may find convenient.

    Go ahead an implement all four of the operations if you haven't done so already. They should be quite easy once you have the ArrayList in place.

    This change allows Clacky to work with stacks of any height. Try it!

  2. Add some new commands to manipulate the stack. For example add a "dup" command that runs the StackOperations.dup method on Clacky's stack. Notice that since the method is a static method in another class you have to prefix its name with the name of the class (StackOperations in this case). Adding a command is a matter of adding another case in the switch statement that handles words on the command line. (Don't forget the break!)

    For the operations that require an integer, first pop a value off the stack and use that as the integer controlling the stack operation. By "pop" I mean take the value from stack level 0 and remove it from the stack. To illustrate suppose you have:

            14
            13
            12
            11
            10
            > 3 rollup
          

    Here the user is using the "rollup" command. Notice that first the 3 is pushed onto stack level zero as usual and then immediately removed by "rollup." The result is:

            14
            12
            11
            10
            13
            >
          

    You can see why this is called "roll UP" since the stack moves upward in the picture. Numbering the first stack level as level zero might be a little counter-intuitive to some users (it appears that four levels were involved in the "3 rollup" command above) but don't worry about that too much right now.

    It occurs to me that I forgot the "swap" stack operation. It would just exchange stack levels 0 and 1 (the bottom two levels in the pictures above). It's very useful. Although not a priority, if you feel inclined to implemented it please do so. Add a method to StackOperations that does swap and an associated command to Clacky. Note that "1 rollup" is the same thing as swap, just more verbose for the user to type.

    These stack manipulation commands allow the Clacky user to move numbers around to more convenient locations while calculating.

  3. Instead of using an ArrayList of integers change Clacky to use an ArrayList of BigInteger. The BigInteger class is in the java.math package so you'll need to add:

            import java.math.BigInteger;
          

    to the top of your files. Unlike normal integers BigIntegers require that you use various methods to manipulate them. See the JDK documentation on BigInteger for the details. Also refer to notes on the Rational class developed during the lecture. The approach to using BigInteger is similar.

    You will also have to change StackOperations to take an ArrayList of BigInteger.

    Clacky is now a fully operational four function calculator that can compute effectively on integers of any size. Try it!

What does the following Clacky command line compute?

      > 2 dup * dup * dup * dup *
    

Try it! If you aren't sure what it's doing, try entering the command line in one word at a time to see the effect each word has on the stack separately.

As a final optional step, consider exporting your program to an executable jar file as I demonstrated in the lecture. This would allow you to use Clacky outside of Eclipse and makes the program much more practical. Email it to your friends! Turn in your final program with appropriate comments. I'm mostly interested in just the Clacky.java file. I don't need to see your modified StackOperations.java. Be sure your name is on your submission.

P.S. If you start using Clacky much you will most likely immediately find things you want to do with it that don't work. For example an ability to clear the stack or remove and throw away the last item ("drop") might be nice. How about an ability to raise numbers to powers? I invite you to reflect on features like these, or others you might want. YOU are the programmer so YOU can make them happen. You are no longer at the mercy of what programs allow you to do. You can shape your programs to do your bidding.

Enjoy!