Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
John Michael
1,239 PointsJava Generics
Hello everyone, I am in the process of learning about generics in Java. I have been working on converting some code from using primitive types to generics and was hoping someone could help me.
There are 4 java classes I am wanting to convert. I have started working on Algo.java but I have gotten a little lost. Then, I am unsure how to go about converting Vehicle.java and Bike.java.
Vehicle.java
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;
}
Bike.java
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;
}
Algo.java
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
<E, F extends E>
void copyIf(List<F> src, List<E> tgt, UnaryPredicate pred)
{
for (E 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 <E> void copyIfGreaterThan(List<E> src, List<? super E> tgt, final Comparable<? super E> 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<E extends Comparable<? super E>>
E findMax(List<E> lst)
{
assert lst != null;
// handle first element
if (lst.size() == 0) return null;
E max = lst.get(0);
// handle remaining elements
for (E obj:lst)
{
if (max.compareTo(obj) < 0)
max = obj;
}
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<E extends Comparable<? super E>>
void storeMin(Comparable<?> lhs, Comparable<?> rhs, List<E> dst)
{
Comparable<? super E> 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<E extends Comparable<? super E>>
void selectionSort(ArrayList<E> lst)
{
for (int i = 0; i < lst.size(); ++i)
{
int min = i;
Comparable<? super E> minobj = lst.get(min);
for (int j = i+1; j < lst.size(); ++j)
{
if (minobj.compareTo(lst.get(j)) > 0)
{
min = j;
minobj = lst.get(min);
}
}
swap(lst, min, i);
}
}
}
UnaryPredicate.java
interface UnaryPredicate<E>
{
boolean test(E obj);
}
For the above code, I am not wanting the generic annotations to be too narrow. For example, source and target containers may use different types (e.g. I can copy cars to a list of vehicles).
P.S I know that code needs to be added to the UnaryPredicate Interface, I have not gotten to that yet :)
Thank you to anyone that helps me with the above code sample and I apologize for the length of it!