I am getting errors with my code. this particular class is supposed to represent
ID: 3624289 • Letter: I
Question
I am getting errors with my code. this particular class is supposed to represent all the objects in space. I am unsure how to set up the constructor though?I am getting errors when i try to make my methods. Errors that say certain things can't be resolved to variables...
my code:
package comets;
public abstract class SpaceObject
{
public static double playfieldHeight;
public static double playfieldWidth;
protected double xVelocity;
protected double yVelocity;
//how do I set up this constructor?
public SpaceObject(double xPos, double yPos, double xVelocity, double yVelocity, double radius)
{
}
//my methods
double getRadius()
{
return radius; //I get an error here that says radius cannot be resolved to variable
}
double getXPosition()
{
return xPos; //same here
}
double getYPosition()
{
return yPos; // same here
}
void move()
{
}
boolean overlapping(SpaceObject rhs)
{
return
}
}
Explanation / Answer
from what you provided, I can only give you a somewhat nondetailed solution but here we go:
package comet;
public class SpaceObject
{
private double xPos = 0;
private double yPos= 0;
private double xVelocity= 0;
private double radius = 0;
public static double playfieldHeight;//Don't know what these variables refer to
public static double playfieldWidth;//Don't know what these variables refer to
// I imagine they are the size in which the objects will be located, if so you would
//assign their values here while initializing or within the constructor
// notice that doing so will make them immutable afterwards
public SpaceObject(double xPos, double yPos, double xVelocity, double radius) {
this.xPos = xPos;
this.yPos = yPos;
this.xVelocity = xVelocity;
this.radius = radius;
}
//my methods
double getRadius()
{
return radius; //fixed
}
double getXPosition()
{
return xPos; //same here
}
double getYPosition()
{
return yPos; // same here
}
void move(double xPos, double yPos)
{
//move to the given coordinate along x-axis and y-axis,
//could be edited to be better but this is just an example
this.xPos = xPos;
this.yPos = yPos;
}
boolean overlapping(SpaceObject rhs)
{
//this would not go in this class per se in my opinion, and would make more sense
//to do in another, lets call it Universe, this class would host a list of Space Objects
// and check if they overlap with each other
//again this is just what I can infer by the little details i was given
//but I guess a calculation with the radius will lead to a conclusion of whether the objects overlap could be made
return false;
}
public double getxVelocity() {
return xVelocity;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.