CSci 1211 Problem set 9. Due Friday, April 9th at 5pm
Individual work: Problem 1 (12 points)
In this problem you are asked to write an applet that will draw this picture. More specifically:
- The squares alternate orientation and color.
- The number of squares depends on the size of the applet: the
largest orange square (oriented diagonally) has its vertices at the
middles of the sides of the largest square that can be fit into the
applet; the smallest square has the side no less than 10 pixels. To
see how it all works, download the HTML file and the PS9.class file
and try to resize the applet.
- The width of the stroke is 2 pixels. Find the appropriate
constructor of the BasicStroke class to set the width.
- This is definitely a loop problem. The loop should stop when the
side of the square becomes less than 10.
It might be helpful to compute the vertices of the new
square from those of the current square.
It might also be helpful to
write a method that computes the distance between two points. You
can use this method to figure out when to stop the loop.
- Alternating colors might be somewhat tricky, leave it out until
you get the shape right.
Project Assignment, Part 1 (30 points each person)
For this project you need to implement the Battleship game. Here
is the description of the game. Here are the details of the rules:
- We will use the board 10-by-10, with rows indexed by letter from a
to j going down, and columns indexed by numbers from 1 to 10 left to
- The ships may be positioned horizontally or vertically, but not
diagonally. A ship may be positioned in a straight line only.
- Ships cannot intersect (i.e. occupy the same square).
- We are not going to fix the number and sizes of the ships,
the same program will be able to handle different ship combinations.
Programming details for this part of the program
Your task for this part would be to implement two classes: Board and
Ship. You are given the compiled versions of these
classes (.class files only) and the testing file Game.java.
The class Board will implement one game board (to play a game, you'll
need two instances of this class: one for you and one for your
opponent). The class will have the following public methods:
The printing methods are for testing purposes.
Board() a default constructor
boolean addShip(String name, int l, char c, int n, boolean
horizon) a method to add a ship to the board. Here
The method returns true if the ship was added successfully, and false
if one of the following occurred:
name is the name of the ship (such as Carrier or
l is the length of the ship
c,n are the coordinates (such as a5) of the leftmost
square for a horizontally positioned ship, and of the uppermost square
for a vertically positioned ship
horizon is a boolean value which is true if the ship
is positioned horizontally, and false if it's positioned vertically.
If the method returns false then the ship has not been added to the
- at least one of the coordinates was outside of the grid or had any
other invalid value.
- there is another ship in at least one of the squares to be
occupied by the newly added ship.
int shot(char c, int n) a method that simulates a
shot directed at the board. There are 4 possible return values:
- 0 if the shot is a miss. The board gets updated to record the shot.
- 1 if a ship is hit, but not sunk. The board gets updated to record
the shot. If the Ship class contains information about the hits to the
ship, then the ship gets updated as well.
- 2 if a ship is sunk. The board gets updated to record the
shot. The ship state gets updated if necessary. The message gets
printed with the name of the ship that was sunk, such as "You
have sunk my Carrier"
- -1 if the coordinates are invalid. An error message gets
printed. No shot is recorded.
void printBoard() prints the contents of the board. 0
is printed for squares that have not been hit, 1 for squares that have
been hit, but no ship is positioned there, and 2 if there is a hit to
void printShips() prints names, lengths, and
positions of all the ships located on the board.
The Board class should have two 10-by-10 arrays:
While the 2-array implementation is not mandatory, it is highly
recommended. If you are considering a different implementation, please
discuss it with me ahead of time.
- an array of booleans
to record the opponents shots (false = no shot, true otherwise),
regardless of whether there is a ship there.
- an array of references to objects of class Ship. Each square where
a ship is positioned is a reference to the ship. Squares where no ship
is positioned have a null value.
Ship contains all the information about the
ship (the name, the length, etc.). It may or may not contain the
information about whether the ship has been hit or sunk. Since the
class Ship is not going to be used directly, so the hit/sunk
information can be obtained through the board on which the ship is
A suggestion: the printing done directly from the classes Board
and Ship is for debugging purposes only. You might want to add a
variable that indicates whether you are running your program in a test
mode. For instance, if this variable is
boolean testmode = true;
and all the print statements are written under an 'if' statement
then switching this variable to false will turn off all the
printing. You may further fine-tune this approach by having different
kinds of test printing and different kinds of mode variables.
Finally, a few suggestions:
- If you are working in a group, don't split up your work. Work
together, at the same terminal: one person typing, the other person
checking and making suggestions.
- Before you write the first line of code, think carefully of what
information you have in which class and which methods you need. Draw
pictures, write method descriptions.
- If you have any questions, please ask right away. The class list
is the best place to ask. If you see someone's message with a question
and you know the answer, please answer. Even if you think that your
answer may be incomplete, still give it a try. However, don't send
your entire code files to the class list, choose the smallest
possible fragment to explain the problem.
- Write code in small portions, method by method, and test it right
This page is a part of the course CSci
1211 at UMM.
The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.