Lab 4: Stacks


Lab Exercise:

Implement the following classes:

Class Course that includes three instance variables:           

            private int ID;             // course ID

            private String Name; // course name

            private Course next;   // link next

Your class should have the following:

A constructor that initializes the two instance variables id and name.

Set and get methods for each instance variable.


class Department that includes two instance variables:

            private String deptName;

            private Course top;


Your class should have the following:

a constructor that initializes the department name

public boolean exist(int id)that checks whether the course object with id passed as parameter exists in the stack or not.

Public Boolean push(int id, String n)that creates and adds the course if the course with id passed as parameter does not exist in the stack. The course must be added to the top

private void push(Course temp) that inserts a node given as a parameter into the stack.

Public Course pop()removes the top element from the stack if it exists and returns it to main; otherwise, return null.

Public String findCourse(int id) that returns a course name if the id exists in the stack.

Public void print()that prints the department name and all the courses in the stack.

public void print Reverse()that prints the department name and all the courses in the stack in reversed order.


Write a test application named Lab4Test. In the main method, do the following:

Input the department name then create a department object.

Display a menu to the user and asking for a choice to be entered. As follows:

The program can perform the following:

1- push a course

  2- pop a course

  3- search for a course

  4- print stack

  5- print stack in reverse

  6- exit

Please enter your selection:

The program will perform the action selected by the user and display a proper message when necessary.

The program will repeat these actions until the user terminates the program (Hint: Use a loop).



    • 5 years ago
    A+ Work

    Purchase the answer to view it

    • attachment