Skip to the content.
« Previous task Practice tasks Next task »

11. Generics in Java

1) Make a new Eclipse project called task11 or similar and create new public class Util.

2) Make a static method called getLast to which you pass a List and get back the last entry of that list. If you pass it an List of Strings, you should get back a String. If you pass it a List of Circles, you should get back a Circle. E.g.:

List<Circle> listOfCircles = ...;
Circle lastCircle = Util.getLast(listOfCircles);
List<String> wordList = ...;
String lastWord = Util.getLast(wordList);

Hint: Use a Generic Methods.

Hint: It is probably easiest to test this using Lists of String and Lists of Integer so that you don’t have to copy in any other classes, but be sure that your method does not do anything specific to String or Integer.

3) Make a second version of the method that support arrays (Java Method Overloading). That is, you should be able to call Util.getLast(someList) or Util.getLast(someArray).

List<String> wordList = ...;
String lastWord1 = Util.getLast(wordList);
String[] wordArray = ...;
String lastWord2 = Util.getLast(wordArray);

4) Make a class called Pair that stores two entries of a given type and has getter methods to retrieve them. Give it a useful toString method. Here are two examples of its use:

Pair<String> twoNames = new Pair<>("Juan", "Juanita");
System.out.printf("twoNames=%s.%n", twoNames);
String nameA = twoNames.getA(); // Value is "Juan"
String nameB = twoNames.getB(); // Value is "Juanita"
System.out.printf(" First item: %s.%n", nameA);
System.out.printf(" Second item: %s.%n", nameB);

Pair<Integer> twoNums = new Pair<>(11, 22);
System.out.printf("twoNums=%s.%n", twoNums);
Integer numA = twoNums.getA(); // Value is 11
Integer numB = twoNums.getB(); // Value is 22
System.out.printf(" First item: %s.%n", numA);
System.out.printf(" Second item: %s.%n", numB);

Hint: Use a Generic Types.

Hint: Remember that Eclipse will help you write the constructor, the getter methods, and the toString method.

Extra

Assume that you have four variables: name1, balance1, name2, and balance2. Produce output that shows the values, but line up the balances on the decimal point. Assume that the names are 7 characters or fewer and the balances are less than $10M. For example:

Juanita's bank account balance is $2,345,678.99.
   Juan's bank account balance is $   15,455.26.

Hint: Formatting Numeric Print Output.


« Previous task Practice tasks Next task »