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 Helper Methods

Brandon Wash
PLUS
Brandon Wash
Courses Plus Student 1,186 Points

can anyone take a crack at this and explain how this code works?

Need some help understand this

GoKart.java
public class GoKart {
  public static final int MAX_BARS = 8;
  private String mColor;
  private int mBarsCount;

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

  public String getColor() {
    return mColor;
  }

  public void charge() {
    mBarsCount = MAX_BARS;
  }
  public boolean isBatteryEmpty() {
    return mBarsCount == 0;
  }
  while(!isFullyCharged()) {
    mBarsCount++;
 }
Ken Alger
Ken Alger
Treehouse Teacher

Brandon;

Would love to explain as much as I am able too. Is there a specific portion of the code you are finding particularly difficult?

Ken

5 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Brandon;

I think trying to sum up the "key principles in Java coding" in a forum response would be unfair. If you have gone through the Java Basics course, and still are having difficulties understanding some of the concepts, by all means go through it again hitting the areas where you are not "getting it."

In terms of how simple is it for me personally to write code, I can honestly say that it really depends. There are times when everything just seems to click, I type in a bunch of code, execute the code, and no issues occur. There are other times when I can't seem to recall how to define a variable, okay, perhaps not that bad, but it can feel that way. Fortunately, the more I code the more I find myself in the former mode instead of the later.

This holds true though, I believe, in most endeavors or professions. Take Dr. Harvey Cushing as an example. Dr. Cushing graduated from Harvard Medical School in 1895. He was a pioneer in the field of neurosurgery, to the extent that he discovered and has a syndrome named after him (Cushing's syndrome). By all accounts a brilliant guy. I am sure that he saved the lives of many in his long career. He, I am sure, was able to get in and out of the brain quickly and do incredible things. I would guess, however, that his first few surgeries were not as efficient.

Now I am not trying to equate doing software development to brain surgery. How many times has someone said, "Come on... this isn't brain surgery!"? But, the point is that everyone has to start somewhere, and with practice it becomes easier. The first time you see a variable declaration in Java of

public String someVariable = "Some Value";

it can make you read it twice to figure out what is going on, right? That doesn't even take into account classes, functions, import statements, lists, arrays, etc. It is like reading a different language, right? Well, it is. With enough practice and exposure, hopefully, you are able to know that the above creates a publicly accessible variable of the type String named someVariable with a value of Some Value.

So, starting out, how does one get there? For me it was by taking the code and breaking it down into realistic programming chunks. Let's say that your goal is to write an entirely new interface, framework, API, and website for Treehouse. Ready, set, go! Rather daunting, correct? I doubt many would be able to sit down in one sitting and go from start to finish and include everything in, write great code, etc. It needs to be broken down into manageable sections.

I find it helpful to do the same for small projects. You want to write a simple calculator program, what do you need? User interface (data input and output), the procedure to do the various math tasks, the procedure for error handling (no sir, I cannot divide 4 by 0), etc. Work on each piece until you get that piece correct, and then continue on. Often you will wind up having to go back and add or remove additional things, but if you code correctly it shouldn't be that big a task to make changes.

I have forgotten which course on Treehouse by Andrew Chalkley covers the 4 P's of problem solving, but it is definitely worth a look. I found it to be a very intuitive way to lay out and take a development project from conception to a functioning project. I'll see if I can find it and post a follow up comment.

Sorry for the length of the post. Keep your head up, you have taken a great first step by starting to code at Treehouse and by asking questions in the forum. That is huge! I am sure you will find the forum a tremendous resource as you continue on.

Happy coding, Ken

Robert Walker
Robert Walker
17,146 Points

You cant really explain how it all works without seeing the rest of the code but from what I understand, it is a way to charge a Gokart.

It takes 8 bars to be fully charged, if its not fully charged it adds a bar of charge over X amount of time till its full.

It also gets the colour of the Gokart.

Im not too sure but I think the lower case m: mBarCount and mColor means they are from another class of the same name.

Brandon Wash
PLUS
Brandon Wash
Courses Plus Student 1,186 Points

Sure Ken. Im just trying to understand key principles in Java coding? May I ask, how simple is coding for you to write?

Brandon Wash
PLUS
Brandon Wash
Courses Plus Student 1,186 Points

Okay Ken thank you for this awesome feedback. I certainly appreciate your help and support.

Brandon