Hi,

just doing a uni assignment and have completed a majority of it, just asking if someone could please proofread and help me out with the last line as i can't get it to compile.

heres my instructions:
All objects that float through space are instances of the SpaceObject class (or one of its
various subclasses). Your first task is to define some basic code for the SpaceObject
class. As you might expect, a space object has several attributes associated with it, a
constructor, and several methods. A description of each of these members is given
below, and you need to convert these descriptions into actual code. Your code must obey
the Golden Rule of OO (described at the end of your Tutorial 3 notes).
Attribute "space" of type Space.
Attribute "x" of type double.
Attribute "y" of type double.
Attribute "radius" of type double.
Attribute "hDirection" of type int.
Attribute "vDirection" of type int.
Attribute "hDelta" of type double.
Attribute "vDelta" of type double.
Constructor taking parameters for space, x, y, radius.
This constructor must initialise the 4 corresponding attributes from the 4 parameters.
Method getSpace which returns attribute "space".
Method getX which returns attribute "x".
Method getY which returns attribute "y".
Method getRadius which returns attribute "radius".
Method getHDelta which returns attribute "hDelta".
Method getVDelta which returns attribute "vDelta".
Method getHDirection which returns attribute "hDirection".
Method getVDirection which returns attribute "vDirection".
Method setHDelta which updates attribute "hDelta" from a parameter.
Method setVDelta which updates attribute "vDelta" from a parameter.
Method setHDirection which updates attribute "hDirection" from a parameter.
Method setVDirection which updates attribute "vDirection" from a parameter.
Method move taking parameters dx and dy (both of type double).
This method must update attributes "x" and "y" by adding dx to "x" and adding dy to "y".

i've been able to do everything except the method move.

here's what i've been able to do so far:

public abstract class SpaceObject
{
    //
    //ATTTRIBUTES
    //
    
    private Space space;
    
    private double x;
    
    private double y;
    
    private double radius;
    
    private int hDirection;
    
    private int vDirection;
    
    private double hDelta;
    
    private double vDelta;
    

    public SpaceObject( Space space, double x, double y, double radius)
    {
        }

    public Space getSpace()
    {
        return space;
    }
    
             
    public double getX()
    {
        return x;
    }
    
    public double getY()
    {
        return y;
    }
    
    public double getRadius()
    {
        return radius;
    }
    
    public double getHDelta()
    {
        return hDelta;
    }
    
    public double getVDelta()
    {
        return vDelta;
    }
    
    public int getHDirection()
    {
        return hDirection;
    }
    
    public int getVDirection()
    {
        return vDirection;
    }
    
    public void setHDelta(double x)
    {
        hDelta = x;
    }
    
    public void setVDelta(double y)
    {
        vDelta = y;
    }
    
    public void setHDirection(int x)
    {
        hDirection = x;
    }
    
    public void setVDirection(int y)
    {
        vDirection = y;
    }
    
    
    public void move(double dx, double dy)
    {
        x = dx;
        y = dy;
     }
}

all help is appreciated

Recommended Answers

All 4 Replies

It compiles pretty well here, u should check if there is anything wrong with the rest of your code!

Sorry i left out the most important bit of the instructuctions, this follows on from what i had written:

Although your first task is not complete just yet, you may stop at this point and try to
compile and test your code. One way to test your code is to rightclick
on your
SpaceObject class and create a new instance. Since we don't have a space object at the
moment, we will use null as the first parameter. So, for example, you can try using these
parameter values: null, 100.0, 200.0, 10.0. A new SpaceObject will appear in the bottom
pane of the BlueJ window. Rightclick
on this object and inspect its attributes to see if
they contain the expected values. Also, test your move() method by rightclicking
on the
object and selecting move(dx,dy) with parameters 1.0 and 2.0. If you now inspect the
attributes of your object, the "x" attribute should now have value 101.0 and the "y"
attribute should now have value 202.0.

this is done in bluej. now when i do this, my values come out as 1.0 and 2.0 respectively for x and y.

but i have noticed when i right click the object to test move(), it is displayed as "void move(dx, dy)" not "move(dx, dy)" as is written in my outline.

You need to actually add 'dx' and 'dy' to the existing values of x and y. You have set them equal instead of adding.

Also, your constructor needs to set the values of your property variables equal to the parameter values that were passed. Your current constructor does not do anything as posted.

I don't understand why it would display 101.1 and 202.0 because there is no line in the code that assigns the values of x and y . you should put in some assignment at the constructor class or a a setX and setY methods to set the values of x and y to the created object. Sth like this shold work

public SpaceObject( Space space, double x, double y, double radius)
{
    this.x = x;
    this.y = y ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.