## CSci 2101 Data Structures: Lab 2

### Problem 1

Copy/paste the following program in the file LabTwoProbOne.java, add code to print out the two numbers in a non-decreasing order (i.e. if one of them is smaller than the other, then that one will be printed first, otherwise they can be printed in any order. Use if/else statement to compare the two numbers.
``````
import java.io.*;

public class LabTwoProbOne {
public static void main(String [] args) throws IOException {
// The code below reads two integers, n and m
// prompt the user:
// read a line of input:
// extract the integer from the entered data:
int n = Integer.parseInt(line);

// prompt the user again:
// read another line of input:
// extract the integer from the entered data:
int m = Integer.parseInt(line);

// print the two integers in a non-decreasing order
}
}

``````

### Problem 2: Comparison of characters

Java allows you to compare two characters (i.e. variables of type `char`) using == and also using <, >, <=, etc. Write a program to check each of the following:
• Is 'a' equal to 'A'?
• Which character is "less": 'a' or 'z'? 'A' or 'Z'? 'a' or 'Z'? 'A' or 'z'? Write down your results in comments.
• Is it legal to compare a letter to a digit (such as '5')? If yes, which one is "less"?
• Is it legal to compare a letter to a punctuation mark (such as '?')? If yes, which one is "less"?
Submit the program with all your test cases and results.

### Problem 3

For this and remaining problems you need to download the files CharStack.class and StackException.class into your current working directory. Write a program to perform the following operations (in this order). It is convenient to write the operations in comments first, and then fill in the code.
1. Create a stack.
2. Push a few elements on the stack (you may leave the stack empty as one of your test cases).
3. Check if the stack is empty. If it is, print the message "The stack is empty"
4. Otherwise (i.e. if the stack is not empty) pop the top element of the stack and check if it's the character 'A'. If it is, print "Yes", otherwise print "No"
5. Check if the stack is empty. If it is, print out "The stack is now empty", otherwise print out "The stack is still not empty"
Change the elements that you push onto the stack in part 2 to test all possible cases. Write the test data and the results in comments.

### Problem 4

Use a `while` loop to print out the stack. More precisely, write a program to do the following (in this order):
1. Create a stack.
2. Push a few elements on the stack (you may leave the stack empty as one of your test cases).
3. In a while loop, keep popping elements off the stack. Print out each element. The loop should keep going until the stack becomes empty. If the stack was empty to begin with, nothing should be printed.
At the end of the program the stack should be empty. Write all your test cases and results in comments.

### Problem 5

Print out the stack and put it back together. At the end of the program the stack should have exactly the same elements as it had after you pushed all the elements on the stack. You will need:
• Another stack.
• Another while loop to put the elements back onto the stack.