Reputation: 117
I have a simple error in a very simple start of a program that has me stumped. I've included comments of the errors on the appropriate lines. What am I missing? (Yes, I am a nube.)
package mainPack;
public class Bodies{
int mass;
int radius;
int xpos;
int ypos;
float xvel;
float yvel; //Syntax error, insert "}" to complete ClassBody
public Bodies(mass, radius, xpos, ypos, xvel, yvel){
}
} //Syntax error on token "}", delete this token
Upvotes: 0
Views: 74
Reputation: 14699
Your issue is that the parameters in your constructor have no data types.
NB:
Since your parameter names are the same as your instance variable names, you will need to use this
, as in:
public Bodies(int mass, int radius, int xpos, int ypos, float xvel, float yvel)
{
this.mass = mass;
this.radius = radius;
//...
}
where this.mass
refers to the instance variable mass
, as opposed to the passed parameter of the constructor.
For more information, check out the Oracle Tutorial on Java Constructors.
As an aside, on float
also from Oracle:
As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency.
Upvotes: 6
Reputation: 6809
package mainPack;
public class Bodies{
int mass;
int radius;
int xpos;
int ypos;
float xvel;
float yvel;
public Bodies(int mass, int radius, int xpos, int ypos, float xvel, float yvel){
this.mass = mass;
this.radius = radius;
this.xpos = xpos;
this.ypos = ypos;
this.xvel = xvel;
this.yvel = yvel;
}
}
You were missing the types for the arguments for the constructor. You will probably want to initialize the fields in the constructor, so I did it also.
Upvotes: 0
Reputation: 2402
You have to define the types of the parameters as well as their names in the constructor.
Upvotes: 0
Reputation: 500407
When declaring the constructor, you need to specify the types of its arguments:
public Bodies(int mass, int radius, int xpos, int ypos, float xvel, float yvel) {
Having done this, you also need to initialize the data members:
public Bodies(int mass, int radius, int xpos, int ypos, float xvel, float yvel) {
this.mass = mass;
...
Upvotes: 2