Results 1 to 18 of 18
- 01-11-2013, 04:10 AM #1
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Coding question for program on cars
Guys I was doing a tutorial on objects, and I was trying to make a program that made two cars; a lexus and a ferrari. I made a speed, mph, and color. I tried to run but received 15 errors, all saying "class, interface, or enum expected", and pointing at the beginning letter of all my lines of code. What am I doing wrong? Thanks so, so much in advance.
Java Code:Class car { public static void main(String[] args) { int speed; int mph; String color; car ferrari = new car(); car lexus = new car(); lexus.mph=160; lexus.color="white"; lexus.speed=60; ferrari.speed=120; ferrari.mph=200; ferrari.color="red"; System.out.println("a ferrari's stats: " + ferrari.speed + ferrari.mph + ferrari.van + ferrari.color); System.out.println("a lexus's stats: " + lexus.speed + lexus.mph + lexus.van + lexus.color); } }
- 01-11-2013, 04:34 AM #2
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Coding question for program on cars
Class has to have a lowercase c, class.
- 01-11-2013, 05:24 AM #3
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: Coding question for program on cars
In fact, every key word in Java (including class) must be in all lower-case.Class has to have a lowercase c, class
- 01-11-2013, 05:27 AM #4
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Re: Coding question for program on cars
hey guys, thanks for the reply. I uncapitalized it, but I still get an error :(
What else could I be doing wrong...? In the main method, string should be capitalized shouldn't it? Ahh, I'm so frustrated >.< Thanks for the repliesJava Code:class car { public static void main(String[] args) { int speed; int mph; string color; car ferrari = new car(); car lexus = new car(); lexus.mph=160; lexus.color="white"; lexus.speed=60; ferrari.speed=120; ferrari.mph=200; ferrari.color="red"; System.out.println("a ferrari's stats: " + ferrari.speed + ferrari.mph + ferrari.color); System.out.println("a lexus's stats: " + lexus.speed + lexus.mph + lexus.color); } }
- 01-11-2013, 05:34 AM #5
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Coding question for program on cars
Yes, String should be capitalized.
You should also print the entire error for us if you can.
- 01-11-2013, 05:43 AM #6
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Re: Coding question for program on cars
ok I copy and pasted all of the errors :
Java Code:car.java:5: error: cannot find symbol string color; ^ symbol: class string location: class car car.java:8: error: cannot find symbol lexus.mph=160; ^ symbol: variable mph location: variable lexus of type car car.java:9: error: cannot find symbol lexus.color="white"; ^ symbol: variable color location: variable lexus of type car car.java:10: error: cannot find symbol lexus.speed=60; ^ symbol: variable speed location: variable lexus of type car car.java:11: error: cannot find symbol ferrari.speed=120; ^ symbol: variable speed location: variable ferrari of type car car.java:12: error: cannot find symbol ferrari.mph=200; ^ symbol: variable mph location: variable ferrari of type car car.java:13: error: cannot find symbol ferrari.color="red"; ^ symbol: variable color location: variable ferrari of type car car.java:14: error: cannot find symbol System.out.println("a ferrari's stats: " + ferrari.speed + ferrari.mph + ferrari.color); ^ symbol: variable speed location: variable ferrari of type car car.java:14: error: cannot find symbol System.out.println("a ferrari's stats: " + ferrari.speed + ferrari.mph + ferrari.color); ^ symbol: variable mph location: variable ferrari of type car car.java:14: error: cannot find symbol System.out.println("a ferrari's stats: " + ferrari.speed + ferrari.mph + ferrari.color); ^ symbol: variable color location: variable ferrari of type car car.java:15: error: cannot find symbol System.out.println("a lexus's stats: " + lexus.speed + lexus.mph + lexus.color); ^ symbol: variable speed location: variable lexus of type car car.java:15: error: cannot find symbol System.out.println("a lexus's stats: " + lexus.speed + lexus.mph + lexus.color); ^ symbol: variable mph location: variable lexus of type car car.java:15: error: cannot find symbol System.out.println("a lexus's stats: " + lexus.speed + lexus.mph + lexus.color); ^ symbol: variable color location: variable lexus of type car 13 errors
- 01-11-2013, 05:50 AM #7
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Coding question for program on cars
You already know the answer to the first error. Fix that first and then start working on the other ones. It cannot find the symbols, so the following variables are missing
For each of the instances of your Car object.Java Code:mph, color,speed
- 01-11-2013, 05:51 AM #8
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Re: Coding question for program on cars
but I do have them, look they are declared at the beginning:
Java Code:class car { public static void main(String[] args) { int speed; int mph; string color; car ferrari = new car(); car lexus = new car(); lexus.mph=160; lexus.color="white"; lexus.speed=60; ferrari.speed=120; ferrari.mph=200; ferrari.color="red"; System.out.println("a ferrari's stats: " + ferrari.speed + ferrari.mph + ferrari.color); System.out.println("a lexus's stats: " + lexus.speed + lexus.mph + lexus.color); } }
- 01-11-2013, 05:54 AM #9
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Coding question for program on cars
I see that.
Oracle doesn't seem to have a guide to scope, but this is an OOP concept so you should know it.
Are those variables visible to the class? You also need to work on formatting the code a little bit better
Java Code:class Car { public static void main(String[] args) { int speed; int mph; string color; car ferrari = new car(); car lexus = new car(); lexus.mph=160; lexus.color="white"; lexus.speed=60; ferrari.speed=120; ferrari.mph=200; ferrari.color="red"; System.out.println("a ferrari's stats: " + ferrari.speed + ferrari.mph + ferrari.color); System.out.println("a lexus's stats: " + lexus.speed + lexus.mph + lexus.color); } }
- 01-11-2013, 06:01 AM #10
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
- 01-11-2013, 06:09 AM #11
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Coding question for program on cars
Variables, methods and other items have scope, meaning what can see them. You can set the scope by using a modifier
You can also limit the visibility by placement.Java Code:private int a; public int a; public void getPizza() private void getPizza()
Notice that there are two variables, both named a? The print method will print out 10. This is due to that variable being visible to the entire class. The variable with the number 5 is only accessible in the main method.Java Code:class Test{ static int a = 10; public static void main(String[] args){ int a = 5; print(); } public static void print(){ System.out.println(a); } }
- 01-11-2013, 06:13 AM #12
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Re: Coding question for program on cars
But how does that apply to my code? I tried making the ints and the string public, but then it said that they were an illegal start of expression.... What can I do to fix my code?
- 01-11-2013, 06:17 AM #13
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Coding question for program on cars
Look where they are placed. They are inside the main method. When you try to make an instance of your class with
and then called their properties withJava Code:Car ferrari = new Car(); Car lexus = new Car();
Are those variables visible? Remember what I said about placement?Java Code:lexus.color="white"; lexus.speed=60; ferrari.speed=120; ferrari.mph=200; ferrari.color="red"
- 01-11-2013, 06:23 AM #14
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Re: Coding question for program on cars
I am really confused... I don't understand; I think that they are visible. Let me give you the code again; I declare the variables first.
Java Code:class car { public static void main(String[] args) { int speed; int mph; string color; car ferrari = new car(); car lexus = new car(); lexus.mph=160; lexus.color="white"; lexus.speed=60; ferrari.speed=120; ferrari.mph=200; ferrari.color="red"; System.out.println("a ferrari's stats: " + ferrari.speed + ferrari.mph + ferrari.color); System.out.println("a lexus's stats: " + lexus.speed + lexus.mph + lexus.color); } }
- 01-11-2013, 06:26 AM #15
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Coding question for program on cars
They are visible, to everything inside the main method. You are trying to access them at the class level. Here is an example of what I am talking about.
- 01-11-2013, 06:33 AM #16
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Re: Coding question for program on cars
uh, I looked through it and I don't quite understand it. So do I not access them at a class level...? o_O
EDIT: I got it! I just put them OUTSIDE of the main method. I have absolutely no idea why this matters though. Should I declare all vars and strings outside of the main method?
- 01-11-2013, 06:54 AM #17
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Coding question for program on cars
Very good!
You do however need to learn what it means to put variables outside any method. For the simple programs you seem to be working on now, it doesn't seem like it's an issue. However, when you start doing programming that is not basic, you want to be selective. The reason why this worked outside the main method and not it in it is because you are creating an instance of your car class. You are essentially creating a car object. Now, objects have variables called instance variables and they belong to the object. You need these to be outside in the class in order for them to be considered this. Here is a simple rundown, and I hope there are no errors.
I created a simple program that creates car and stores them in a garage (Array)
Here is my car class, which is turned into a car object. Notice that the variables name, color, speed and weight are instance variables, or part of the object. When I create the object, these are assigned values. The method below it that looks weird is called a constructor, it's what is called when I create the object with
Java Code:Car myCar = new Car("Lexus", "Red", 120, 2500);Here is my Garage, in which the cars are effectively stored. I create the Car objects and store them in an array. I then print out the values of the car with a loop with the syntax you were using. For example:Java Code:public class Car { String name; String color; int speed; int weight; public Car(String name, String color, int speed, int weight){ this.name = name; this.color = color; this.speed = speed; this.weight = weight; } }
Java Code:Car myCar = new Car("Lexus", "Red", 120, 2500); System.out.println(myCar.name);I hope this makes it a little bit more clear to you. Now to talk about your code.Java Code:public class Garage { static Car[] myGarage = new Car[4]; public static void main(String[] args){ System.out.println("Welcome to my garage!"); myGarage[0] = new Car("Lexus", "Red", 120, 2500); myGarage[1] = new Car("Ferrari", "White", 180, 1100); myGarage[2] = new Car("Toyota", "Blue", 20, 4000); myGarage[3] = new Car("Ford", "Black", 100, 3821); printCars(); } public static void printCars(){ for(int i = 0; i < myGarage.length; i++){ System.out.println("Car: " + myGarage[i].name); System.out.println("Color: " + myGarage[i].color); System.out.println("Speed: " + myGarage[i].speed); System.out.println("Weight: " + myGarage[i].weight + "\n"); } } }
You are creating the same objects I am essentially, the instance variables are different though. When you callJava Code:class car { String color; int speed; int mph; public static void main(String[] args) { int speed; int mph; car ferrari = new car(); car lexus = new car(); lexus.mph=160; lexus.color="white"; lexus.speed=60; ferrari.speed=120; ferrari.mph=200; ferrari.color="red"; System.out.println("a ferrari's stats: " + ferrari.speed + ferrari.mph + ferrari.color); System.out.println("a lexus's stats: " + lexus.speed + lexus.mph + lexus.color); } }
You are invoking the default constructor, which is not visible but exists. You create the object car and fill its variable with nothing (Default values). When you callJava Code:car ferrari = new car();
You are assigning that objects instance variable to the value 200.Java Code:ferrari.mph = 200;
I suggest that you format your code properly, and read some more of the tutorials that Oracle provides.Last edited by Wnt2bsleepin; 01-11-2013 at 06:59 AM.
- 01-11-2013, 04:18 PM #18
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Similar Threads
-
question on swing coding?
By MW130 in forum New To JavaReplies: 8Last Post: 01-07-2013, 02:22 AM -
how to write the coding for this type of question
By sf1213 in forum New To JavaReplies: 2Last Post: 06-06-2012, 10:44 AM -
guys....im having problem with this coding question.do help me
By ah choy in forum New To JavaReplies: 8Last Post: 05-03-2012, 06:37 PM -
question about coding conventions
By gib65 in forum New To JavaReplies: 8Last Post: 08-05-2010, 04:24 AM -
A difficult question - efficient coding?
By tyang in forum Advanced JavaReplies: 3Last Post: 02-05-2010, 02:48 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks