SECTION A

Task 1 Rescue.java

Space ships sometimes become disabled. When this happens there is only a limited amount of time to locate the disabled space ship and rescue the people on board. This is a job for the elite space rescue crews and their rescue space ships.

You are the captain of one of these rescue space ships.

Write a program named Rescue.java that helps you and the crew fly your space ship to locate and rescue disabled space ships.

The program starts by prompting (asking) the user for the size of the grid. The grid has a minimum size of 3, but no maximum. If the user enters a number less than 3, the program must ask the user to enter a number, again and again, until the user enters a valid number.

The size of the grid is an integer.

This grid is the basic search grid that the program will use as a reference. The x axis is the horizontal axis and the y axis is the vertical axis. Co-ordinates are always listed with the x axis number first followed by the y axis number. The coordinate 0,0 is the point at the bottom left of the grid and the coordinate (grid size, grid size) is at the top right hand corner of the grid.

The grid size is included, so, for example, if the user entered a grid size of 6, then the x coordinates would be from 0 - 6 as would the y co-ordinates. Co-ordinates are integers.

Once the grid size is established then the disabled space ship is placed somewhere in this grid, as described below.

The program generates a random number for the x co-ordinate and another random number for the y co-ordinate, for the disabled space ship. These random numbers must, of course, be within (and including the edges) of the grid.

The disabled space ship, once placed, is always at these co-ordinates for the whole program, it does not move.

The program then generates yet another random number for the starting x co-ordinate of the rescue space ship and another random number for the starting y co-ordinate of the rescue space ship.

As with the disabled space ship x and y co-ordinates, the x and y co-ordinates of the rescue space ship are integers and must start within the grid, including the edges.

Due to the uncertainty of emerging from hyperspace, it is possible that the rescue space ship will emerge at the same co-ordinates as the disabled space ship.

If this happens then both space ships are destroyed, the program displays an appropriate message and that is the end of the program.

Assuming that the initial co-ordinates of the disabled space ship and the rescue space ship are not the same, then the program prompts (asks) the user for the number of attempts.

This is the number of turns through the program, either the rescue space ship has rescued the disabled space ship within this number of turns or else the rescue space ship is forced to return to base.

If the rescue space ship is forced to return to base, then the mission has failed, an appropriate message should be displayed to the screen and the program ends.

Next the program repeatedly presents the user with a menu of actions that can be carried out by the rescue space ship. The user chooses an action each time the menu is shown. Choosing an action updates the x and y co-ordinates of the rescue space ship. This is explained below.

The program loops through the menu until:

  • The x and y co-ordinates of the rescue space ship and the disabled space ship are the same, in which case the rescue was successful. An appropriate message is displayed to the screen and the program ends.
  • The number of attempts is reached, in which case the rescue is unsuccessful, as described above
  • The user selects the action "return". Selecting this action causes the rescue space ship to immediately return to its base. The mission is unsuccessful. An appropriate message is displayed to the screen and the program ends.
Only certain actions are allowed, these are entered as text (not numbers)

The allowed actions and the change they make to the x and y coordinates of the rescue space ship are:

port this goes left by 2 (x - 2), but leaves y unchanged starboard this goes right by 2 (x + 2), but leaves y unchanged. full up this goes up by 2 (y + 2), but leaves x unchanged full down this goes down by 2 (y - 2), but leaves x unchanged
port up this goes up by 1 (y + 1) and left by 1 (x - 1)
port down this goes down by 1 (y - 1) and left by 1 (x - 1)
starboard up this goes up by 1 (y + 1) and right by 1 (x + 1)
starboard down this goes down by 1 (y - 1) and right by 1 (x + 1) return this immediately abandons the mission

For example, if the port action is entered, the nett effect would be

new value of x = old value of x - 2
new value of y = old value of y (unchanged)

There must be a space between any actions that have more than one word and all actions must be case insensitive. This means that Port or PoRt or port should all produce the same result.

Your program must correctly deal with the user entering an invalid action (not one of the commands listed above). If the user enters an invalid action, an appropriate message is displayed to the screen, but the rescue space ship does not have its x and y coordinates updated, they stay at the same positions and nothing about the ships positions is displayed.

To rescue the disabled space ship, the rescue space ship must be at the same coordinates as the disabled space ship in the same turn, except, of course, at the very beginning of the program.

coordinates are always integers, including the initial coordinates.

There is one final problem. The rescue space ship can only locate the disabled space ship out to the grid size range in all directions, using the rescue space ship as the centre.

