d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > What Am I Doing Wrong?
Prev12345Next
Add Reply New Topic New Poll
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 24 2016 12:31am
Quote (Eep @ Apr 24 2016 02:30am)
true. I lose track of time pretty easily these days......april eh.

All I can think about is a big release we are doing in August....


sounds like a great time to go on vacation
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Apr 24 2016 12:32am
So...this is my new code... but i still get an error, not sure why...wth!!!!!


Code
class Animal {
boolean omnivore;
boolean carnivore;
boolean herbavore;



String check() {
if(omnivore == true) return "omnivore";
if(carnivore == true) return "carnivore";
if(herbavore == true) return "herbavore";
}

}


class Animal2 {
public static void main(String args[]) {

Animal dog = new Animal();
Animal tiger = new Animal();
Animal skunk = new Animal();

dog.omnivore = true;
tiger.carnivore = true;
skunk.herbavore = true;

System.out.println("Dog is a " + dog.check());
System.out.println("tiger is a " + tiger.check());
System.out.println("Skunk is a " + skunk.check());


}
}










Code
Animal2.java:12: error: missing return statement
}
^
1 error


This post was edited by ferf on Apr 24 2016 12:33am
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Apr 24 2016 01:28am
return null;

^fixed it.... not sure why i need null since all the variables are true...but...
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 24 2016 03:01am
Quote (ferf @ Apr 24 2016 02:28am)
return null;

^fixed it.... not sure why i need null since all the variables are true...but...


What happens if all of them are false? What would it return in that case? That is where the missing return statement is.
Member
Posts: 5,354
Joined: Dec 15 2008
Gold: 66.63
Apr 24 2016 06:13pm
Quote (ferf @ Apr 24 2016 02:28am)
return null;

^fixed it.... not sure why i need null since all the variables are true...but...



java doesn't know that the if statements will always return a value

if carnivore, herbivore and omnivore were all false, it would be left with no return statement
Member
Posts: 12,786
Joined: May 17 2013
Gold: 4,010.00
Apr 25 2016 06:03am
Hey ferf, I wanted to add something since your code samples puzzle me a bit.
I'll just keep it short, but it can help you a lot if you are new to object oriented programming.

When you deal with generic abstractions of something (such as the Animal class representing a generic animal) it's a bad habit to keep irrelevant information bundled with the class.
Rather, what you should strive for is something called seperation of concern, which basically means seperating responsibility of your program into bits that fit together, rather than trying to jumble everything into one method/class etc.

As an example, in your previous code you jumbled all details of animals being omnivores, carnivores or herbivores into the same class and then set whichever field to true in order to represent their eating habits.
Like this:

Code
class Animal {
boolean omnivore;
boolean carnivore;
boolean herbavore;

String check() {
if(omnivore == true) return "omnivore";
if(carnivore == true) return "carnivore";
if(herbavore == true) return "herbavore";
}

}

class Animal2 {
public static void main(String args[]) {

Animal dog = new Animal();
Animal tiger = new Animal();
Animal skunk = new Animal();

dog.omnivore = true;
tiger.carnivore = true;
skunk.herbavore = true;

System.out.println("Dog is a " + dog.check());
System.out.println("tiger is a " + tiger.check());
System.out.println("Skunk is a " + skunk.check());
}
}


However, since most object oriented languages support inheritance, it is much cleaner to implement a check such as this by having seperate classes for each possibility, each doing their own check for what type of animal you're dealing with.
Obviously the check is being done on which type of object it is when you call the check method, but that doesn't matter to you as the programmer.

The code for seperating the concern of checking the type of eating habit an animal has, would be done this way:


Code
abstract class Animal {

abstract String check();
}

class Omnivore extends Animal {
String eats = "omnivore that eats everything";

// Has to implement this method since it derives from the abstract Animal class.
String check() {
return eats;
}
}

class Carnivore extends Animal {
String eats = "carnivore that eats meat.";

// Has to implement this method since it derives from the abstract Animal class.
String check() {
return eats;
}

}

class Herbivore extends Animal {
String eats = "herbivore that eats plants.";

// Has to implement this method since it derives from the abstract Animal class.
String check() {
return eats;
}

}

class Main {
public static void main(String args[]) {

Animal dog = new Omnivore();
Animal tiger = new Carnivore();
Animal skunk = new Herbivore();

System.out.println("Dog is an " + dog.check());
System.out.println("Tiger is an " + tiger.check());
System.out.println("Skunk is an " + skunk.check());
}
}


Best regards.

This post was edited by Klexmoo on Apr 25 2016 06:04am
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 25 2016 08:43am
Quote (Klexmoo @ Apr 25 2016 07:03am)
Hey ferf, I wanted to add something since your code samples puzzle me a bit.
I'll just keep it short, but it can help you a lot if you are new to object oriented programming.

