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) Meet Objects Constructors

Confused about this challenge....

A little mind boggled about this challenge. I even compared my code to answer previously given for this challenge and from what i can see its exactly the same. However, im still getting a syntax error and im not seeing it?

GoKart.java
public class GoKart {
  private String mColor = "red";
  public GoKart(String mColor) {
    mColor = color;
  }

  public String getColor() {
    return mColor;
  }
}
David Aryeetey
David Aryeetey
6,258 Points

I have noticed where you went wrong. Look at your constructor's parameter.It should be the local variable color not the field mColor in your parentheses. So just change mColor to color in your round brackets.

public GoKart(String color) {/* not public GoKart(String mColor) */ mColor = color; }

3 Answers

yeah thanks lol. For some reason there seems to be a magical presence that appears and enters your brain so you figure it out after you've already asked how to do it lmao

David Aryeetey
David Aryeetey
6,258 Points

You're welcome. I've been programming java for 4 years and it does happen. It's normal. However I do hope you do understand how params work right? color the parameter is merely the symbolic name for whatever compatible value would be passed into the constructor when it is actually called in a program. Then that String value for color would be stored in the mColor variable. /my actual program called Demo/ class Demo{

 public static void main(String[] args){  

      /* creating a GoKart called bowserKart*/
       GoKart bowserKart = new GoKart("blue");   

       System.out.printf("The color passed in for this cart is %s", bowserKart.mColor);

/* prints out The color passed in for this cart is blue */ } }

Honestly i just started....However, i sort of do. Currently im finding it difficult to retain some of the information and understanding how it all works together. Its just not clicking.

David Aryeetey
David Aryeetey
6,258 Points

It's quite simple at least the general philosophy. A class is either just a blueprint (and most of the time it is) or a program. The difference is when a class has a main() method that means it's meant to be a program and not a mere blueprint. Now the blueprint is meant to be like a Design or a set up of code that when it's ready we can launch in the program.

In Object Oriented programming we use software objects to model real world objects. class Car{// a class is simply a blueprint //attributes or properties-- variables that describe your object String model; int speed; boolean isSedan; Battery carBattery;

//behaviors or named actions - methods/functions - 
public void startEngine() {
    System.out.println("BrmM!!Brm!! ");
}

public void accelerate(int increase) { 
    speed = speed+increase;
    System.out.println(model+" is accelerating at new speed of "+speed);

    if(speed>150){
        System.out.println(model+" SLOW DOWN you'r overspeeding");
    }
}

public void brake(int decrease) {
    speed = speed-decrease;
    System.out.println(model+" speed has slowed down to "+speed);

    if (speed <= 0) {
        System.out.println(model+ " is no longer moving.");
    }
}

public void turn(String direction) {
    System.out.println(model+" turning "+direction);
}

}

//The Program public class CarDemo {

/*Most code is usually made of the ff:
 * keywords    identifiers    operators
 * 
 */

public static void main(String[] args) {

    //creating 2 Car objects and using reference variables to label them
    Car car1 = new Car();
    Car car2 = new Car();


    String name ="wwfjrfj";// [wwfjrfj  ] name

    car1.model="Toyota Camry";
    car2.model ="Dodge Caravan";
    car1.isSedan = true;
    car2.isSedan = false;

    car1.startEngine();// I'm starting car1
    car2.startEngine();//starting car2

    System.out.println();

    car1.accelerate(50);
    car2.accelerate(170);

    System.out.println();

    car1.accelerate(100);
    car2.accelerate(10);

    System.out.println();

    car1.brake(50);
    car2.brake(100);



}

}