This means it is possible that the rescue space ship could lose contact with the disabled space ship. If this happens then the program no longer displays the co-ordinates of the disabled space ship, only the co-ordinates of the rescue space ship. This continues until the rescue space ship re-acquires the disabled space ship. Or, of course, runs out of attempts or chooses the "return" action.

The rescue space ship can go outside the boundaries of the grid. Other than potentially losing contact with the disabled space ship, the rescue space ship can go in any allowed direction as far as it wants to, within the conditions above.

Each time the program is going to continue, the position of the disabled space ship and rescue space ship are displayed to the screen.

Some sample runs of the program are included below (user input is in bold): (Note: not all options, functionality and messages are shown.)

> java Rescue
Enter grid size >> 7
Initial position of the disabled Space ship is x = 1 and y = 1
Initial position of the rescue Space ship is x = 6 and y = 1
Enter number of attempts >> 8

Space ship menu
port
starboard
full up
full down
port up
port down
starboard up
starboard down
return Enter

ship choice >> starboard
Position of the disabled Space ship is x = 1 and y = 1
Position of the rescue Space ship is x = 8 and y = 1

Space ship menu
port
starboard
full up
full down
port up
port down
starboard up
starboard down
return
Enter ship choice >> starboard

Disabled Space ship is out of detector range.
You need to urgently re-acquire the disabled Space ship,
they are depending on you!
Position of the rescue Space ship is x = 10 and y = 1

Space ship menu
port
starboard
full up
full down
port up
port down
starboard up
starboard down
return
Enter ship choice >> port down

Disabled Space ship is out of detector range.
You need to urgently re-acquire the disabled Space ship,
they are depending on you!
Position of the rescue Space ship is x = 9 and y = 0

Space ship menu
port
starboard
full up
full down
port up
port down
starboard up
starboard down
return
Enter ship choice >> port down

Position of the disabled Space ship is x = 1 and y = 1
Position of the rescue Space ship is x = 8 and y = -1


Space ship menu
port
starboard
full up
full down
port up
port down
starboard up
starboard down
return
Enter ship choice >> port

Position of the disabled Space ship is x = 1 and y = 1
Position of the rescue Space ship is x = 6 and y = -1

Space ship menu
port
starboard
full up
full down
port up
port down
starboard up
starboard down
return
Enter ship choice >> port

position of the disabled Space ship is x = 1 and y = 1
Position of the rescue Space ship is x = 6 and y = -1

Space ship menu
port
starboard
full up
full down
port up
port down
starboard up
starboard down
return
Enter ship choice >> port

position of the disabled Space ship is x = 1 and y = 1
Position of the rescue Space ship is x = 6 and y = -1

Space ship menu
port
starboard
full up
full down
port up
port down
starboard up
starboard down
return
Enter ship choice >> port

position of the disabled Space ship is x = 1 and y = 1
Position of the rescue Space ship is x = 6 and y = -1
You are out of fuel!!
You must return to base immediately
Do a better job next time, people are depending on you!

Another run of the program

> java Rescue
Enter grid size >> 7
Initial position of the disabled Space ship is x = 4 and y = 7
Initial position of the rescue Space ship is x = 4 and y = 2
Enter number of attempts >> 5

Space ship menu
port
starboard
full up
full down
port up
port down
starboard up
starboard down
return
Enter ship choice >> return

It must have been a grave emergency that caused you to abandon those people

Another run of the program

> java Rescue
Enter grid size >> 9
Initial position of the disabled Space ship is x = 2 and y = 7
Initial position of the rescue Space ship is x = 9 and y = 6
Enter number of attempts >> 6

Space ship menu
port
starboard
full up
full down
port up
port down
starboard up
starboard down
return
Enter ship choice >> port

Position of the disabled Space ship is x = 2 and y = 7
Position of the rescue Space ship is x = 7 and y = 6

Space ship menu
port
starboard
full up
full down
port up
port down
starboard up
starboard down
return
Enter ship choice >> port

Position of the disabled Space ship is x = 2 and y = 7
Position of the rescue Space ship is x = 5 and y = 6

Space ship menu
port
starboard
full up
full down
port up
port down
starboard up
starboard down
return
Enter ship choice >> port

Position of the disabled Space ship is x = 2 and y = 7
Position of the rescue Space ship is x = 3 and y = 6

Space ship menu
port
starboard
full up
full down
port up
port down
starboard up
starboard down
return
Enter ship choice >> port up

Position of the disabled Space ship is x = 2 and y = 7
Position of the rescue Space ship is x = 2 and y = 7
Congratulations!! You and your crew have rescued everybody!
Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.