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

naiara gonzalez
naiara gonzalez
16,778 Points

I dont know what is wrong with this code,can anyone help me,please?thanks anyway

Question: 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.

public class GoKart { public static final int MAX_B = 8; public final int mBattery; private String mColor;

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

public String getColor() { return mColor; }

public void battery() { mBattery = MAX_B; }

} my code:

public class GoKart {
  public static final int MAX_B = 8;
public final int mBattery;
  private String mColor;

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

  public String getColor() {
    return mColor;
  } 

  public void battery() {
    mBattery = MAX_B;
  }

}
GoKart.java
public class GoKart {
    public static final int MAX_B = 8;
public final int mBattery;
  private String mColor;

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

  public String getColor() {
    return mColor;
  } 
   public void battery() {
   final mBattery = MAX_B;
 }
}

6 Answers

If you want to simply start over, you may have a better understanding of what is happening in this challenge, and how it relates to the lesson. Look at this code for your example to follow:

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;
  }
}
  • Instead of MAX_PEZ, you are creating MAX_BARS
  • Instead of mPezCount, you are creating mBarsCount
  • Instead of a load method, you are creating a charge method
public String getColor {

should be:

public void getColor {

i.e. "String" should be "void".

Hope it helps,

ISAIAH S

Craig Dennis
Craig Dennis
Treehouse Teacher

getColor should return a String like it says.

naiara gonzalez
naiara gonzalez
16,778 Points

I changed String for void and it didnt work....

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Not sure where the finals are coming from... Try removing them.

Are you getting compilation errors? What are they?

naiara gonzalez
naiara gonzalez
16,778 Points

This is my compiler error: /GoKart.java:14: error: expected final mBattery = MAX_B; ^ ./GoKart.java:11: error: cannot return a value from method whose result type is void return mColor; ^ ./GoKart.java:14: error: cannot find symbol final mBattery = MAX_B; ^ symbol: class mBattery location: class GoKart 3 errors

on the character name what do i put.?

You aren't making any methods that use character name. You only need to follow the example of the load method.

public class GoKart {
  public  int mBars = 0 ;
  private String mColor;
  public static final int M_COUNT = 8;

  public GoKart(String color) {
    mColor = color;

  }

  public void mBarCount(){
       mBars = M_COUNT;
  }

  public String getColor() {
    return mColor;
  } 
}
...