I include the main method in one of my class, but the following error still appeared.
"Error: Main method not found in class Maximizer, ... "
However, if I moved the main method to class Maximizer, the problem solved.
Is main method required to be written inside a certain class? If so, what's the rule?
Or are there other solutions to this problem?
Thanks.
-------------------------my codes-------------------------
public class DogLauncher {
public static void main(String[] args){
Dog d1 = new Dog("Elyse", 3);
Dog d2 = new Dog("Sture", 9);
Dog d3 = new Dog("Benjamin", 15);
Dog[] dogs = new Dog[]{d1, d2, d3};
System.out.println(Maximizer.max(dogs));
Dog d = (Dog) Maximizer.max(dogs);
d.bark();
}
}
public class Maximizer {
public static Comparable max(Comparable[] items) {
int maxDex = 0;
for (int i = 0; i < items.length; i++) {
int cmp = items[i].compareTo(items[maxDex]);
if (cmp > 0) {
maxDex = i;
}
}
return items[maxDex];
}
}
public interface OurComparable {
/* Return negative number if this < o.
* Return 0 if this equals o.
* Return positive number if this > o.
*/
public int compareTo(Object o);
}
public class Dog implements Comparable<Dog>{
private String name;
private int size;
public Dog(String n, int s){
name = n;
size = s;
}
public void bark(){
System.out.println(name + " says: bark");
}
/* Returns negative number if this dog is less than the dog pointed at by o. */
public int compareTo(Dog uddaDog){
return this.size - uddaDog.size;
}
}