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

Add a constant field to the class that stores the maximum number of energy bars.

Please help, I don't know how to complete this challenge.

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.

GoKart.java
public class GoKart {
  static public final String mCharge;

  public GoKart(String charge) {
    charge = charge;
  }

  public String getCcharge() {
    return mCharge;
  }
}

2 Answers

When I start this challenge, I get the following code:

public class GoKart {
  private String mColor;

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

  public String getColor() {
    return mColor;
  } 
}

It asks me to add the constant; add a constant field to the class that stores the maximum number of energy bars, which gives me the following code:

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

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

  public String getColor() {
    return mColor;
  } 
}

Next, the challenge asks for add a private uninitialized field to store the current number of energy bars. Name it mBarsCount. Initialize it to zero in the constructor

That gives me:

public class GoKart {
  private String mColor;
  public static final int MAX_ENERGY_BARS = 8;
  private int mBarsCount; // new member variable

  public GoKart(String color) {
    mColor = color;
    mBarsCount = 0; // initialize it here
  }

  public String getColor() {
    return mColor;
  } 
}

Lastly, it asks add a method named charge. It should be public and return nothing. When called it should set mBarsCount to the value of the constant you declared in Task 1. So we need to add a method called charge(). It returns nothing, takes no parameters and is public. Inside it, the member variable mBarsCount is set equal to the constant we defined right at the start.

That's:

public void charge(){
  mBarsCount = MAX_ENERGY_BARS;
}

So, the final code sample looks like:

public class GoKart {
  private String mColor;
  public static final int MAX_ENERGY_BARS = 8;
  private int mBarsCount;

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

  public String getColor() {
    return mColor;
  } 

  public void charge(){
    mBarsCount = MAX_ENERGY_BARS; // set member var to the constant value
  }  
}

Make sense?

Steve.

Yes it does, thank you.

Hi Madison,

You're very close with that one.

The constant holds an integer; 8. The naming convention for constants is to use capital letters. So, for this, you might end up with something like:

public static final int MAX_ENERGY_BARS = 8;

I hope that helps.

Steve.

I have changed that, thank you very much, now it is saying there is an error on return mCharge. I have tried changing the mCharge to charge and then when that didn't work I tried changing it to Charge without the m. What is wrong with this?