-
October 29th, 2003, 06:46 AM
#1
Help creating class in Java!!
first of all i want to say that this post is completly not security related and i thought that this forum might matches it the best out of them all. Anyways, this sounds really stupid but i jhave written up a class in Java, now all i want to do is call it from main(). The problem is that i forgot how to do this and none of my lame text books or any tutorials on this site or sun.java.com tell me how to do this. Anyways, this is my code so far, it is a stupid program so dont tell me it is stupid cause i already know:
Code:
public class Main
{
public class Car
{
private int year;
private int size;
private int gear;
public void set(int y, int s, int g)
{
year=y;
size=s;
gear=g;
}
public void tell()
{
System.out.println("You car is " +size +" big and ");
System.out.print("was made in "+year +" and has ");
System.out.print(gears +" gears.");
}
}
public static void main(String[] args)
{
Car volvo = new Car;
volvo.set(1998,100,5)
volvo.tell();
}
}
the files name is Main. Anyways when i try to compile this program i get one error on the line that says . How do i make the stupid object volvo!!!????? God i hate this language...anyways any help would be very much appriciated! Thanks
Support your right to arm bears.
^^This was the first video game which i played on an old win3.1 box
-
October 29th, 2003, 06:56 AM
#2
I don't see any probelms with your code that I can easily spot, however looking at a simple example in one of my texts, I notice that they declare classes outside of the Programs main class.
To make that clear.
Try defiing your Car class outside of your main class. Here is some code from my book to demonstrate this:
The book is Teach Yourself Java by Joseph O'Niel pg 129
class Point3D {
double x;
double y;
double z;
}
class Point3DExample {
public static void main(String args[]) {
Point3D p = new Point3D;
p.x = 1.1;
p.y = 3.4;
p.z = -1.2;
System.out.println("p.x = " + p.x);
System.out.println("p.y = " + p.y);
System.out.println("p.z = " + p.z);
}
}
Hope this helps
DeafLamb
-
October 29th, 2003, 07:13 AM
#3
so you think my code should look like this instead?
Code:
public class Car
{
private int year;
private int size;
private int gear;
public void set(int y, int s, int g)
{
year=y;
size=s;
gear=g;
}
public void tell()
{
System.out.println("You car is " +size +" big and ");
System.out.print("was made in "+year +" and has ");
System.out.print(gears +" gears.");
}
}
public class Main
{
public static void main(String[] args)
{
Car volvo = new Car();
volvo.set(1998,100,5);
volvo.tell();
}
}
That didnt work either....this time i got two errors, one complaining that it coudlnt reconize gears in my
System.out.print(gears +" gears.");
and another one saying " class Main is public, should be declared in a file named Main.java" i am using Netbeans not updated cause i am too lazy to update...thanks for the help deaflamb, but it still isnt working
Support your right to arm bears.
^^This was the first video game which i played on an old win3.1 box
-
October 29th, 2003, 07:39 AM
#4
you got the error because you had it as gears when the variable was gear.
If you do make it a separate file for each class and change gears to gear, it should work.
also try switching the order of the classes to the Main class being first then the Car class second. It might not require two different files if you do it that way. Jcreator will let you have many classes in the same file. I don't know about netbeans though. Yes I forgot, thanks for reminding me weaver. Just put the Main class first then the Car class and take out the public keyword before the Car class.
-
October 29th, 2003, 02:26 PM
#5
Junior Member
I don't know why you made a second class in this example. You can have a main method in any class. It will only run if you try to run this class or explicitly call it.
I see 2 errors with your code.
#1) You can have multiple classes in one file. You can't, however, have more than one public class in the same file. The file also has to have the same name as the public class.
#2) When you are trying to output the text, you are trying to reference a variable that doesn't exist - gears. You have a variable called gear.
-
October 29th, 2003, 02:47 PM
#6
Senior Member
actually you can have your set method change to a constructor
Code:
public class Car
{
private int year;
private int size;
private int gear;
public Car(int y, int s, int g)
{
year = y;
size = s;
gear = g;
}
public void tell()
{
System.out.print("You car is " + size +" big and ");
System.out.print("was made in "+ year +" and has ");
System.out.println(gear +" gears.");
}
public static void main(String[] args)
{
Car volvo = new Car(1998,100,5);
volvo.tell();
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|