Overview Schedule Announcements Resources Assignments Home

CSC 121: Computer Science I, Spring 2005

Lab 6

Laboratory 6 - Collections and Project 2

Goals:
To gain experience with collections and stacks. To complete more functionality of project 2.
Getting started:
The Steps of the Lab:
  1. Open the stackitem project.
    Read In project 2, the calculator we will finish building needs the ability to remember several numbers and operators like +, - and *. We want to remember these items in one place. We call that place a stack, because of the order in which we add and remove items to and from it. Because the stack needs to be able to store two different kinds of things, we created a class called StackItem for this purpose. A StackItem object can be used to represent either a number or an operator (+, - , *, /, etc.). In the next few steps you will create and manipulate a few StackItem objects.
    Do

    Create two StackItem objects, one a number (int), 45 for example, and another an operator (char), '+' for example. Now examine and invoke some of the methods for each object. What are the results of isInt() when applied to each? What is the result of applying isOperator()? What is returned by getCharItem()? By getIntItem()? Does it make sense to apply getIntItem to a StackItem, when its isInt() method returns the value false?

    Create a Control object. Invoke its makeIntItem(int newIntValue) method. Now invoke the getStackItem() method. A window with the result will pop up. Inspect the object by clicking on the inspect button. You should see a familiar number among the items displayed.

    Complete writing the makeCharItem(char newCharValue) method in the Control class. Test your methods by creating a Control object, invoking your makeCharItem method and then the getStackItem() method. Are the results what you expected?

    Close the StackItem project and go to the next step of this lab.

  2. Open the stack project.
    Do Create a Control object. Invoke the addInt() method a few times. Each time you do, a new item will be added to a stack that the Control class uses. Now invoke the displayStack method. In what order are the items displayed?
    Complete

    The Stack class is missing a complete pop method. Pop removes an item from the end (top) of a stack. Complete the pop method in the Stack class. Just add the statement:

        item = (StackItem) theStack.remove(theStack.size() - 1);
                       
    

    at the appropriate place in the partially written pop method.

    Note: Since an ArrayList can store different kinds objects, we are required to specify the kind of object we expect to remove. That is why (StackItem) is in front of theStack.remove(theStack.size()-1); in the statement above. Note also that size() returns the number of objects in an ArrayList and since each object in the list has an index (position number) with the first beginning at index 0, the last item is always at size()-1.

    Test your pop method by adding (push) some ints, displaying the stack, invoking removeStack and again displaying the stack.

    Practice

    Practice using the Stack class by writing a Control class method to push the integers from 1 to 10 onto myStack

    Write a method called addChar in the control class to add a character to myStack; use the addInt method as a model. Test your method by adding '-', '+', etc. and then invoking displayStack().

  3. Open the calculator project.
      This is the almost-complete version of project 2. You will be completing it over the coming days. A description of the project is available now. The calculator project you are being given needs several enhancements to be complete. You will complete three of them today.
    Today Complete the methods push, isEmpty, and makeEmpty in the Stack class. Test your methods by creating a calculator object and using it to evaluate several expressions. Note: Using a while loop, a stack will finally be empty when its size() is 0.
Overview Schedule Announcements Resources Assignments Home

Valid HTML 4.01!Valid CSS!DePauw University , Computer Science Department , Spring 2005
Maintained by Brian Howard ( bhoward@depauw.edu ). Last updated