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 trial
alexandracoder0809
2,606 Points"Abstraction at Play": Code doesn't work in Eclipse. What can I do?
Good Evening,
I'm trying to follow the Java Track along with coding in Eclipse, because I have already coded there, before I have started the Java Course here. Until now everything has worked fine.
However, Eclipse is pointing several errors out, when I'm trying to run the code. What is the problem here?
1.) PezDispenser
class PezDispenser
{
public static final int MAX_PEZ = 12; // Constant // final = endgültig initialisiert // static = provide variables +methods directly off the class
private int pezCount;
public static void main(String[] args)
{
}
final private String characterName;
public PezDispenser(String characterName)
{
this.characterName = characterName;
pezCount = 0; //create a new dispenser, that is first empty
}
public void fill()
{
pezCount = MAX_PEZ;
public boolean isEmpty()
{
return pezCount == 0;
}
}
public String getCharacterName()
{
return characterName;
}
}
2.) Example.java
ublic class Example
{
public static void main(String[] args)
{
System.out.println("We are making a new PEZ Dispenser!");
System.out.printf("FUN FACT: There are %d PEZ allowed in every dispenser %n", PezDispenser.MAX_PEZ);
PezDispenser dispenser = new PezDispenser("Yoda");
System.out.printf("The dispenser is %s. %n", dispenser.getCharacterName());
System.out.println("Filling the dispenser with delicious PEZ...");
dispenser.fill();
if(dispenser.isEmpty())
{
System.out.println("Dispenser is empty.");
if (!dispenser.isEmpty ())
{
System.out.println("Dispenser is full.");
}
}
}
}
Error messages:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method isEmpty() is undefined for the type PezDispenser The method isEmpty() is undefined for the type PezDispenser at Example.main(Example.java:15)
Thanks in Advance
P.S. I'm really sorry for that formatting and I would be very happy about any help with that code. Until that video everything worked.
3 Answers
Craig Dennis
Treehouse TeacherYou have your if statement inside the other if statement in Example.java.
Livia Galeazzi
Java Web Development Techdegree Graduate 21,083 PointsYou have a small curly brace issue here:
public void fill()
{
pezCount = MAX_PEZ;
public boolean isEmpty()
{
return pezCount == 0;
}
}
Your "isEmpty()" method is inside the fill() method. Just move one of those closing curly braces back between the fill() and isEmpty() methods, and it should work fine.
alexandracoder0809
2,606 PointsThank you very much. I have also another question:) Now the code works partially, but does not print out the exspected text. "We are making a new PEZ Dispenser! FUN FACT: There are 12 PEZ allowed in every dispenser The dispenser is Yoda. Filling the dispenser with delicious PEZ..." So, it's missing: "Dispenser is empty." and "Dispenser is full." However I have include3d those statemnets and have implemented those methods. Where could be the mistake?
Craig Dennis: I now reach out to you because I have tried now everything, but I have still the same problem seen above and like in the comment. Would you mind trying to help me? Your course is really good, but I can't progess due to this problem.
Craig Dennis
Treehouse TeacherAre you sure you are Example.main and not PezDispenser.main?
If that's not the problem, can you post your code as it is currently please?
alexandracoder0809
2,606 PointsThank you very much for your fast reply! This is my code as it is currently:
1.) PezDispenser
class PezDispenser
{
public static final int MAX_PEZ = 12; // Constant // final = endgültig initialisiert // static = provide variables +methods directly off the class
private int pezCount;
public static void main(String[] args)
{
}
final private String characterName;
public PezDispenser(String characterName)
{
this.characterName = characterName;
pezCount = 0; //create a new dispenser, that is first empty
}
public void fill()
{
pezCount = MAX_PEZ;
}
public boolean isEmpty()
{
return pezCount == 0;
}
public String getCharacterName()
{
return characterName;
}
}
2.) Example
public class Example
{
public static void main(String[] args)
{
System.out.println("We are making a new PEZ Dispenser!");
System.out.printf("FUN FACT: There are %d PEZ allowed in every dispenser %n", PezDispenser.MAX_PEZ);
PezDispenser dispenser = new PezDispenser("Yoda");
System.out.printf("The dispenser is %s. %n", dispenser.getCharacterName());
System.out.println("Filling the dispenser with delicious PEZ...");
dispenser.fill();
if(dispenser.isEmpty())
{
System.out.println("Dispenser is empty.");
if (!dispenser.isEmpty ())
{
System.out.println("Dispenser is full.");
}
}
}
}
My output:
We are making a new PEZ Dispenser! FUN FACT: There are 12 PEZ allowed in every dispenser The dispenser is Yoda. Filling the dispenser with delicious PEZ...
-----> I am missing the last part as shown in the video with that it's full etc.
Thank you in advance and Best Regards
alexandracoder0809
2,606 Pointsalexandracoder0809
2,606 PointsThank you very much! It finally works:)