Assignment 1.2
Assignment 1.2
Practical exercises
Question 1. Write a Java program to implement a stack of integer values with the following
operations:
1. boolean isEmpty() - return true if the stack is empty and false otherwise.
2. void clear() - clear the stack.
3. void push(int x) - insert a node with value x at the top of the stack.
4. int pop() - remove the top element on the stack and return it's value; throw
EmptyStackException for empty stack.
5. int top() - return value of a node at the top of the stack; throw EmptyStackException
for empty stack.
6. void traverse() - display all values in the stack from the top to the bottom.
7. Use a stack to convert an integer number in decimal system to binary system and display on
the screen.
Note: You can write some constructors and other methods as you see they are necessary.
Question 2. Write a Java program to implement a queue of integer values with the following
operations:
1. boolean isEmpty() - return true if the queue is empty and false otherwise.
2. void clear() - clear the queue.
3. void enqueue(int x) - insert a node with value x at the end of the queue.
4. int dequeue() - remove the first element on the queue and return it's value; throw Exception
for empty queue.
5. int first() - return value of the first node of the queue; throw Exception for empty queue.
4. void traverse() - display all values in the queue from the front to the rear.
6. Use a queue to convert a real number less than 1 in decimal system to binary system
and display on the screen.
Note: You can write some constructors and other methods as you see they are necessary.