When you deal with generic abstractions of something (such as the Animal class representing a generic animal) it's a bad habit to keep irrelevant information bundled with the class.
Rather, what you should strive for is something called seperation of concern, which basically means seperating responsibility of your program into bits that fit together, rather than trying to jumble everything into one method/class etc.

As an example, in your previous code you jumbled all details of animals being omnivores, carnivores or herbivores into the same class and then set whichever field to true in order to represent their eating habits.
Like this:

Code
class Animal {
boolean omnivore;
boolean carnivore;
boolean herbavore;

String check() {
if(omnivore == true) return "omnivore";
if(carnivore == true) return "carnivore";
if(herbavore == true) return "herbavore";
}

}

class Animal2 {
public static void main(String args[]) {

Animal dog = new Animal();
Animal tiger = new Animal();
Animal skunk = new Animal();

dog.omnivore = true;
tiger.carnivore = true;
skunk.herbavore = true;

System.out.println("Dog is a " + dog.check());
System.out.println("tiger is a " + tiger.check());
System.out.println("Skunk is a " + skunk.check());
}
}


However, since most object oriented languages support inheritance, it is much cleaner to implement a check such as this by having seperate classes for each possibility, each doing their own check for what type of animal you're dealing with.
Obviously the check is being done on which type of object it is when you call the check method, but that doesn't matter to you as the programmer.

The code for seperating the concern of checking the type of eating habit an animal has, would be done this way:


Code
abstract class Animal {

abstract String check();
}

class Omnivore extends Animal {
String eats = "omnivore that eats everything";

// Has to implement this method since it derives from the abstract Animal class.
String check() {
return eats;
}
}

class Carnivore extends Animal {
String eats = "carnivore that eats meat.";

// Has to implement this method since it derives from the abstract Animal class.
String check() {
return eats;
}

}

class Herbivore extends Animal {
String eats = "herbivore that eats plants.";

// Has to implement this method since it derives from the abstract Animal class.
String check() {
return eats;
}

}

class Main {
public static void main(String args[]) {

Animal dog = new Omnivore();
Animal tiger = new Carnivore();
Animal skunk = new Herbivore();

System.out.println("Dog is an " + dog.check());
System.out.println("Tiger is an " + tiger.check());
System.out.println("Skunk is an " + skunk.check());
}
}


Best regards.


He has issues with simple syntax and you want to throw in polymorphism. Seems to me you were just looking to jerk yourself off. Your encapsulation sucks though. Here let me help you, since you are so concerned about him being a better at OOP.

Code
public abstract class Animal {
protected string eats;
public Animal(String eats)
{
this.eats = eats;
}

public String check()
{
returns eats;
}
}

public class Omnivore extends Animal {
public Omnivore()
{
super("omnivore that eats everything");
}
}

public class Carnivore extends Animal {
public Carnivore()
{
super("carnivore that eats meat.");
}
}

public class Herbivore extends Animal {
public Herbivore()
{
super("herbivore that eats plants.");
}
}


Still sucks, but an improvement. Now we can circle jerk each other instead of focusing on OP's actual problems.

This post was edited by Minkomonster on Apr 25 2016 08:44am
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Apr 25 2016 08:52am
you could also use an interface

Code
public interface Animal {
public String check( );
}



Code
public class Omnivore implements Animal {
@Override
public String check( ) {
return "Everything";
}
}
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Apr 26 2016 02:48pm
Quote (ferf @ Apr 24 2016 01:28am)
return null;

^fixed it.... not sure why i need null since all the variables are true...but...


handy shortcut
when you're dealing with boolean expressions

Code

boolean omnivore;
boolean carnivore;
boolean herbavore;

String check() {
if(omnivore == true) return "omnivore";
if(carnivore == true) return "carnivore";
if(herbavore == true) return "herbavore";
}


you don't need to set your if statemenets as omnivore==true you can simply write
Code
String check() {
if(omnivore) return "omnivore";
if(carnivore) return "carnivore";
if(herbavore) return "herbavore";
}

when checking for false you can write if (!omnivore) dostuff; //for not ("!") omnivore
Member
Posts: 12,786
Joined: May 17 2013
Gold: 4,010.00
Apr 26 2016 02:54pm
Quote (Ideophobe @ 26 Apr 2016 22:48)
handy shortcut
when you're dealing with boolean expressions

Code
boolean omnivore;
boolean carnivore;
boolean herbavore;

String check() {
if(omnivore == true) return "omnivore";
if(carnivore == true) return "carnivore";
if(herbavore == true) return "herbavore";
}


you don't need to set your if statemenets as omnivore==true you can simply write
Code
String check() {
if(omnivore) return "omnivore";
if(carnivore) return "carnivore";
if(herbavore) return "herbavore";
}

when checking for false you can write if (!omnivore) dostuff; //for not ("!") omnivore


Why are you teaching him things? It's frowned upon around here.
Go Back To Programming & Development Topic List
Prev12345Next
Add Reply New Topic New Poll