Homework P4

General

Last homework we learned about the basics of javadoc and commenting.  Now we will learn a couple of additional tags for our class files.

If you have any questions, see the javadoc FAQ for 1322 which the TA's have prepared.

In the class header we will add two additional tags after you write the description of the class.

The first is @author  MyName and gtnum.  This identifies that person who wrote the class.  Probably seems unimportant to you now as a comment, but in a real software project, it is important to know who wrote and maintains a specific class in the application.

The second is @version versionID This identifies which version of a class people are looking at.  Again, seems unimportant, but in the real world, with lots of people changing the code, it can become confusing which version of a class someone is using.  Each project will have its own numbering systems for versions.  In this class, you can just make your initial version of a class 1.0.  As you make subsequent changes to the class, the version number may change.  Smaller changes change the decimal place (version 1.1, 1.3) and larger changes change the integer part (version 2.0, 5.0).  If you have multiple versions of a class for 1322, use whatever numbering system seems useful to you.  If you have only one version, then just put 1.0 as your version.

For example:

/**

 * This is a description of the entire class

 * that may be some length.

 *

 * @author Bob Waters

 * @version 1.0

 */

 

By convention the first line of a javadoc comment is always a summary sentence.  Other sentences can then be included to detail the desired description.

 

We also need to comment our instance variables.  Instance variables are commented like:

/** the average computed temperature of the mixture */

int temperature;

 

Note that the comment begins with our /** javadoc comment marker and then contains the description of the instance variable.  The comment ends with the standard */.  There is NO blank line between the comment and the instance variable declaration.

 

Required comments for P4 are:

  Javadoc style comment the class header and each method

  Javadoc style comments for the instance variables

  Regular comments to denote end braces of method and class defs, and blocks.

 

For instance:

while (true) {

 

} //end while

 

if (true) {

 

} else {

 

}//end if-else

 

These will help you keep your braces matched up.

Program 1

COUNTER

Implement a program to take in a user supplied string.  Count the number of words in the string.  For each word, count the number of vowels (a,e,i,o,u) in the word.  Your output should consist of the word count, a newline, then for each word, the word and its vowel count.  You should then ask the user if they want to continue.  If they answer 'y' you should prompt for another string, if 'n', your program should exit with a goodbye message.  Assume there are no punctuation or non-letter characters in the string.  For instance:

Please input your string:
Howdy Doody

The string "Howdy Doody" has 2 words.

Howdy has 1 vowel

Doody has 2 vowels

Do you want to me to handle another string?

n

GoodBye!!

Assume also that a word is defined as a sequence of characters separated by whitespace. (i.e. words are separated by a space, tab, etc.).  HINT: Look at section 3.6, page 169 of your textbook.  It discusses the StringTokenizer class which you will find very useful in this problem.  Also the character count program might be interesting in Listing 3.11.

Program 2

RAINBOW

Implement a program to draw a rainbow on the screen.  Use tightly spaced concentric arcs to draw each part of the rainbow.  Each arc should be a random color.  When you are done, color the middle arc yellow (Color.yellow).  Your class should have a constant:

/** this value sets the number of arcs to draw */

public static final int ARC_COUNT = 9;

 

The TA's may change this value so do not hard-code your drawing or your calculation of the center arc with respect to the arc count.  If there is an even number of arcs (like 8) your application may choose which of the two "center" arcs to recolor.  Section 3.10 of the book reviews using iteration with drawing.

Program 3

PIG GAME
 

This is a game that uses two 6 sided dice (i.e. generates the numbers 1-6)  to roll values.  Your first job is to create a Dice class.  The dice class should be able to roll itself and store the number of the roll.  The constructor to the dice class should take in the number of sides on the dice.  You should also be able to ask the dice what the roll was.  For example:

Dice d = new Dice(6);  //create six sided dice

d.getValue()  //get the current value

d.getSides() //get the number of sides on the dice

d.roll() //roll the individual dice

The next task is to create the DiceRoller class.  This class manages two dice and controls the throws.  The constructor should take two dice objects.  You should be able to ask for either individual dice value, the total of both, or whether doubles were thrown.  For example:

Dice d1 =new Dice(6);

Dice d2 = new Dice(6);

DiceRoller dr = new DiceRoller(d1,d2);

dr.throwDice(); //throws the two dice

int value = dr.getTotal(); //gets the sum of the two dice

if (dr.isDoubles()) //tests to see if d1 and d2 were same

int vd1 = dr.getD1(); // gets value of first dice

int vd2 = dr.getD2(); // gets value of second dice

Now we create the game itself, the PigGame class.  In the game of pig, a user competes against the computer.  On each turn the current player throws a pair of dice and accumulates points.  The goal is to reach 100 points first.  If, on any turn, either of the dice rolled by the player is a 1, all the points accumulated on that round are lost and control passes to the other player.  If the player rolls double 1's, all their points gained in the entire game are lost and the dice passes to the other player.  A player may voluntarily give up the dice after any roll.  A player needs to decide if they will roll again (be a pig -- but risk losing points) or give up the dice (and risk the other player winning).  For the computer player, implement the game such that the computer always gives up the dice if they have gotten 20 points or more in a round.  A sample output might look like:

Welcome to Pig. What is your name?

Bob

Hi Bob, you roll first.

Current score Bob=0 Computer=0

You roll a 6 and 3.  You have 9 points this round.

Do you want to roll again?

y

You roll a 5 and 2.  You have 16 points this round.

Do you want to roll again?

n

Current score Bob=16 Computer=0

I roll a 4 and 4.  I have 8 points this round.

I roll a 5 and 3.  I have 16 points this round.

I roll a 1 and 4.  I get 0 points this round.

Current score Bob=16 Computer=0

You roll a 1 and 1.  You lose all your points.

Current score Bob=0 Computer=0

...

Current score Bob = 98 Computer = 94

I roll a 6 and 6.  I have 12 points this round.

I Win!!!!!

Play again?

n

Goodbye, thanks for playing.

Hint: Look in your book in Listing 4.6 and 4.7 for an example dice class.

Turn-in

Rainbow.java

Rainbow.html

Counter.java

PigGame.java

DiceRoller.java

Dice.java