I’m trying to write a program that accepts type comparable into the DataSet class so it can find the maximum and minimum values. The only problem is that I can’t compile either one and I’m a little confused by how to fix the errors. Thanks
DataSet.java:
public class DataSet<T implements Comparable>
{
private T maximum;
private T least;
private int count;
public void add(T x)
{
if(count == 0){
least = x;
maximum = x;
}
else if(least.compareTo(x) > 0)
least = x;
else if(maximum.compareTo(x) < 0)
maximum = x;
count++;
}
public T getMaximum()
{
return maximum;
}
public T getLeast()
{
return least;
}
}
error:
java:5: error: '(' or '[' expected
DataSet<String> ds = new DataSet<String>;
^
1 error
Comparable:
public interface Comparable
{
public int compareTo(Object other);
}
no errors here
public class DataSetTester
{
public static void main(String[] args)
{
DataSet<String> ds = new DataSet<String>;
ds.add(man);
ds.add(woman);
System.out.println("Maximum Word: " + ds.getMaximum());
}
}
error:
java:5: error: '(' or '[' expected
DataSet<String> ds = new DataSet<String>;
^
Watch out for the missing parantesis:
Also, you are misuing the “implements” keyword. For generics, you should use the extends keyword. Therefore, it should read:
You are also missing the “” when passing the string arguments:
Most of these errors are pretty trivial, you should try practicing a bit more to understand the language. Also, the error messages from the compiler should help.