Back to Homework

Back to Main Page

Homework 3

General 

Up to now, we have been lax about code comments.  Comments are very important for software maintenance and understanding of your code.  There is an old adage in development that says, “Comment your code so that someone who has no idea what the code does can understand it, because in six months that someone will be you!”

 

We start comment enforcement gradually and build up the requirements as we go along in the course.  For P3 we will begin to “javadoc” our code.  Javadoc is an application that is part of the JDK which you downloaded.  You can run it on the command line by typing “javadoc *.java” which will create documentation for all the classes in your current directory.  If you are using JGrasp, you can invoke javadoc by using Project->Generate Documentation from the pull-down menu.   Javadoc creates the nice web-page based documentation that we have been using in class (the API).

 

Remember from class we said that javadoc comments were a special form of comment.  Javadoc recognizes a comment that starts with /**   as a javadoc comment.  For each new program assignment we will introduce new javadoc features.  For P3 we will create the following javadoc comments:

 

The class header:

Start your class header with the javadoc  comment /**

Then write a short description of the class.  Note that each line starts with an asterisk (*) and space.

End the header comment with a */

For example:

 

/**

 * This is P3, problem 1.

 * This class does some real work.

 * Written By:  Bob Waters gtg000k

 */

 

The method header:

Start each method with a comment that tells what that method does, just like we did for the class.  Javadoc also recognizes special commands called tags that denote important information for the web page being generated.  We are going to use two of those tags in our P3 method comment.

@param paramName sdescription

@return returnValue description

 

For example:

/**

 * This method computes the area of a triangle using heron's rule.

 * @param a an integer representing side a of a triangle

 * @param b an integer representing side b of a triangle

 * @param c an integer representing side c of a triangle

 * @return a double representing the computed area of the triangle

 */  

 public double heron(int a, int b, int c) {

 

We will learn more specialized javadoc tags in future program assignments.

 

You should also place good comments into your code when the algorithm you are using is not obvious.

For the statement

int perimeter = a + b + c;

 

a good comment might be:

// calculate the perimeter by summing the lengths of the sides

 

a bad comment would be:

//add a b and c together

 

A frequent syntax problem developers have is mismatched braces.  One commenting technique that can really help you is to comment the close brace with a short description of what it is enclosing.

 

For example:

}// end class def Demo

}// end main method

}// end switch(x)

 

Part 1. GUESS MY NUMBER GAME

Write a java application to play a simple mind reading game.  When the program first starts, it should ask the user for the upper limit of a range of numbers they would like to guess.  For instance if the user enters: 20, then they will be guessing a number from 1-20.  The program should then generate a random integer between 1 and the number entered by the user.  The program should then prompt the user that it is thinking of a number.  The user should then be able to enter their guess as an integer.  The system should then print out a message indicating whether the user's guess is correct or not.

 

Part 2.  PERSONAL BUSINESS CARD

Write a java applet that displays a business card of your own design.  The card should include both graphics and text.  At a minimum, your text should include your name.  The graphics should include at least 3 or 4 different shapes from the Graphics class.  You should also use a least 3 different colors on your card.  You may make the business card any dimensions you wish.  Include an html file which displays your applet in a web page.  We will put some of the "coolest" cards off our class web page (with the student's permission of course).

Part3.  COIN CALCULATOR

Write a java application that determines the value of coins in a jar and prints the value in dollars and cents.  Read in integer values that represent the number of quarters, dimes, nickels, and pennies.  Use one of the formatters we talked about to format and print the results.

 

IMPORTANT:

For parts 1 and 3, you need to create an instance of your class in your main method and then call your object's methods to do the actual work.  Do not put everything in the main method and do not use only static methods.  If you are unsure how to do this, look at the stubbed out code we gave you for P2.

 

TURN-IN:

4 files:

  NumberGuesser.java

  MyCard.java

  MyCard.html

  CoinCalc.java

Copyright © College of Computing
Any unauthorized reproduction or use is strictly prohibited.