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
cody perry
PHP Development Techdegree Student 18,448 PointsJava Public Method Challenge help?
Hello, i'm having problems getting and understanding how to approach this challenge?
public class GoKart {
public String mColor = "red";
public static void main(String[] args) {
System.out.println("We are making a new gokart! ");
GoKart goKart = new goKart();
System.out.printf("The new goKart color is %s\n", get.goKart());
}
}
There is the code i have for it.
./GoKart.java:5: error: cannot find symbol GoKart goKart = new goKart(); ^ symbol: class goKart location: class GoKart ./GoKart.java:6: error: cannot find symbol System.out.printf("The new goKart color is %s\n", get.goKart()); ^ symbol: variable get location: class GoKart 2 errors
Those are the errors i keep getting? Can't seem to get passed this?
13 Answers
cody perry
PHP Development Techdegree Student 18,448 Pointspublic class GoKart {
public String mColor = "red";
public static void main(String[] args) {
System.out.println("We are making a new gokart! ");
GoKart goKart = new goKart();
System.out.printf("The new goKart color is %s\n", get.goKart());
}
}
Code i have so far.
Steve Hunter
57,712 PointsHi Cody,
Have a look through this post.
I think you've not quite got the constructor right.
Let me know if that works for you - else I'll talk you through it.
Thanks,
Steve.
Steve Hunter
57,712 PointsThere is this one too which explains it differently.
cody perry
PHP Development Techdegree Student 18,448 PointsSo my question now would be do you need the code it starts with inside of you're code block?
public class GoKart {
public String mColor = "red";
}
Steve Hunter
57,712 PointsNone of my code blocks have that code. However, it looks like a kind of constructor.
A constructor creates an instance of the class it belongs to, taking in any parameters that make each instance unique. In the case of the GoKart class, the constructor takes a colour represented by a string.
The class & constructor would look like:
public class GoKart {
// instance variables
private String mColor;
// getters and setters
// other stuff 'n nonsense
// constructor(s)
public GoKart(String color) {
mColor = color;
}
// end of class
}
So that's the mechanism the challenge is relying on. This already exists behind the scenes in the challenge you're doing so it dosn't need replicating.
That challenge suggests you create a new instance of GoKart with the knowledge that the constructor takes a colour represented as a string. I always use "red" for some reason so let's mix that up a bit! To create an instance of a GoKart we need to call the constructor and pass in a colour as a parameter. That looks like:
GoKart notRedGoKart = new GoKart("Blue");
You just need to add that to the code in the challenge - nothing else. I hope that makes sense!
Steve.
cody perry
PHP Development Techdegree Student 18,448 Points public class GoKart {
private static void main(String[] args) {
System.out.println("We are going to create a GoKart");
GoKart notRedGoKart = new GoKart("Blue");
}
}
So that code gives me an error with the "new" part of the last line?
Here is the error:
./GoKart.java:5: error: constructor GoKart in class GoKart cannot be applied to given types; GoKart notRedGoKart = new GoKart("Blue"); ^ required: no arguments found: String reason: actual and formal argument lists differ in length 1 error
Am i missing something? i thought i needed to declare the GoKart with the preceeding "new"?
Thanks for all the help.
Steve Hunter
57,712 PointsHi Cody,
It looks like you've amended the class that the challenge wants you to complete.
The skeleton you should use is provided in the challenge and looks like:
public class Example {
public static void main(String[] args) {
System.out.println("We are going to create a GoKart");
}
}
In your code, you've amended Example to GoKart. This means you don't have a constructor for the GoKart class and you've tried to instantiate a GoKart object inside it's own class definition. That'll never go well!
So, if you change the skeleton you were provided with back to Example that will work fine. The GoKart class is defined elsewhere:
*"This already exists behind the scenes in the challenge you're doing so it dosn't need replicating.
That challenge suggests you create a new instance of GoKart with the knowledge that the constructor takes a colour represented as a string." *
Does that make sense?
In short, you just need to add one line, GoKart notRedGoKart = new GoKart("Blue"); to the end of the skeleton that the challenge has given you. Amend nothing else and you'll be fine.
Steve.
cody perry
PHP Development Techdegree Student 18,448 Pointspublic class Example {
public static void main(String[] args) {
System.out.println("We are going to create a GoKart");
GoKart redGoKart = new GoKart("red");
}
}
So you need to still define GoKart as well like (public class GoKart). I get access level restrictions when it tries to compile the code and access the GoKart, I get error: cannot access GoKart and file does not contain class GoKart?
Sorry to be a bother, trying to fully understand it before i move on?
Thanks.
Cody
Steve Hunter
57,712 PointsHi Cody,
The GoKart class is defined elsewhere but included within the challenge so the constructor can be accessed. The first part of the code challenge looks exactly as you just typed, yes. If you try that within a class outside of the challenge, you'd need to create a separate class file and include it in the Example class header. That's not what we're trying to achieve here, though.
The challenge is testing whether you are able to use a constructor to generate a new instance of the GoKart class using the required parameter. You've done that above with your line GoKart redGoKart = new GoKart("red");.
I'm not sure what you are having issues with if your compiler is causing errors about the GoKart class. You must be trying to replicate all of this with your own files?
Steve.
cody perry
PHP Development Techdegree Student 18,448 PointsIt keeps coming back with errors in the challenge. The ones i mentioned above from that code i typed. I'm using chrome, not sure if the browser could be causing an issue with the challenge or not.
Steve Hunter
57,712 PointsOK - let's be absolutely clear now.
Post the code you are entering in the challenge and the errors that code comes back with.
Meanwhile, here's what I've just recorded (apologies for the noise in the background - some nonsense is on the TV!) - this shows what I am trying to explain! Hope the link works: https://goo.gl/ZqqVmz
Steve.
cody perry
PHP Development Techdegree Student 18,448 Pointspublic class Example {
public static void main(String[] args) {
System.out.println("We are going to create a GoKart");
GoKart redGoKart = new GoKart("red");
}
}
Here is the exact code i'm imputing into the challenge code screen.
Here are the errors i'm getting:
./GoKart.java:1: error: class Example is public, should be declared in a file named Example.java public class Example { ^ JavaTester.java:65: error: cannot access GoKart Method[] methods = GoKart.class.getDeclaredMethods(); ^ bad source file: ./GoKart.java file does not contain class GoKart Please remove or make sure it appears in the correct subdirectory of the sourcepath. 2 errors
cody perry
PHP Development Techdegree Student 18,448 PointsI'm gonna try it on a different computer, i typed exactly what you had and it would not pass on my Laptop.
Steve Hunter
57,712 PointsYeah - that looks like there's something wrong with the challenge. Did you just enter the single line, like in my video, and you still get that error? Very strange, if so.
It isn't a browser issue, I don't think. I'm in Chrome too.
But you may want to refresh caches etc and make sure you have a clean reload of that challenge.