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

Java Java Objects (Retired) Harnessing the Power of Objects Methods and Constants

Help with GoKart mEnergy (constants) question quiz

Ok, so I was trying to use my PezDispenser example coding from the video, to complete the quiz for the GoKart. I matched them, but it still says there is a syntax error though, any idea? The first is the PezDispenser the attached is GoKart -

public class PezDispenser { public static final int MAX_PEZ = 12; private String mCharacterName; private int mPezCount;

public PezDispenser(String characterName) { mCharacterName = characterName; mPezCount = 0; } public void load() { mPezCount = MAX_PEZ;
}

public String getCharacterName() { return mCharacterName;

}
}

GoKart.java
public class GoKart {
  public static final int MAX_ENERGY = 8;
  private String mColor;
  private int mEnergyBars;

  public GoKart(String color) {
    mColor = color;
  }
  public void load() {
    mEnergyBars = MAX_ENERGY;    
  }

  public String getColor() {
    return mColor;
  } 
}

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

In order to pass task 1:

These GoKarts have a single rechargeable battery that have a display of bars to measure its energy level. Each battery has a maximum of 8 bars.

For this task, let's add a constant field to the class that stores the maximum number of energy bars. Make sure the field cannot be changed and is accessible from the class, not just the instance. Use the naming convention we learned.

All you have to do is insert line with constant, and remove load method and mEnergyBars:

public class GoKart {
  public static final int MAX_ENERGY = 8; // correct
  private String mColor;
  private int mEnergyBars; // remove : this is wrong

  public GoKart(String color) {
    mColor = color;
  }

  // remove this method : it is not needed
  public void load() {
    mEnergyBars = MAX_ENERGY;    
  }

  public String getColor() {
    return mColor;
  } 
}

Is the 1-st task the one you are struggling with ?

Please don't try to copy code exactly. Do things task by task, following the instructions provided.

Like above I shown you that in order to solve task 1 all you do is adding public static final int as you did nothing else

Try to follow the same conception in the task 2 :)