d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > What Am I Doing Wrong?
1235Next
Add Reply New Topic New Poll
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Apr 23 2016 05:09pm
http://pastebin.com/aTGSvmhD

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



void check() {
if(omnivore = true) System.out.println("is an omnivore");
if(carnivore = true) System.out.println("is an carnivore");
if(herbavore = true) System.out.println("is an herbavore");
}

}


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

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

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());


}
}


This post was edited by ferf on Apr 23 2016 05:12pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 23 2016 05:38pm
I'd start by looking up the difference between = and == in your conditionals. Also, next time explain what your problem is
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Apr 23 2016 05:42pm
Quote (carteblanche @ Apr 23 2016 07:38pm)
I'd start by looking up the difference between = and == in your conditionals. Also, next time explain what your problem is


My problem is the code doesn't compile,a nd idk why, you'renot very helpful
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 23 2016 06:10pm
Quote (ferf @ Apr 23 2016 07:42pm)
My problem is the code doesn't compile,a nd idk why, you'renot very helpful


know what would be helpful? posting the compile error and highlighting which lines of code is causing it. nobody here is gonna compile your code, so the onus is on you to give as much info as you can.

as for my suggest of comparing = and == you can look at this link:

http://www.javacoffeebreak.com/faq/faq0022.html

This post was edited by carteblanche on Apr 23 2016 06:29pm
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Apr 23 2016 08:00pm
Quote (carteblanche @ Apr 23 2016 08:10pm)
know what would be helpful? posting the compile error and highlighting which lines of code is causing it. nobody here is gonna compile your code, so the onus is on you to give as much info as you can.

as for my suggest of comparing = and == you can look at this link:

http://www.javacoffeebreak.com/faq/faq0022.html


ok thanks, and sorry for being rude.

error i get:
Code
Animal2.java:24: error: cannot find symbol
dog.omnivore = true;
^
symbol: variable omnivore
location: variable dog of type Animal2
Animal2.java:25: error: cannot find symbol
tiger.carnivore = true;
^
symbol: variable carnivore
location: variable tiger of type Animal2
Animal2.java:26: error: cannot find symbol
skunk.herbavore = true;
^
symbol: variable herbavore
location: variable skunk of type Animal2
Animal2.java:28: error: cannot find symbol
System.out.println("Dog is a " + dog.check());
^
symbol: method check()
location: variable dog of type Animal2
Animal2.java:29: error: cannot find symbol
System.out.println("tiger is a " + tiger.check());
^
symbol: method check()
location: variable tiger of type Animal2
Animal2.java:30: error: cannot find symbol
System.out.println("Skunk is a " + skunk.check());
^
symbol: method check()
location: variable skunk of type Animal2




http://pastebin.com/W28JGSpM













Also my updated code:

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



void check() {
if(omnivore == true) System.out.println("is an omnivore");
if(carnivore == true) System.out.println("is an carnivore");
if(herbavore == true) System.out.println("is an herbavore");
}

}


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

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

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());


}
}

http://pastebin.com/ZeESQHMe

This post was edited by ferf on Apr 23 2016 08:02pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 23 2016 08:29pm
ok, lets dissect it.

Quote
Animal2.java:25: error: cannot find symbol
tiger.carnivore = true;
^
symbol: variable carnivore
location: variable tiger of type Animal2


look at your Animal2 class. what fields do you have? all i see is a main method. so where is your carnivore field coming from?

This post was edited by carteblanche on Apr 23 2016 08:32pm
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Apr 23 2016 08:30pm
Quote (carteblanche @ Apr 23 2016 10:29pm)
ok, lets dissect it.



look at your Animal2 class. what fields do you have? all i see is a main method. so where is your carnivore field coming from?


i have no idea what you mean..... carnivore was declared in class animal

EDIT: Btw i'm only 115 pages into the book i'm reading, so there's still a lot i don't know or haven't even covered yet

This post was edited by ferf on Apr 23 2016 08:34pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 23 2016 08:34pm
Quote (ferf @ Apr 23 2016 10:30pm)
i have no idea what you mean..... carnivore was declared in class animal


yes, that is exactly the point.

Quote
Animal2 tiger = new Animal2();
tiger.carnivore = true;


notice how you're not using the Animal class? you're using Animal2, which does not have the carnivore field.

This post was edited by carteblanche on Apr 23 2016 08:34pm
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Apr 23 2016 08:35pm
Quote (carteblanche @ Apr 23 2016 10:34pm)
yes, that is exactly the point.

Code
[B]Animal2[/B] tiger = new [B]Animal2[/B]();
tiger.[B]carnivore[/B] = true;


notice how you're not using the Animal class? you're using Animal2, which does not have the carnivore field.


oh yea, WOW i'm dumb LOL, sorry thanks! :D
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Apr 23 2016 08:36pm
hmmm i still get these errors:


Code
C:\Users\ferfy\Desktop>javac Animal2.java
Animal2.java:28: error: 'void' type not allowed here
System.out.println("Dog is a " + dog.check());
^
Animal2.java:29: error: 'void' type not allowed here
System.out.println("tiger is a " + tiger.check());
^
Animal2.java:30: error: 'void' type not allowed here
System.out.println("Skunk is a " + skunk.check());
Go Back To Programming & Development Topic List
1235Next
Add Reply New Topic New Poll