CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades | Students

Lab #10 (Exercise 11-3)

Create a stack

Due Date: Tuesday of Week 4
10 points

In this lab, you'll create a class called genericStack that uses a linked list to implement a stack, which is a collection that lets you access entries on a first-in, last-out basis. Then, you'll create another class that uses the GenericStack class. the GenericStack class should implement these methods:

Method Description
push(element) Adds an elemnt to the top of the stack.
pop() Returns and removes the element at the top of the stack.
peek() Returns but does not remove the element at the top of the stack.
size() Returns the number of entries in the stack.

    Create the GenericStack class

  1. Start a new class named GenericStack that specifies a type variable that provides for generics.

  2. Declare a linked list that will hold the elements in the stack. Then, use the linked list to implement the methods shown above.

  3. Compile the class.
  4. Create a class that uses the GenericStack class


  5. Open the GenericStackApp class:

  6. Declare a generic stack at the beginning of the main method that will store String objects.

  7. Add code to the main method that uses the push method to add at least three items to the stack. After each item is added, display its value at the console (you'll need to use a string literal to do this). Then, use the peek method to return the first item and display that item, and use the size method to return the nmber of items in the stack and display that value. Next, use the pop method to return each item, displaying it as it's returned. Finally, use the size method to return the number of items again and display that value.

  8. Compile and run the class. If it works correctly, your output should look something like shown above.
Include in your program the following comments:
  • Name of program
  • Team's name with all the names of the members (e.g. Team A: John Doe, Jane Smith, etc.)
  • Current Date
  • Computer system and compiler you are using
  • Brief description of the program

Compile and run the Java program.

Take a screen capture by hitting simultaneously the keys Alt and Print Screen (Prt Sc) of the output results (console) and paste it in MS Paint. Save it as a gif file.

When you are ready, upload the java program and the gif file by clicking the link below:

Upload Lab

I cannot grade if I don't get the files and receive your email.