You are on page 1of 11

C HAPTER 1

Assignment 3:
Initializing a battle
ship game board
(of sorts)

Creating the starting board for a game of


battleship with only one player and one
ship.

S ECTION 1 This assignment is a review of basic computer science con-


cepts such as 2D arrays. We are still working with procedural
Required programming programming, writing the code within the main method or in

style conventions for all as- other public static methods.

In all programming assignments you will be required to follow


signments conventions. This section explains four of these conventions

In source code file documentation: Unless otherwise


indicated, you are only required to submit the source code file
C ONVENTIONS (files ending in .java). But every source code file must include
the following:
1. In source code file documentation
1. The program name and purpose of the class in the solution
2. Introduction screen (when working with non-
of the problem. You may add that the program is the solu-
windows program) tion to a specific programming assignment is allowed, do
3. User prompt (when working with non- not forget the more important aspects of the purpose of the
class
windows program
2. Name(s) of those that contributed to this file. You will only
4. Writing two methods using arrays get credit for working on a team if the submitted code has
your name in the source code file.
5. Termination screen (when working with non-
3. The date the program code was last changed (revised).
windows program)
Introduction screen:

The first display from a program is an “introduction screen”.


A complete introduction screen has the following three items:

1. Explains the purpose of the program. This must be specific


to the problem it is solving, not that it is a the solution for a

specific programing assignment. This is to tell the user that Termination screen:
is running the program what the program will do.
The last display of a program should be a message that indi-
2. Explains what the user is expected to do. If the user does
cates that the program in ending.
not need to enter anything, say this.
3. Explains what will happen after the user does as instructed. Examples:

• This program in now ending!


User prompts:
• Program terminating!
Each user prompt must have the following parts:
• Process complete and program ending!
1. A clear explanation of what must be entered which should
include the significance of the number or text. For example
if an interest rate to a loan is needed, the user prompt
This is to convey the idea that nothing else will happen and if
should say “ Please enter the loan annual interest rate”.
needed, another program can be run.
2. An example of an acceptable value demonstrating the cor-
rect format. For the same example as used above, the
prompt could continue “For 4.25%. please enter 4.25”
3. An indication of the range of a acceptable value. This can
be done by a list of symbols, indicate that it is positive or
give a start and end value, indicating if the range is inclu-
sive or exclusive. To continue the same example as above
you could write, “This rate must be in the range of 2.5% to
11.5%”. This, of course, would be completely dependent on
the requirements.

S ECTION 2 JOptionPane.showOptionDialog

Review of OptionsDialog In lecture you were shown the following statics methods of the
class JOptionPane.

• showMessageDialog

• showInputDialog

In this lab we will introduce you to another dialog:

A DDITIONAL STATIC METHOD • showOptionsDialog

1. showOptionsDialog which give buttons for your user to click on to make their se-
lection. When using this method you pass it an array of op-
2. Using Small images tions as strings and these show up as labeled buttons. The se-
lection the user clicks on is returned as an integer represent-
ing the index of the label in the array of options. The use of
buttons for selection eliminates the need for data validation as
would be needed is you asked them to enter their selection
into a text field.

The arguments for this method are as follows:

1. parent component - use null as there is not parent GUI


component
2. Text message - The message that goes inside the box
3. Title of box - The text that appears in the dialog box title
bar.
4. Message type - Announcement, caution or error ( use 0)
5. option type - use 0
6. icon image

7. option array - use an array of strings


8. default selection - enter an array element.

The sample provided in the code above, creates the dialog box Notice that the buttons are all the same size and that size is
below. determined by the longest label. If shorter names are used,
say numbers instead of longer names, a much more narrow
dialog box is produced, as shown below.

More items can be added to the array you present as options.


More options will produce more buttons to click on. Howev-
er, again due to the limited editing capability of the JOption
pane, the layout of the buttons is always in one row. They can As can be seen, you have only limited control over the appear-
not be made to wrap around and make two rows of buttons. ance of the JOptionPanes.
Adding more options will simply make the pop-up wider. For
example, if we add two additional flowers, say Dianthus and Please Note: The images shown in this manual were taken
Daisy, as shown in the code snippet below, the popup get form a program run on a MAC laptop. The Mac “look and
much wider. feel” it opposite of the windows “look and feel”. On a
windows machine, the same array of options would produce a
line of buttons that are ordered in the opposite direction such

that Daisy is on the rightt and Rose would be on the left. Also,
the control buttons for the window are on the opposite side of
the box title bar on a windows machine.

And believe it or not, LookAndFeel is a formal class within


the java libraries.

6

S ECTION 3 Battle ship Game: brief description:

Problem Description Historically, battle ship was played on a game case that con-
sisted of a pegboard so that you could track where your ships
were and the shots you were making into your opponents
fleet.

Peg boards lend themselves quite nicely to representation by


arrays. So to give you a chance to review all of the program-
ming basics such as arrays, if-else statements, for loops and
O BJECTIVE writing custom methods (function), etc. . . , you will imple-
ment a program that will initialize a battle ship game board
1. Review if-else statements with a single 5 unit long ship, positioned randomly but legally
2. Work with a program loop within the confines of the game board. For example:

3. Work with 2D arrays


4. Practice Java’s Random number generator
5. Practice using the showOptionDialog static Or
method from the JOptionPane class

You should be able to see that the board is displayed using a 7. You must have a method called displayBoard that takes
JOptionPane.show Options Dialog popup. The syntax for this a populated array as argument and creates a popup window
static method is given in Section 2 of this document. If you displaying the board. When a button on the popup is
don’t want to bother with a custom icon, simply enter null as clicked, the methods and returns the option selected by the
an argument where you would have passed the icon user determined by the button clicked.

The list of program requirements is given below:


8. All methods must be static (we will get to non-static next
1. There must be a program loop to generate a new board with module), and there should be not static variables.
random ship location until the user wants to quit. I will run
the board multiple times. I should see the ship located in 9. You must adhere to the programming convention as speci-
all quadrants of the board in both directions. fied in the evaluation criteria table shown on the submis-
sion site.
2. When initializing a board or assigning positions to the ship,
a for loops MUST be used. This assignment is rife with Extra credit potential!

• Do more than one ship of varying length (careful, no over-


3. The “empty” character can be the dash, not an underscore.
laps allowed).
• Allow the user to select ship length size or board size.
4. The appearance of the board MUST BE square if
the # rows and # columns are the same.
You can earn a maximum of 20 points extra credit.

5. All empty space characters MUST BE perfectly


aligned, especially in the columns to the right of
the ship placement.

6. You must have a method called generateBoard that takes


the number or rows and column as argument that returns a
2D array which is randomly populated with a single ship.
This method must use a loop structure to determine
the positions of the ship.
8

So, the place to start is the pseudo code. And to do that you need
to do problem decomposition.

Start asking yourself questions and if you can’t answer them, ask
me or a class mate. I have seen some truly over complicated solu-
tions for this assignment.

•What are your givens?


•What does the user need to input?
•What needs to be processed?
•What needs to be output?

Let me recommend a very high level program flow, which is not


detailed enough to be pseudo code. Fit your methods around this
flow. OR if you choose you own, go for it.

1. Create a 2D array and initialize it with the empty space symbol


using a for loop.
2. Determine location and orientation of the ship using a random
number generating object.
3. Add the ship symbol to the appropriate location of the array.
4. Generate a string accumulator to print the board
5. Display accumulator to a JOptionPane

Remember, this should be repeated using the back door method,


until the user wants to quit.

Start early and work steadily. If you have any questions ASK!!!!!!

S ECTION 4 Remember , if your program does not compile, you will re-
ceive no credit for it. So, write your code in stages that you
Evaluation Criteria can alway return to if new code prevents you program from
compiling.

Extra credit:
E VALUATION C RITERIA I have suggested extra credit. If you think of something else
that will add value to the solution or give it an extra feature to
1. Submission requirements make it more versatile, you should discuss it with me to de-
termine if it will earn you any extra credit. You may earn as
2. Extra credit
much as 20/100 in extra credit per problem.
3. Source code Documentation
However, be aware, if your solution to the required problem
4. Programming style
is not correct, you will earn no extra credit points. This in-
cludes the alignment of the column characters and the ap-
pearance of being square. In other words, extra credit can
not be used to make up for not getting the required
assignment correct. If your characters are not lining up
Submission requirements:
correctly or your game board that has the same number of
Source code is expected to be submitted to the Moodle
rows and column does not appear square, any extra effort will
submission link for Assign 3, before the posted dead-
not count toward points. Extra credit is only for you to get
line. Assignments that are late will have a 20 point
credit for trying new ideas after you have correctly completed
penalty on the base problem.
the required problem.

No code will be accepted through e-mail.


Programming style:
We have been covering programming style these last two se-
mesters. The following is a summary of the conventions that
you will be required to follow.

10

• Any statements within a set of braces, must be indented one Here are a few exam-
tab to the right of the opening brace. ples of misalign-
• Variables should be given intuitive names indicating what is ment and mis-shape
stored in them. When helping you, I should not have to ask that will prevent you
you what is stored in a variable. from earning extra
• All variable names must start with a lowerCase letter. Suc- credit. In the image to
cessive words in a variable name, can either be separated the right and below, the
with an underscore, or start with an upper case. X’s do not align with
• Program name should start with an uppercase letter. the empty spot charac-
• Blank lines should be used to separate blocks of code. Do ters in either of these ex-
not put a blank line after each line of code! You will loose amples. Also, the one im-
points if you do this. age above is not square.

Remember, a convention is simply a set of rules established This misalignment is


for the sake of consistent program layout. As mentioned in most noticeable in the
lecture, this increases the readability of the program and al- horizontal position, as
lows the logic in the program to be more quickly understood. shown in the image be-
low. This is the type of
So, to be candid, it is possible to write code that will compile misalignment that will
and run per specifications without adhering to any of these
make your program in-
conventions. However, good programming habits dictates
eligible for extra credit.
adhering to good programming style and as such, you will be
So get the base problem
evaluated on your own programming style.
correct first before ex-
There is a criteria table at the submission site on Moodle. panding you solution to
Check your work against this detailed table BEFORE submit- include extra credit.
ting it. And remember, extra credit is assigned only if you
have all correct on the structure, content and display of the
game board.

11

You might also like