/** * @param args the command linearguments */ public static void main(String[]
ID: 3618538 • Letter: #
Question
/**
* @param args the command linearguments
*/
public static void main(String[] args)
{
System.out.println("max = "+ min(new Product(1, "de"), newProduct(2, "bc")));
}
public static <T extends Comparable > Tmin(T a, T b)
{
if(a.compareTo(b) < 0)
returnb;
return a;
}
public static <T> booleanduplicate(List<T> coll)
{
int count;
for(T i1 : coll)
{
count= 0;
for(Ti2 : coll)
{
/**
* it compares references so equals() has been defined in productclass
*/
if(i1.equals(i2))
count++;
}
if(count > 1)
return true;
}
return false;
}
}
class Product implements Comparable
{
private int id;
private String name;
publicProduct(int i, String n)
{
name = n;
id = i;
}
public void display()
{
System.out.println(id +"" +name);
}
public int compareTo(Object o)
{
returnname.compareTo(((Product)o).name);
}
public boolean equals(Object obj)
{
Product p2 =(Product)obj;
returnname.equals(p2.name);
}
public int hashCode()
{
int hash = 5;
hash = 11*hash+id;
hash = 11*hash +(name !=null ? name.hashCode(): 0);
return hash;
}
}
Explanation / Answer
Your Product classneeds a toString() method. Rightnow println() is printing the defaulttoString() of Object. publicString toString() { return""+id+" "+name; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.