Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27
Like Tree2Likes

Thread: Using a Constructor in Java

  1. #21
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Using a Constructor in Java

    I renamed the file to Car.java and tried to run java Car.java and same compile errors

  2. #22
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using a Constructor in Java

    Ah, sorry, I see. You're also using Public, not public as wsaryada points out in his answer above.

    Yes, Java is *that* persnickety. Most programming languages are.
    Last edited by Fubarable; 05-16-2012 at 04:20 AM.

  3. #23
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Using a Constructor in Java

    I just upated all instances from Public to public using find replace and am getting these compile errors:

    Car.java:1: error: class, interface, or enum expected
    public Class Car
    ^
    Car.java:4: error: class, interface, or enum expected
    String model;
    ^
    Car.java:5: error: class, interface, or enum expected
    String trans;
    ^
    Car.java:7: error: class, interface, or enum expected
    public int getInfo()
    ^
    Car.java:10: error: class, interface, or enum expected
    }
    ^
    Car.java:15: error: class, interface, or enum expected
    model = Mustang;
    ^
    Car.java:16: error: class, interface, or enum expected
    trans = Manual;
    ^
    Car.java:17: error: class, interface, or enum expected
    }
    ^
    Car.java:22: error: class, interface, or enum expected
    model = mo;
    ^
    Car.java:23: error: class, interface, or enum expected
    trans = t;
    ^
    Car.java:24: error: class, interface, or enum expected
    }
    ^
    Car.java:26: error: class, interface, or enum expected
    public static void main (String[] args)
    ^
    Car.java:29: error: class, interface, or enum expected
    carObj1 = new Car();
    ^
    Car.java:30: error: class, interface, or enum expected
    carObj2 = new Car(Ford, Mustang, Manual);
    ^
    Car.java:31: error: class, interface, or enum expected
    System.out.println("The Make, Model and Transmission of the car is: " +
    carObj1.getInfo());
    ^
    Car.java:32: error: class, interface, or enum expected
    }
    ^
    16 errors

  4. #24
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using a Constructor in Java

    Next ... it's class, not Class.

    Yes, you will get this project finished eventually,... promise!

  5. #25
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Using a Constructor in Java

    Several more compile errors

    Car.java:19: error: cannot find symbol
    Car(string m, string mo, string t)
    ^
    symbol: class string
    location: class Car
    Car.java:19: error: cannot find symbol
    Car(string m, string mo, string t)
    ^
    symbol: class string
    location: class Car
    Car.java:19: error: cannot find symbol
    Car(string m, string mo, string t)
    ^
    symbol: class string
    location: class Car
    Car.java:9: error: incompatible types
    return make + model + trans;
    ^
    required: int
    found: String
    Car.java:14: error: cannot find symbol
    make = Ford;
    ^
    symbol: variable Ford
    location: class Car
    Car.java:15: error: cannot find symbol
    model = Mustang;
    ^
    symbol: variable Mustang
    location: class Car
    Car.java:16: error: cannot find symbol
    trans = Manual;
    ^
    symbol: variable Manual
    location: class Car
    Car.java:30: error: cannot find symbol
    carObj2 = new Car(Ford, Mustang, Manual);
    ^
    symbol: variable Ford
    location: class Car
    Car.java:30: error: cannot find symbol
    carObj2 = new Car(Ford, Mustang, Manual);
    ^
    symbol: variable Mustang
    location: class Car
    Car.java:30: error: cannot find symbol
    carObj2 = new Car(Ford, Mustang, Manual);
    ^
    symbol: variable Manual
    location: class Car
    10 errors
    Last edited by Rahim2312; 05-16-2012 at 04:34 AM.

  6. #26
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using a Constructor in Java

    Quote Originally Posted by Rahim2312 View Post
    Several more compile errors
    Java Code:
    Car.java:19: error: cannot find symbol
        Car(string m, string mo, string t)
            ^
      symbol:   class string
      location: class Car
    Car.java:19: error: cannot find symbol
        Car(string m, string mo, string t)
                      ^
      symbol:   class string
      location: class Car
    Car.java:19: error: cannot find symbol
        Car(string m, string mo, string t)
                                 ^
      symbol:   class string
      location: class Car
    It's pointing at the "s" in string. There is no class called string (hint see what we've been working on for the last few hours -- it's the same type of error, though in the other direction).

    Java Code:
    Car.java:9: error: incompatible types
            return make + model + trans;
                                ^
      required: int
      found:    String
    The error tells you exactly what's wrong. Your method signature states that it will return an int, but in reality, you're returning a String.


    Java Code:
    Car.java:14: error: cannot find symbol
            make = Ford;
                   ^
      symbol:   variable Ford
      location: class Car
    Car.java:15: error: cannot find symbol
            model = Mustang;
                    ^
      symbol:   variable Mustang
      location: class Car
    Car.java:16: error: cannot find symbol
            trans = Manual;
                    ^
      symbol:   variable Manual
      location: class Car
    You're assigning make, model and trans to variables or parameters that don't exist, and so Java is rightfully complaining.

    Java Code:
    Car.java:30: error: cannot find symbol
            carObj2 = new Car(Ford, Mustang, Manual);
                              ^
      symbol:   variable Ford
      location: class Car
    Car.java:30: error: cannot find symbol
            carObj2 = new Car(Ford, Mustang, Manual);
                                    ^
      symbol:   variable Mustang
      location: class Car
    Car.java:30: error: cannot find symbol
            carObj2 = new Car(Ford, Mustang, Manual);
                                             ^
      symbol:   variable Manual
      location: class Car
    You're passing in parameters that have never been declared and so don't exist. Look and see how your example programs do this, and try to emulate them.

  7. #27
    Arun_N is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default Re: Using a Constructor in Java

    Where You defined Ford, Mustang, Manual?
    If these are not defined please pass parameters within double quotes.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 5
    Last Post: 12-14-2011, 02:47 PM
  2. Java Constructor Question
    By ashly in forum New To Java
    Replies: 6
    Last Post: 03-09-2010, 03:10 AM
  3. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 10:01 AM
  5. Help with constructor in java
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 02:00 AM

Posting Permissions

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