Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By Wnt2bsleepin

Thread: Coding question for program on cars

  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default 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);
        }
    }

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Coding question for program on cars

    Class has to have a lowercase c, class.

  3. #3
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Coding question for program on cars

    Class has to have a lowercase c, class
    In fact, every key word in Java (including class) must be in all lower-case.

  4. #4
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Coding question for program on cars

    hey guys, thanks for the reply. I uncapitalized it, but I still get an error :(

    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);
      }
    }
    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 replies

  5. #5
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Coding question for program on cars

    Yes, String should be capitalized.

    You should also print the entire error for us if you can.

  6. #6
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default 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

  7. #7
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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

    Java Code:
    mph, color,speed
    For each of the instances of your Car object.

  8. #8
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default 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);
      }
    }

  9. #9
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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);
        }
    }

  10. #10
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Coding question for program on cars

    Quote Originally Posted by Wnt2bsleepin View Post
    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);
        }
    }
    Um, how do i know if they are visible to the class?

  11. #11
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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

    Java Code:
    private int a;
    public int a;
    
    public void getPizza()
    private void getPizza()
    You can also limit the visibility by placement.

    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); 
          }
            
            
    }
    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.

  12. #12
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default 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?

  13. #13
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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

    Java Code:
         Car ferrari = new Car();
         Car lexus = new Car();
    and then called their properties with

    Java Code:
            lexus.color="white";
            lexus.speed=60;
            ferrari.speed=120;
            ferrari.mph=200;
            ferrari.color="red"
    Are those variables visible? Remember what I said about placement?

  14. #14
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default 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);
      }
    }

  15. #15
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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.

  16. #16
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default 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?

  17. #17
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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);
    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; 
    	}
    	
    	
    	
    
    }
    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:
    Car myCar = new Car("Lexus", "Red", 120, 2500);
    System.out.println(myCar.name);
    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");
    			
    			
    		}
    	}
    
    }
    I hope this makes it a little bit more clear to you. Now to talk about your code.

    Java 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 creating the same objects I am essentially, the instance variables are different though. When you call

    Java Code:
    car ferrari = new car();
    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 call

    Java Code:
    ferrari.mph = 200;
    You are assigning that objects instance variable to the value 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.
    MW130 likes this.

  18. #18
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Coding question for program on cars

    thank you so much for taking your time to teach me that. Also, should I always declare variables/ints outside of the main method?

Similar Threads

  1. question on swing coding?
    By MW130 in forum New To Java
    Replies: 8
    Last Post: 01-07-2013, 02:22 AM
  2. how to write the coding for this type of question
    By sf1213 in forum New To Java
    Replies: 2
    Last Post: 06-06-2012, 10:44 AM
  3. Replies: 8
    Last Post: 05-03-2012, 06:37 PM
  4. question about coding conventions
    By gib65 in forum New To Java
    Replies: 8
    Last Post: 08-05-2010, 04:24 AM
  5. A difficult question - efficient coding?
    By tyang in forum Advanced Java
    Replies: 3
    Last Post: 02-05-2010, 02:48 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •