University of Ottawa - SITE
CSI1102 (Winter 2005)
A 4 – Solution
Question 1 :
import
cs1.Keyboard;
public class
Backwards
{
// Takes a string
of characters from the keyboard and
// prints it
reversed
public static void main(String[] args)
{
String msg;
System.out.print("Input a string of characters:
");
msg = Keyboard.readString();
System.out.print("\nThe reversed
string is: ");
printBackwards(msg);
System.out.println();
}
// recursively reverse a string of characters
public static void printBackwards(String s)
{
if (s.length() != 0)
{
int LastIndex = s.length()-1;
System.out.print(s.charAt(LastIndex));
printBackwards(s.substring(0,LastIndex));
}
}
}
Questions 2 and 3:
import
java.util.Stack;
public
class StackTest
{
public static void main(String[] args)
{
// declares and creates a stack
Stack st = new Stack();
// adds the integers to the stack
for (int i=0; i<10; i++){
Integer I = new Integer(i);
st.push(I);
}
Integer I = new Integer(5);
st.push(I);
try
{
// prints the elements of the stack
System.out.println("The stack
...");
printStack(st);
// Reverse
the stack
Stack reversedS = reverseStack(st);
System.out.println("\nReversed
stack");
printStack(reversedS);
// print the elements of the original stack
System.out.println("The
initial stack ...");
printStack (st);
// remove all the occurrences of the value 5
Stack newSt
= removeElement(st, 5);
// print the elements of the initial stack and
of the
// new stack
System.out.println("\nThe
initial stack ...");
printStack(st);
System.out.println("The
stack after removing the 5s ...");
printStack(newSt);
}
catch(EmptyStackException esExc) {
System.out.println(esExc.getMessage());
}
// print empty stack (Exception)
try {
Stack newSt
= new Stack();
System.out.println("\nPrint an
empty stack ...");
printStack(newSt);
}
catch(EmptyStackException esExc) {
System.out.println(esExc.getMessage());
}
}
// Print the elements of the stack from the top to the
bottom
public
static void printStack(Stack s) throws EmptyStackException
{
String errmsg = "Exception:
if (s.empty()) {
EmptyStackException esExc
=
new EmptyStackException(errmsg);
throw esExc;
}
Stack temp
= new Stack();
while (!s.empty())
{
Object val = s.pop();
temp.push(val);
System.out.print (val + "
");
}
System.out.println();
// restore the initial stack
while
(!temp.empty())
{
Object val = temp.pop();
s.push(val);
}
}
// Return a new stack with the
same elements as
// s but in reversed order
public static Stack reverseStack (Stack s)
{
Stack reversedS = new Stack();
Stack temp = new Stack();
while (!s.empty())
{
Object val = s.pop();
reversedS.push(val);
temp.push(val);
}
// restore s
while (!temp.empty())
{
s.push( temp.pop() );
}
return reversedS;
}
// Return a new stack with the same elements as
// s except that the occurrences of val are
removed
public static Stack removeElement (Stack s,
int val)
{
Stack newSt = new Stack();
Stack temp = new Stack();
// remove
the elements
from the top of s and add them to temp
while (!s.empty())
{
temp.push(s.pop());
}
// remove the elements of temp,
add them to s, than add them
//
to the new stack except the ones equal to val
while (!temp.empty())
{
Integer element = (Integer)
temp.pop();
s.push(element);
if (element.intValue() != val)
newSt.push(element);
}
return newSt;
}
}
class EmptyStackException extends Exception
{
// the constructor
initializes the exception with a specific message
EmptyStackException (String message)
{
super(message);
}
}