Sale!

WIA1002/WIB1002 Lab 7 solution

$30.00 $25.50

Category:

Description

5/5 - (4 votes)

Stack and Problem Solving with Stacks

Question 1
a) Create a generic stack called MyStack class using ArrayList. The MyStack generic class
should implement the following methods :
1) public void push(E 0);
2) public E pop();
3) public E peek();
4) public int getSize();
5) public boolean isEmpty();
6) public String toString();
7) public boolean search(E o);
b) Write a test program for Q1(a) called TestMyStack class. Create a stack of type Character
from MyStack.
1) Following the given order, add elements, a b and c in the stack.
2) Print the stack.
3) Check if element ‘b’ is in the stack.
4) Check if element ‘k’ is in the stack.
c) In the same test program as Q1(b), create a second stack of type integer.
1) Following the given order, add elements, 1 2 and 3 in the stack.
2) Print the stack.
3) Check if element ‘6’ is in the stack.
Question 2
Write a new test program for Q1(a) called TestIntMyStack class.
a) Prompt user to enter an integer value.
b) Push the values 1 through the user entered value onto the stack.
c) Print the size of the stack.
d) Display the contents of the stack by repeatedly calling pop() until the stack is empty.
What is the output of the elements? What is the order, why?
Page 1
WIA1002/WIB1002 Lab 7 Sem. 2,
2018/2019
Question 3
Assuming that an unknown number of positive integers are stored in a stack, S. Using only stack
ADT operations write an algorithm/function to find the sum of every element in S.
Question 4
A string is a palindrome if it spells the same way forwards and backwards. Write a Java program
that uses a Stack to determine if a string is a palindrome or not. Let your string be of maximum
size 15 characters. Note however that the actual size may be 15 or less.
Question 5
The Tower of Hanoi is a mathematical game or puzzle. It consists of three rods, and a number of
disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat
stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following
rules:
i. Only one disk may be moved at a time.
ii. Each move consists of taking the upper disk from one of the rods and sliding it onto
another rod, on top of the other disks that may already be present on that rod.
iii. No disk may be placed on top of a smaller disk.
You are required to write the source code using Java syntax to solve Tower of Hanoi Problem
using stacks data structure.
Page 2