forked from Hithomelabs/Princeton1
67 lines
1.7 KiB
Java
67 lines
1.7 KiB
Java
package com.hithomelabs.clients.module5;
|
|
|
|
import com.hithomelabs.princeton1.module5.Selection;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
public class SelectionClient {
|
|
|
|
|
|
|
|
public static void main(String[] args){
|
|
|
|
Apple[] apples = new Apple[3];
|
|
Orange[] oranges = new Orange[3];
|
|
Selection<Apple> selection = new Selection<Apple>();
|
|
|
|
apples[0] = new Apple(3);
|
|
apples[1] = new Apple(5);
|
|
apples[2] = new Apple(4);
|
|
selection.sort(apples);
|
|
|
|
//* * Sample output
|
|
for (int i = 0; i < apples.length; i++)
|
|
System.out.println(apples[i]);
|
|
|
|
oranges[0] = new Orange(4);
|
|
oranges[1] = new Orange(1);
|
|
// * Should give runtime exception as ClassCastException is a runtime exception
|
|
//selection.sort(oranges);
|
|
Selection<Orange> selection2 = new Selection<Orange>();
|
|
// * Should result in a compile time exception, as casting to Orange will fail
|
|
//selection2.sort(apples);
|
|
}
|
|
|
|
private static class Orange{
|
|
private int size;
|
|
Orange(int size){
|
|
this.size = size;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "An orange of size "+size;
|
|
}
|
|
}
|
|
|
|
private static class Apple implements Comparable<Apple>{
|
|
private int size;
|
|
|
|
Apple(int size){
|
|
this.size = size;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "An apple of size "+size;
|
|
}
|
|
|
|
@Override
|
|
public int compareTo(Apple that) {
|
|
if (this.size < that.size) return -1;
|
|
else if (this.size == that.size)return 0;
|
|
else return 1;
|
|
}
|
|
}
|
|
}
|