Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Implement a class Automobile

  1. #1
    Tiger is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Question Implement a class Automobile

    I can not figure this out 2 errors. WHats the issue with Car? If I use automobile instead of car I get almosty 30 errors

    //This is creating the constructor and initiliazing the variables, Make, Model and Price
    public class Automobile
    {
    String make;
    String model;
    String price;

    public int getInfo()
    {
    return make + model + price;
    }

    Car();
    {
    make = Toyota;
    model = Camry;
    price = $22055;
    }

    Car(string m, string mo, string p);
    {
    make = m;
    model = mo;
    price = p;
    }
    public static void main (String[] args)
    {
    Car carObj1, CarObj2;
    CarObj1 = new Car();
    Obj2 = new Car(Toyota, Camry, $22055);
    System.out.println("The Make, Model and Price of the car is: " + carObj1.getInfo());
    }
    }

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

    Default Re: Implement a class Automobile

    Quote Originally Posted by Tiger View Post
    I can not figure this out 2 errors. ...
    What errors? If you've posted the error messages, I don't see them. Please point them out to us.

  3. #3
    Tiger is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: Implement a class Automobile

    ----jGRASP exec: javac -g Automobile.java

    Automobile.java:13: error: <identifier> expected
    Car int;
    ^
    Automobile.java:20: error: invalid method declaration; return type required
    Car(str m, string mo, string p);
    ^
    2 errors

    ----jGRASP wedge2: exi

  4. #4
    Tiger is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: Implement a class Automobile

    Revised with new errors

    //This is creating the constructor and initiliazing the variables, Make, Model and Price
    public class Automobile
    {
    String make;
    String model;
    String price;
    }

    public string getInfo()
    {
    return make + model + price;
    }

    Automobile string;
    {
    make = Toyota;
    model = Camry;
    price = 22055;
    }

    Automobile(string m, string mo, string p);
    {
    make = m;
    model = mo;
    price = p;
    }
    public static void main (String[] args)
    {
    Car carObj1;
    CarObj1 = new Car();
    Automobile myAutomobile = new Autombile(Toyota, Camry, 22055);
    System.out.println("The Make, Model and Price of the car is: " + carObj1.getInfo());
    }
    }


    ----jGRASP exec: javac -g Automobile.java

    Automobile.java:9: error: class, interface, or enum expected
    public string getInfo()
    ^
    Automobile.java:12: error: class, interface, or enum expected
    }
    ^
    Automobile.java:15: error: class, interface, or enum expected
    {
    ^
    Automobile.java:17: error: class, interface, or enum expected
    model = Camry;
    ^
    Automobile.java:18: error: class, interface, or enum expected
    price = 22055;
    ^
    Automobile.java:19: error: class, interface, or enum expected
    }
    ^
    Automobile.java:22: error: class, interface, or enum expected
    {
    ^
    Automobile.java:24: error: class, interface, or enum expected
    model = mo;
    ^
    Automobile.java:25: error: class, interface, or enum expected
    price = p;
    ^
    Automobile.java:26: error: class, interface, or enum expected
    }
    ^
    Automobile.java:27: error: class, interface, or enum expected
    public static void main (String[] args)
    ^
    Automobile.java:30: error: class, interface, or enum expected
    CarObj1 = new Car();
    ^
    Automobile.java:31: error: class, interface, or enum expected
    Automobile myAutomobile = new Autombile(Toyota, Camry, 22055);
    ^
    Automobile.java:32: error: class, interface, or enum expected
    System.out.println("The Make, Model and Price of the car is: " + carObj1.getInfo());
    ^
    Automobile.java:33: error: class, interface, or enum expected
    }
    ^
    15 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

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

    Default Re: Implement a class Automobile

    You may be going about your coding wrong. If you can't use an IDE that flags your compiler errors immediately, then you should code in a step-wise fashion where you compile your classes early and often, perhaps after every 1-2 new lines of code, and most important, you should not add any new code until all current compiler errors have been fixed. If you don't do it this way, you risk ending up with a rat's nest of errors.

    Current problems include
    • You have semi-colons at the end of constructor and method declarations where they don't belong.
    • You're creating a class called Automobile and trying to use a class called Car. The Java compiler is strict and it's dumb, and it has no way that you meant to create an Automobile variable when you've declared it as Car.
    • You have statements that just aren't Java, like -- Automobile string; -- you can't make stuff up and hope it works. You will want to read your text to see how to create classes, constructors and methods, and follow their examples.
    • and many more...

  6. #6
    Tiger is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: Implement a class Automobile

    Here some changes I have implemented through your advice errors down to 7

    Java Code:
    //This is creating the constructor and initiliazing the variables, Make, Model and Price
    public class Automobile
     {
      String make;
      String model;
      String price;
      }
    
    	public string getInfo()
    	{
    		return make + model + price;
    		
    	Automobile string;
    	
    		make = Toyota
    		model = Camry
    		price = 22055
    	}
    	
    	Automobile(string m, string mo, string p)
    	{
    		make = m
    		model = mo
    		price = p
    	}
    	public static void main (String[] args)
    	{
    		Automobile;
    		Automobile myAutomobile = new Autombile(Toyota, Camry, 22055);
    		System.out.println("The Make, Model and Price of the car is: " + carObj1.getInfo());
    	}
    }
    ----jGRASP exec: javac -g Automobile.java

    Automobile.java:9: error: class, interface, or enum expected
    public string getInfo()
    ^
    Automobile.java:13: error: class, interface, or enum expected
    Automobile string;
    ^
    Automobile.java:15: error: class, interface, or enum expected
    make = Toyota
    ^
    Automobile.java:26: error: class, interface, or enum expected
    public static void main (String[] args)
    ^
    Automobile.java:29: error: class, interface, or enum expected
    Automobile myAutomobile = new Autombile(Toyota, Camry, 22055);
    ^
    Automobile.java:30: error: class, interface, or enum expected
    System.out.println("The Make, Model and Price of the car is: " + carObj1.getInfo());
    ^
    Automobile.java:31: error: class, interface, or enum expected
    }
    ^
    7 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.
    Last edited by Fubarable; 02-01-2013 at 04:01 AM. Reason: code tags added.

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

    Default Re: Implement a class Automobile

    I've added tags around your code in the post above to help your code retain its formatting.
    Again, study the structure of a class. One important requirement is that after declaring the class, it's code starts with an opening curly brace, {, then you have all the code in the class, and it finishes with a closing curly brace, }, that matches the opening. You seem to have a closing curly brace present a bit prematurely.

    But regardless, just throw this code out, it's not that big anyway, and to be blunt, it still is junk code, and start over first with skeleton class, nothing more than something like:

    Java Code:
    public class MyFoo {
    
    }
    Then check that it compiles. If it does, then add variables.

    Java Code:
    public class MyFoo {
      private int bar;
      private String baz;
    
    }
    Then again check that it compiles. If it does, then add a constructor.

    Java Code:
    public class MyFoo {
      private int bar;
      private String baz;
    
      public MyFoo(int bar, String baz) {
    
      }
    
    }
    Then again check that it compiles. If it does, then add code to the constructor's body.... etc
    Estrella likes this.

  8. #8
    Tiger is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: Implement a class Automobile

    I dont see the code tags you added.

    //This is creating the constructor and initiliazing the variables, Make, Model and Price
    public class Automobile
    {
    String make;
    String model;
    String price;
    }

    public string getInfo()
    {
    return make + model + price;

    Automobile string;

    make = Toyota
    model = Camry
    price = 22055

    Automobile(string m, string mo, string p)

    make = m
    model = mo
    price = p

    public static void main (String[] args)

    Automobile;
    Automobile myAutomobile = new Autombile(Toyota, Camry, 22055);
    System.out.println("The Make, Model and Price of the car is: " + carObj1.getInfo());
    }
    }
    Last edited by Tiger; 02-01-2013 at 04:14 AM.

  9. #9
    Tiger is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: Implement a class Automobile

    Oh you put the numbers beside it I see.

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

    Default Re: Implement a class Automobile

    No, I placed no numbers beside anything. I simply placed the tag [code] above the code block and the tag [/code] below the block of code:

    [code]
    public class {
    //....
    }
    [/code]

    And again, scrap that code; it's junk. Start over.

  11. #11
    Tiger is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: Implement a class Automobile

    OK lol Start over it took me 2 days to get that. Im starting over LOL

Similar Threads

  1. Replies: 15
    Last Post: 02-05-2012, 12:15 AM
  2. Replies: 0
    Last Post: 01-24-2011, 05:10 PM
  3. Replies: 1
    Last Post: 10-02-2009, 02:35 AM
  4. how to implement interface
    By makpandian in forum New To Java
    Replies: 1
    Last Post: 12-09-2008, 03:39 PM
  5. Implement a class Employee-Help due tmrw!
    By Britt7 in forum New To Java
    Replies: 3
    Last Post: 10-27-2008, 02:37 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
  •