if this program is all you want to do probably i think the easiest way would be to use an enum and constructor rather than having a boolean value for each of the types of "vores" you can make a datatype that is the "type of vore"
Code
class Animal {
//boolean omnivore;
//boolean carnivore;
//boolean herbavore;
//since any animal is just going to be one of these things write
public enum Eater{ OMNIVORE, CARNIVORE, HERBAVORE }
//then you can create just 1 field for which type of Eater it is rather than having 3 boolean properties that might be irrelevant
Eater vore;
// then you can further simplify code in your main by making a constructor for it that lets you set "new Animal(OMNIVORE)" instead of new Animal(); animal.vore=OMNIVORE;
public Animal(Eater voreVariable){
vore=voreVariable
}
//it's a poor practice to have multiple return statements in a method and when are trying to pick from a list of options a switch statement is more efficient
//String check() {
// if(omnivore == true) return "omnivore";
// if(carnivore == true) return "carnivore";
// if(herbavore == true) return "herbavore";
//}
// this would work better as
String check(){
String s = null;
switch (vore){
case OMNIVORE: s="Omnivore";break;
case HERBAVORE: s="Herbavore";break;
case CARNIVORE: s="Carnivore";break;
default: s="nothing...apparently";break;
}
return s;
}
}
class Animal2 {
public static void main(String args[]) {
//now you can simplify all of these
//Animal dog = new Animal();
//Animal tiger = new Animal();
//Animal skunk = new Animal();
//
//dog.omnivore = true;
//tiger.carnivore = true;
//skunk.herbavore = true;
Animal dog = new Animal(Animal.Eater.OMNIVORE);
Animal tiger = new Animal(Animal.Eater.CARNIVORE);
Animal skunk = new Animal(Animal.Eater.HERBAVORE);
System.out.println("Dog is a " + dog.check());
System.out.println("tiger is a " + tiger.check());
System.out.println("Skunk is a " + skunk.check());
}
}
of course i'm assuming by this point if you are working with classes you are already familiar with the commands switch(){} and enum if not, you're going a little out of order i think, you should probably look into these two things before you progress with oop
This post was edited by Ideophobe on Apr 26 2016 03:51pm