Assignment 7: Guide – SWE 619 – ALL – Spring 2010

 

Goal: Generics

 

Basic Assignment

 

For this assignment we are going to consider a generic method example in which type parameters are used in the return type and in the parameter. The application uses a generic method maximum to determine and return the largest of its three arguments of the same type. Unfortunately, the relational operator > cannot be used with reference types. However, it is possible to compare two objects of the same class if that class implements the generic interface Comparable< T > (package java.lang). All the type-wrapper classes for primitive types implement this interface. Like generic classes, generic interfaces enable programmers to specify, with a single interface declaration, a set of related types. Comparable< T > objects have a compareTo method.

 

Design and implement a single class call “MaximumTest” that has one method call “maximum”. The “maximum” method is a generic and must utilize the compareTo method. For this assignment test the class “MaximumTest” with the following:

 

    public static void main(String args[]){

        System.out.printf("Maximum of %d, %d and %d is %d\n\n", 3,4,5, maximum(3,4,5));

        System.out.printf("Maximum of %.1f, %.1f, and %.1f is %.1f\n\n", 6.6,8.8,7.7, maximum(6.6,8.8,7.7));

        System.out.printf("Maximum of %s, %s, and %s is %s\n", "pear", "apple", "orange", maximum("pear", "apple", "orange"));

    } //end main

 

Hint: The implementation is very small, less than 20 lines of Java code, however, it requires using generics with Comparable and compareTo.

 

 

For this assignment you will need to submit the following:

 

·         Implementation of MaximumTest.java – will only be submitting one java file [5 points]

·         Output file for test data; [1 points]

·         Implementation of JUnit files; [2 points] and

·         Provide a text document that describes the abstract function and rep invariant for MaximumTest. [2points]