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 {
		BufferedReader in = new BufferedReader
		    (new InputStreamReader(System.in));
		// The code below reads two integers, n and m
		// prompt the user:
		System.out.print("Please enter an integer: ");
		// read a line of input:
		String line = in.readLine();
		// extract the integer from the entered data:
		int n = Integer.parseInt(line);
		
		// prompt the user again:
		System.out.print("Please enter another integer: ");
		// read another line of input:
		line = in.readLine();
		// 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: Submit the program with all your test cases and results.

Problem 3

For this and remaining problems you need to download the file CharStack.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: Write all your test cases and results in comments.

*Problem 6* (extra credit)

Remove all occurrences of letter 'a' from a stack. The rest of the elements should appear on the stack in the same order as originally. Print the stack at the end of the program to test. Make sure to test the following cases (among others): the stack doesn't have any letters 'a', the stack has only letters 'a', the stack is empty. Write all your test cases and results in comments.
This is a lab from CSci 2101 course.