Review 4

After writing each of the classes below, and before moving onto the next class, write a driver that tests your code.  And stress test it.  Don’t just write a few lines to test it, try to break your own code.

1.  Write a class named Stack that implements the StackInterface interface as described in Exam 3.  Feel free to use the code in the Exam 3 answer key.  If you use my code, you’ll need to implement the FullStackException class.  A quick solution is provided below.  Write a driver and test that the Stack class works properly before continuing.

public class FullStackException extends RuntimeException { }

Then augment the Stack class so that it implements the Iterable<Object> interface.  The iterator method should return an Iterator<Object> object that iterates over the stack’s array, from the last object added to the first object added.

2. Write a class named Matrix that satisfies the following requirements:

  • The class should store integers (primitive) as an array of arrays.
  • The class should have a constructor that has 2 integer parameters representing the number of rows and columns (m,n) of the matrix respectively.
  • The class should have have a method named set that takes a variable number of int[] arguments which hold the rows of the matrix.  If the number of arguments passed into the method is not equal to m, an IllegalArgumentException exception should be thrown.  If the lengths of each of the arrays that are passed into the method is not equal to n, an IllegalArgumentException exception should be thrown.
  • The class should implement the Iterable<Integer> interface. The iterator method should return an Iterator<Integer> object that can be used to iterate over all of the elements in the matrix, row by row, from left to right.
  • The class should have a method named getDiagonal that returns an Iterator<Integer> object that can be used to iterate over the elements on the diagonal from (0,0) to the (m,n).

3. Write a class named Student that satisfies the following requirements.

  • The class should have a constructor that takes a first name (String) and a last name (String).
  • The class should override toString to print all of the values of the fields.
  • The class should implement the Comparable<Student> interface.   The compareTo method should order the Students objects as follows.
    • For all Students a and b:
      • If a.lastName comes before b.lastName alphabetically, then a < b.
      • If a.lastName and b.lastName are the same and a.firstName comes before b.firstName alphabetically, then a < b.
      • Otherwise a and b are equal.

4. Write a class named Person that satisfies the following requirements.

  • The class should have a constructor that takes a first name (String) and a last name (String).
  • The class should override toString to print all of the values of the fields.
  • The class should implement the Comparable<Person> interface.   The compareTo method should order the Person objects as follows.
    • For all Students a and b:
      • If the total number of letters in a’s first and last name is less than the total number of letter’s in b’s first and last name, then a < b.
      • Otherwise a and b are equal.

5. Augment the Person class so that it implements Serializable.  Set serialVersionUID to 1000L.  Write a driver that creates a serialized instance of the class.  Then email the instance to a peer in class.  Write a second driver that reads a serialized instance of the Person class and prints to the screen the name of the person.

© 2018 – 2019, Eric. All rights reserved.