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();
}
}