Taki komunikat wyskakuje mi przy próbie odpalenia programu:
run: Exception in thread "main" java.lang.ClassCastException: kolokwium.Student cannot be cast to java.lang.Comparable at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:320) at java.util.ComparableTimSort.sort(ComparableTimSort.java:188) at java.util.Arrays.sort(Arrays.java:1246) at kolokwium.Grupa.<init>(Grupa.java:20) at kolokwium.NazwaGrupa.<init>(NazwaGrupa.java:17) at kolokwium.IO.readFile(IO.java:36) at kolokwium.Kolokwium.main(Kolokwium.java:31) Java Result: 1
To klasa Kolokwium z metodą main:
package kolokwium;
import java.io.IOException;
public class Kolokwium {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SkalaOcen skala = new SkalaOcen();
for (String s : args) {
// Sprawdzamy, czy s jest liczbą,
// jeśli tak, to traktujemy ją jako próg skali ocen
try {
double t = Double.parseDouble( s );
skala = new DobraSkalaOcen( t );
} catch( NumberFormatException e ) {
// nie jest liczbą, ignorujemy wyjątek
}
try {
Grupa g = IO.readFile(s);
System.out.println( "Grupa " + ((NazwaGrupa)g).nazwa() + ":");
for (Student stud : g) {
System.out.println( stud + " \t" + skala.ocen(stud.pts()));
}
} catch( IOException e ) {
System.err.println( "Grupa zignorowana: \t" + e.getMessage());
}
}
}
}
To klasa Student
package kolokwium;
public class Student implements Comparable<Student> {
private final String sure;
private final String first;
private double pts;
public Student(String first, String sure) {
this.first = first;
this.sure = sure;
this.pts = 0;
}
public Student(String first, String sure, double pts) {
this(first, sure);
this.pts = pts;
}
public void setPts(double pts) {
this.pts = pts;
}
public double pts() {
return pts;
}
@Override
public String toString() {
return first + ' ' + sure;
}
@Override
public boolean equals( Object o ) {
if( ! (o instanceof Student) )
return false;
else {
Student os = (Student)o;
return first.equals(os.first) && sure.equals(os.sure);
}
}
@Override
public int hashCode() {
return (first+' '+sure).hashCode();
}
@Override
public int compareTo(Student other) {
Student o = (Student) other;
int cs = sure.compareTo(o.sure);
if (cs == 0) {
return first.compareTo(o.first);
} else {
return cs;
}
}
}
To klasa Grupa:
package kolokwium;
import java.util.Arrays;
import java.util.Iterator;
public class Grupa implements Iterable<Student>, Comparable<Grupa> {
private final Student [] studs;
public Grupa( Student [] s ) {
studs = s;
Arrays.sort( studs );
}
@Override
public Iterator<Student> iterator() {
return new StudsIterator( studs );
}
@Override
public int compareTo( Grupa o ) {
return studs.length - o.studs.length;
}
}
i jeszcze NazwaGrupa, która rozszerza Grupę:
package kolokwium;
public class NazwaGrupa extends Grupa {
private final String nazwa;
public NazwaGrupa( Student [] t, String nazwa ) {
super( t );
this.nazwa = nazwa;
}
public String nazwa() {
return nazwa;
}
}
Wydaje mi się, że błąd jest w którejś z tych klas ale nie potrafię go znaleźć. I jeszcze takie pytanie, czy trzeba pisać własny interfejs Comparable? Bo jest chyba już coś takiego w Javie, ale w projekcie z wykładu był również napisany taki interfejs, program nie działa z nim i bez niego:
package kolokwium;
public interface Comparable<T> {
public int compareTo( T o );
}