Results 1 to 11 of 11
Thread: Implement a class Automobile
- 02-01-2013, 12:54 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 9
- Rep Power
- 0
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());
}
}
-
Re: Implement a class Automobile
- 02-01-2013, 01:41 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 9
- Rep Power
- 0
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
- 02-01-2013, 02:21 AM #4
Member
- Join Date
- Feb 2013
- Posts
- 9
- Rep Power
- 0
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.
-
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...
- 02-01-2013, 02:59 AM #6
Member
- Join Date
- Feb 2013
- Posts
- 9
- Rep Power
- 0
Re: Implement a class Automobile
Here some changes I have implemented through your advice errors down to 7
----jGRASP exec: javac -g Automobile.javaJava 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()); } }
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 03:01 AM. Reason: code tags added.
-
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:
Then check that it compiles. If it does, then add variables.Java Code:public class MyFoo { }
Then again check that it compiles. If it does, then add a constructor.Java Code:public class MyFoo { private int bar; private String baz; }
Then again check that it compiles. If it does, then add code to the constructor's body.... etcJava Code:public class MyFoo { private int bar; private String baz; public MyFoo(int bar, String baz) { } }
- 02-01-2013, 03:10 AM #8
Member
- Join Date
- Feb 2013
- Posts
- 9
- Rep Power
- 0
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 03:14 AM.
- 02-01-2013, 03:18 AM #9
Member
- Join Date
- Feb 2013
- Posts
- 9
- Rep Power
- 0
Re: Implement a class Automobile
Oh you put the numbers beside it I see.
-
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.
- 02-01-2013, 03:25 AM #11
Member
- Join Date
- Feb 2013
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Does an anonymous class have to extend a class or implement an interface ?
By fatabass in forum New To JavaReplies: 15Last Post: 02-04-2012, 11:15 PM -
Writing a Class to Implement ListSelectionModel Directly?
By kreyszig in forum AWT / SwingReplies: 0Last Post: 01-24-2011, 04:10 PM -
Help with implement class programming assignment
By ALH813 in forum EclipseReplies: 1Last Post: 10-02-2009, 01:35 AM -
how to implement interface
By makpandian in forum New To JavaReplies: 1Last Post: 12-09-2008, 02:39 PM -
Implement a class Employee-Help due tmrw!
By Britt7 in forum New To JavaReplies: 3Last Post: 10-27-2008, 01:37 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks