Hi guys this is for an assignment, its the last part and its not working:sad:

heres the instructions i've been given:

All SpaceObjects float through "Space", so we will need to also define a Space class.
That is the goal of this task. Similarly to task 1, each attribute, constructor and method
will be described below and you must convert that description into code.
Attribute "width" of type double.
Attribute "height" of type double.
Attribute "playerShip" of type PlayerShip.
Attribute "spaceObjects" of type SpaceObject[] (that is an array of SpaceObjects).
Attribute "running" of type boolean.
Constructor taking parameters for width and height.
The Space constructor must do the following:
• initialise the "width" attribute from the parameter.
• initialise the "height" attribute from the parameter.
• initialise the "playerShip" object with a new PlayerShip object. This object
should be created using the PlayerShip(space,x,y) constructor. For the first
parameter, use "this" space object as the parameter. For the x and y parameters,
use values so that the PlayerShip will appear in the centre of space. Hint: this
space has width and height attributes. You can find the centre of space by
dividing these intervals by 2.
• initialise the "spaceObjects" attribute with a new array big enough to store 10
space objects (i.e. new SpaceObject[10]).
• initialise the "running" attribute to true.
Method getWidth which returns attribute "width".
Method getHeight which returns attribute "height".
Method getPlayerShip which returns attribute "playerShip".
Method getSpaceObjects which returns attribute "spaceObjects".
Method isRunning which returns attribute "running".
Method step which just prints out "Yippee!".
Method run which calls the step() method repeatedly in a never ending loop. It should
work exactly as follows:
The run() method should contain a loop that runs forever. Each time through the loop,
three lines should execute:
• A line to invoke the step() method.
• A line to invoke the notifyListeners() method.
• A line to sleep for 40 milliseconds. For this, use one of the methods in the given
"Util" class.
The gameOver() method which contains exactly the following three instructions:
• Set the running attribute to false.
• Invoke the notifyListeners() method.
• Invoke the stop() method.
Some points in case you were wondering:
• The step() method will eventually be responsible for handling what happens in
each 40 millisecond time slice of the game.
• The run() method will run the game by repeatedly invoking step every 40
milliseconds.
• The notifyListeners() method will let other guys who are listening, like the user
interface, know when something happens in the game so that it has the
opportunity to update the pixels on the screen.
• The gameOver() method will stop the run() method, even though the run()
method looks like it will run forever. This is the behaviour we have inherited
from the Thread class (since we have defined "class Space extends Thread"). The
Thread class provides methods start(), run() and stop(). start() will cause the run()
method to run in the background. stop() will interrupt the run() method and stop
it.
You can test your code by rightclicking
on the Space class in BlueJ and creating a new
instance. A new space object will appear in the bottom pane of the BlueJ window. If you
rightclick
on the space object and invoke/call the "start" method, you should see
"Yippee!" printed repeatedly forever, as this is the default behaviour of "step" that you
have coded. If you inspect the attributes of the space object while it is running, you
should see "running == true". To stop this thread, invoke the "gameOver" method. If
you inspect the attributes of the space object again after it has stopped running, you
should now see "running == false". This is directly displaying the value of the "running"
attribute which you set to "true" in the constructor, and which you later set to "false" in
the gameOver() method.

I can't get my gameOver() method to compile it says that its deprecated.

also with my constructor for where i've initialised playerShip, i've thrown in some values for the ship but how do i write the code so that it makes it as it says in the instructions :
"You can find the centre of space by
dividing these intervals by 2."

all help is appreciated

heres my code:

import java.util.*;

public class Space extends Thread
{
    // STUDENTS - INSERT YOUR CODE HERE - DELETE THIS COMMENT

   //
   //ATTRIBUTES
   //
   
   private double width;
    
   private double height;
    
   private PlayerShip playerShip;
    
   private SpaceObject[] spaceObjects;
    
   private boolean running;
   
   //
   //CONSTRUCTORS
   //
    
   public Space( double width, double height)
   {
       this.width = width;
       this.height = height;
       PlayerShip playerShip = new PlayerShip (this, 40, 80);
       
       SpaceObject[] spaceObjects = new SpaceObject[10];       
       this.running = true;
       
       }

    //
    //METHODS
    //

    public double getWidth()
    {
        return width;
    }
    
    public double getHeight()
    {
        return height;
    }
    
    public PlayerShip getPlayerShip()
    {
        return playerShip;
    }
    
    public SpaceObject[] getSpaceObjects()
    {
        return spaceObjects;
    }
    
    public boolean isRunning()
    {
        return running;
    }
    
    public void step()
    {
        System.out.println("Yippee!");
    }
    
    
    public void run()
    {       while (running==true)
        {
            step();
            notifyListeners();
            sleep(40);
        }
        notifyListeners();
    }
    
    public static void sleep (long ms)
    {
        try
        {
            Thread.sleep(40);
        }
        catch (Exception e)
        {
        }
    }
    
    public void gameOver()
    {
        running = false;
        notifyListeners();
        stop();
    }
    
    
    // STUDENTS DO NOT NEED TO TOUCH ANY CODE BELOW THIS LINE
    private List listeners = new ArrayList();
    public void addListener(Listener listener)
    {
        listeners.add(listener);
    }
    private void notifyListeners()
    {
        for (Iterator it = listeners.iterator(); it.hasNext(); )
            ((Listener)it.next()).spaceStepped();
    }
    public interface Listener
    {
        void spaceStepped();
    }
}

The stop(); method for Thead is deprecated because it has problems.
Here's what the API doc has to say:
. This method is inherently unsafe. Stopping a thread with Thread.stop causes it to unlock all of the monitors that it has locked (as a natural consequence of the unchecked ThreadDeath exception propagating up the stack). If any of the objects previously protected by these monitors were in an inconsistent state, the damaged objects become visible to other threads, potentially resulting in arbitrary behavior. Many uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. If the target thread waits for long periods (on a condition variable, for example), the interrupt method should be used to interrupt the wait.

As for the centre of your space - the centre of anything rectangular is defined as being at exactly half its width and half its height

Ok so how do i define the spot to put the spaceship as half height/width, i tried to input width/2 but it didnt work..

If what you're trying to do is width/2, You need to input the width, then have the program divide it by two for you. Sorry if that doesn't answer your question, I'm not quite sure I understand.

I assume that the numbers in the new PlayerShip call are its initial position? In which case you just have to replace those with the calculated position of the center.

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.