Review 5

Last review, I noticed some of you would write the classes but not write a driver to test the classes that you wrote.  Write a driver that stress tests your class.  Don’t skip this step.  It’s pointless (and silly) to write a class and not verify that it actually works!

1. Write a generic class named Stack that satisfies the following requirements.

  • The class uses an array to store elements.
  • The class has a constructor that has no parameter. When the constructor is called the array is initialized to hold 10 elements.
  • The class includes a method named push that has a parameterized type as a parameter and returns void.  The push method adds the element passed into the method to the top of the stack. If no room is available for the element a FullStackException exeception is thrown.  Provide your own FullStackException class as shown in Review 4.
  • The class includes a  method named pop that has no parameters and returns an object having the parameterized type. The pop method removes and returns the top element on the stack. If the stack is empty an EmptyStackException exception is thrown.

2. Write a non-generic class named ArrayUtil that has the following generic methods.

  • A static method named toList that takes a variable number of arguments of a generic type as arguments and returns an ArrayList containing the arguments passed into the method.
  • A static method named toArray that takes an ArrayList containing a elements of a generic type and returns an Object[] containing the elements in the ArrayList.

3. Write a generic class named Pair that satisfies the following requirements.

  • The class has a constructor that takes two elements of the generic type and stores them internally.
  • The class has a method named getX() which returns the value of the first element passed to the constructor.
  • The class has a method named getY() which returns the value of the second element passed to the constructor.
  • The class has a method named toString which returns a string of the form “(x,y)” where x and y are string representations of the values passed into the constructor.
  • The class overrides equals and hashCode.

4. Add the following methods to the ArrayUtil class.

  • A non-generic static method named min that using wildcards takes an ArrayList of objects whose type extends Number and returns a double holding the smallest value in the ArrayList.
  • A non-generic static method named print that using wildcards takes an ArrayList of objects and prints a string representation of each object to the screen.

Review 6

  1. Consider the following interface.
interface Num {
    int op(int a, int b);
}
  • Write a program that includes an inner class that implements Num and a main method that creates an instance of the inner class and prints the result of op when passed the values 2 and 3.
  • Write a program that has a main method which contains an instance of an anonymous class that implements the interface and that prints the result of op when passed the values 2 and 3.
  • Write a program that has a main method which contains an instance of the interface using a lambda expression and that prints the result of op when passed the values 2 and 3.
  1. Use Streams to solve the following problems
    • Given an ArrayList of Strings, filter out the elements that are over 5 characters long and print them to the console.
    • Given an array of integers, filter out the evens ones, and print to the screen the number of remaining elements.
    • Given an ArrayList of Doubles, round each value to a integer, and print the values to the screen.
    • Create a class named Student with 2 String fields: firstName and lastName, with a constructor that sets both fields and getter methods.
      • Given an ArrayList of Students, print the unique last names in the list to the console.
    • Given an ArrayList of Strings, filter out the words that start with letters A-M then, sort them lexicographically, and print them to the screen.
    • Give an ArrayList of Strings, convert to an IntStream based on the hash code of each string, and print the values to the screen.
    • Given an array of String, filter out the duplicates, then convert each String to an integer representing its length, sort the numbers, and print the values to the screen.
    • Given an array of integers, filter out the odd numbers, keep only the values between 5 and 100, then print the number of elements left in the stream.
    • Given an ArrayList of integers, convert them to hex strings, then sort them lexicographically, then convert them back to integers, and print them to the screen.
    • Given a 2D array of strings, remove all strings with 5 or more characters, and convert back to an 2D array.
    • Given an array of integers, print to the screen the sum of the numbers divisible by 3.