/** * @param args the command linearguments */ public static void main(String[]
ID: 3618529 • Letter: #
Question
/**
* @param args the command linearguments
*/
public static void main(String[] args)
{
int a = 4, b = 5;
System.out.println("max= "+ min(a, b));
System.out.println("max= "+ min('z', 'y'));
System.out.println("max = "+ min(new Product(1, "de"), newProduct(2, "bc")));
ArrayList<Integer>il = new ArrayList<Integer>();
il.add(10);
il.add(20);
il.add(30);
il.add(20);
System.out.println(duplicate(il));
ArrayList<Product>pl1 = new ArrayList<Product>();
pl1.add(new Product(1,"P1"));
pl1.add(new Product(2,"P2"));
pl1.add(new Product(4,"Kingdom"));
pl1.add(new Product(1,"P1"));
System.out.println(duplicate(pl1));
}
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 min() method isreturning the maximum. It should be: publicstatic T min(T a, T b) { if(a.compareTo(b)< 0) return a; return b; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.