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.