Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialstefanrouth
683 Pointshelp using classes
i need help i need a way to use dog_food bacon = new dog_food ("Bacon"); witch is created in the main class in the dog class for the method dog.Eat() so my dog can eat bacon also how would i remove the bacon after the dog eats it so it will apply to the laws of physics and not be infinite. i also need to know how to use arguments in methods
4 Answers
Daniel Santos
34,969 PointsHi Stefan,
I am not sure if this is what you want, but I gave it a try. Craig Dennis can you check if what I am doing is fine?
This is your DogFood class:
public class DogFood {
private String mFood = "";
public DogFood(String food){
mFood = food
}
public String serveFood(){
return mFood;
}
}
This is the Dog class:
public class Dog{
private String mDogName = "";
private String mBreed = "";
private String mEating = "nothing";
public Dog(String dogName, String breed){
mDogName = dogName;
mBreed = breed;
}
public void eat(String foodName){
mEating = foodName;
}
public void removeFood(){
mEating = "nothing";
}
public String isEating(){
return "The dog is eating " + mEating;
}
}
And finally, main:
public static void main(String[] args) {
Dog dog = new Dog("Tommy", "German Shepard")
DogFood dog_food = new DogFood("Beacon");
dog.eat(dog_food.serveFood);
}
-Dan
stefanrouth
683 Pointscool i can finally feed my java dog (=
Daniel Santos
34,969 PointsThe doggy can eat! Hehehe
Craig Dennis
Treehouse TeacherWow you guys took eating your own dog food. to a whole new level.
That's one way to do it for sure. You can always model whatever you need. How about a Bowl
and store the number of kibbles in it.
Bowl bowl = new Bowl();
Dog dog = new Dog();
bowl.fill();
while (!bowl.isEmpty()) {
dog.eatFrom(bowl);
}
Love that you are thinking through this stuff! Just like the "We built this city" earworm....sorry ;)
Tim Jeffries
1,831 PointsOn the DogFood class, the 5th line, I notice there isn't an ";". Won't that break the code?
stefanrouth
683 Pointsstefanrouth
683 PointsThanks this was very helpful