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

Oskar Pihlak
Oskar Pihlak
2,094 Points

Constructors

Now let's add a private uninitialized field to store the current number of energy bars. Name it mBarsCount. Initialize it to zero in the constructor.

What does Initialize it to zero in the constructor mean ? :)

GoKart.java
public class GoKart {
  private String mColor;

  public static final int MAX_ENERGY =8;
    private mBarsCount;
   mBarCount wholeBarCount = new Barcount = 0;

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

  public String getColor() {
    return mColor;
  } 
}

1 Answer

James Vaughan
PLUS
James Vaughan
Courses Plus Student 4,462 Points

Hi Oskar,

The question asks two things:

  1. Add a private unitialised field to store the current number of energy bars (name it mBarsCount).
  2. Initialise it to zero in the constructer.

To answer 1. you need to do the following:

private int mBarsCount;

Then to answer 2. you need to initialise this member variable to a value of zero in the constructer. The constructer is the public method with the same name as the class (so GoKart). You need to add the following to it:

mBarsCount = 0;

Your final code should look like (I've commented out lines that were incorrect):

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

    // mBarCount wholeBarCount = new Barcount = 0;

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

    public String getColor() {
        return mColor;
    } 
}

Hope that helps.