Question: A computer science archaeologist has unearthed a high-quality Java algorithm library from the mid 1990ies. Unfortunately, Java did not support generics at that time. Before

A computer science archaeologist has unearthed a high-quality Java algorithm library from the mid 1990ies. Unfortunately, Java did not support generics at that time. Before the library can be released to the wild, it is your task to improve type-safety by converting the code to use generics. The algorithm library is implemented in class edu.uab.cis.util.Algos (see attached code).

Tasks:

Migrate the library to use Java generics (create a new class edu.uab.cs.util.Algos1).

Add a tester class edu.uab.cs.util.AlgoTester using the provided Vehicle hierarchy.

Add a class edu.uab.cis.util.AlgoTester, illustrating some tests that compile with the old library, but that would fail to compile with the new library.

My understanding is Create a new package cs (instead of cis) in the same project as the given code and convert it(given code) to generics. Then we implement a tester in the CS package and a tester in the CIS package that shows examples that will work in one but not the other. Is it right? How should I start?? Thanks in advance.

Here's the code provided:

Algo.java

package edu.uab.cis;

import java.util.ArrayList; import java.util.List; import java.util.Iterator;

public class Algo { /** * Copies all objects from src to tgt, for which the predicate pred holds. * * @param src source list * @param tgt target list * @param pred unary predicate */ static public void copyIf(List src, List tgt, UnaryPredicate pred) { for (Object obj : src) { if (pred.test(obj)) tgt.add(obj); } }

/** * Copies all objects from src to tgt that are greater than yardstick. * * @param src source * @param tgt target * @param yardstick determines if objects in src should be copied to tgt. */ static public void copyIfGreaterThan(List src, List tgt, final Comparable yardstick) { copyIf(src, tgt, new UnaryPredicate() { public boolean test(Object o) { return yardstick.compareTo(o) < 0; } }); }

/** * Finds a maximum object in lst. * * @param lst a list containing non-null references * @return a maximum object in lst */ static public Comparable findMax(List lst) { assert lst != null;

Comparable max = null; Iterator iter = lst.iterator();

// handle first element if (iter.hasNext()) max = (Comparable) iter.next();

// handle remaining elements while (iter.hasNext()) { assert max != null;

Comparable cand = (Comparable) iter.next();

if (max.compareTo(cand) < 0) max = cand; }

return max; }

/** * Adds the smaller of lhs and rhs to dst. * * @param lhs left hand side object * @param rhs right hand side object * @param dst destination list */ static public void storeMin(Comparable lhs, Comparable rhs, List dst) { Comparable min = lhs;

if (min.compareTo(rhs) > 0) min = rhs;

dst.add(min); }

/** * swaps the elements at a and b in lst. * * @param lst a list * @param a first location in lst * @param b second location in lst */ static private void swap(ArrayList objs, int a, int b) { Object tmp = objs.get(a);

objs.set(a, objs.get(b)); objs.set(b, tmp); }

/** * Sorts the elements in lst. * * @param lst an array list containing non-null references */ static public void selectionSort(ArrayList lst) { for (int i = 0; i < lst.size(); ++i) { int min = i; Comparable minobj = (Comparable) lst.get(min);

for (int j = i+1; j < lst.size(); ++j) { if (minobj.compareTo(lst.get(j)) > 0) { min = j; minobj = (Comparable) lst.get(min); } }

swap(lst, min, i); } } }

Bike.java

package edu.uab.cis;

public class Bike extends Vehicle

{

/**

* Creates a new Vehicle object.

*

* @param desc description

* @param seats max. number of people

* @param gears number of gears

*/

public Bike(String desc, int seats, int gears)

{

super(desc, seats);

this.gears = gears;

}

/**

* Compares this to other.

*

* @param other some other Bike

* @return negative iff this < other

* 0 iff this == other

* positive iff this > other

*/

public int compareTo(Object other)

{

int res = super.compareTo(other);

if (res != 0) return res;

if (other instanceof Bike)

{

Bike that = (Bike) other;

res = this.gears - that.gears;

}

return res;

}

/**

* Returns a textual description.

*

* @return a text

*/

public String toString()

{

return super.toString() + " " + gears;

}

/** gears. */

private int gears;

}

Car.java

package edu.uab.cis;

public class Car extends MotorVehicle

{

/**

* Creates a new Vehicle object.

*

* @param desc description

* @param seats max. number of people

* @param mpg gas mileage

* @param make name of make

*/

public Car(String desc, int seats, double mpg, String make)

{

super(desc, seats, mpg);

this.make = make;

}

/**

* Compares this to other.

*

* @param other some other Car

* @return negative iff this < other

* 0 iff this == other

* positive iff this > other

*/

public int compareTo(Object other)

{

int res = super.compareTo(other);

if (res != 0) return res;

if (other instanceof Car)

{

Car that = (Car) other;

res = this.make.compareTo(that.make);

}

return res;

}

/**

* Returns a textual description.

*

* @return a text

*/

public String toString()

{

return super.toString() + " " + make;

}

/** make. */

private String make;

}

MotorVehicle.java

package edu.uab.cis;

public class MotorVehicle extends Vehicle

{

/**

* Creates a new Vehicle object.

*

* @param desc description

* @param seats max. number of people

* @param mpg gas mileage

*/

public MotorVehicle(String desc, int seats, double mpg)

{

super(desc, seats);

this.mpg = mpg;

}

/**

* Compares this to other.

*

* @param other some other MotorVehicle

* @return negative iff this < other

* 0 iff this == other

* positive iff this > other

*/

public int compareTo(Object other)

{

int res = super.compareTo(other);

if (res != 0) return res;

if (other instanceof MotorVehicle)

{

MotorVehicle that = (MotorVehicle) other;

res = this.mpg.compareTo(that.mpg);

}

return res;

}

/**

* Returns a textual description.

*

* @return a text

*/

public String toString()

{

return super.toString() + " " + mpg;

}

/** mpg. */

private Double mpg;

}

UnaryPredicate.java

package edu.uab.cis;

interface UnaryPredicate

{

boolean test(Object obj);

}

Vehicle.java

package edu.uab.cis;

public class Vehicle implements Comparable

{

/** secret constructor. */

private Vehicle() {}

/**

* Creates a new Vehicle object.

*

* @param desc description

* @param seats max. number of people

*/

public Vehicle(String desc, int seats)

{

description = desc;

capacity = seats;

}

/**

* Compares this to other.

*

* @param other some other Vehicle

* @return negative iff this < other

* 0 iff this == other

* positive iff this > other

*/

public int compareTo(Object other)

{

// if the real type is different, just compare the real type

Vehicle that = (Vehicle) other;

int res = this.capacity - that.capacity;

if (res != 0) return res;

return this.description.compareTo(that.description);

}

/**

* Returns a textual description.

*

* @return a text

*/

public String toString()

{

return description + " " + capacity;

}

/** capacity. */

private int capacity;

/** description. */

private String description;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!