d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > What Am I Doing Wrong?
Prev1345
Add Reply New Topic New Poll
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Apr 26 2016 03:21pm
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
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Apr 26 2016 03:26pm
Quote (Klexmoo @ Apr 26 2016 02:54pm)
Why are you teaching him things? It's frowned upon around here.


sorry about that i know how you guys like to throw code just out of reach of the little fledglings, i dont think inheritance is very elegant for such a simple program though

This post was edited by Ideophobe on Apr 26 2016 03:27pm
Member
Posts: 12,786
Joined: May 17 2013
Gold: 4,010.00
Apr 26 2016 03:29pm
Quote (Ideophobe @ 26 Apr 2016 23:26)
sorry about that i know how you guys like to throw code just out of reach of the little fledglings, i dont think inheritance is very elegant for such a simple program though


Inheritance doesn't make sense with small programs, but it's most easily explained with them though.
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Apr 26 2016 03:50pm
Quote (Klexmoo @ Apr 26 2016 03:29pm)
Inheritance doesn't make sense with small programs, but it's most easily explained with them though.

sure, but he's not there quite yet he still has to learn how to use the tools he's got before he can extend the bag imo
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 26 2016 05:47pm
Quote (Ideophobe @ Apr 26 2016 05:21pm)
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


considering that he's not comfortable with booleans, strings, methods, or return statements...

although he is a pro using label'd breaks.

This post was edited by carteblanche on Apr 26 2016 05:49pm
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Apr 26 2016 10:43pm
Quote (carteblanche @ Apr 26 2016 05:47pm)
considering that he's not comfortable with booleans, strings, methods, or return statements...

although he is a pro using label'd breaks.


cringe "pro at labeled breaks"... university would beat that out of you right quick

tbh i hadn't looked through this whole thread til just now; you're a helluva patient person. reading back... i thought i explained scope to him a couple questions ago... and i know i explained the difference between void and return methods. he's workin through that book of his too fast and not doing enough examples
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 26 2016 11:08pm
Quote (Ideophobe @ Apr 27 2016 12:43am)
cringe "pro at labeled breaks"... university would beat that out of you right quick


i was referring to one of his other topics. he gets hung up over things he doesn't need to worry about. he's told me to fuck off on at least one occasion before editing it out because i give him links that answers his questions. really weird.
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Apr 26 2016 11:21pm
Quote (carteblanche @ Apr 27 2016 01:08am)
i was referring to one of his other topics. he gets hung up over things he doesn't need to worry about. he's told me to fuck off on at least one occasion before editing it out because i give him links that answers his questions. really weird.


:/ <3 :D
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Apr 27 2016 05:07pm
Quote (carteblanche @ Apr 27 2016 12:08am)
i was referring to one of his other topics. he gets hung up over things he doesn't need to worry about. he's told me to fuck off on at least one occasion before editing it out because i give him links that answers his questions. really weird.


to be fair, I wasn't much better
Go Back To Programming & Development Topic List
Prev1345
Add Reply New Topic New Poll