CSE1030Z Lab 02

Wed Jan 16 2:30-4:00 PM

Introduction

The purpose of this lab is to implement utility classes, which includes adding documentation in the form of Javadoc comments and generating APIs using the Javadoc tool. This lab will be graded for style and correctness.

There are three questions to this lab. You need to submit only the first two questions by the end of the lab session.

Question 1: Adding to the DistanceUtility Class

We will start by adding features to an existing fully documented utility class. This exercise allows you to practice implementing features of a utility class without having to implement the class from scratch.

Here is a version of the DistanceUtility class described in the lectures.

Create a new project and copy the code above into Eclipse or your editor. Add the following features to the utility:

  1. A public attribute named MILES_PER_KILOMETRES whose value is the number of miles in a kilometre.
  2. A public method named milesToKilometres that converts a distance in miles to kilometres.
  3. A public method named milesToKilometresthat converts distances in miles to kilometres for arrays.
  4. A public method named milesToKilometresthat converts distances in miles to kilometres for collections.

These features should be similar to their corresponding kilometres-to-miles features that were provided to you. Make sure the features are documented correctly using Javadoc comments. You can view the entire API here.

Generate the Javadoc for the utility class:

If you make changes to the Javadoc comments in your Java file you will need to re-generate the Javadoc. When you are sure that the generated documentation matches the API and that your methods are implemented correctly, submit your class using the following command:

submit 1030Z L2W DistanceUtility.java

Question 2: Create a Utility Class from Scratch

Now that you have worked with an existing utility class, it is time to create your own utility class from scratch. The goal is to create a small utility class that can convert temperatures between degrees Celcius and degrees Fahrenheit.

The API for the required utility class is shown here.

Your class needs to provide and API identical to the one located at the above link; i.e., it must provide:

  1. a package named cse1030 that contains a utility class named Temperature
  2. attributes named DEG_F_PER_DEG_C and DEG_C_PER_DEG_F having the correct values
  3. methods named fahrenheitToCelcius and celciusToFahrenheit that perform the work described in their contracts

The formula to convert from degrees Fahrenheit to degrees Celcius is:

degc = (degf - 32) * DEG_C_PER_DEG_F

The formula to convert from degrees Celcius to degrees is Fahrenheit:

degf = degc * DEG_F_PER_DEG_C + 32

As in Question 1, generate the documentation for your class. When you are sure that the generated documentation matches the API and that your methods are implemented correctly, submit your class using the following command:

submit 1030Z L2W Temperature.java

Question 3: A Utility Class for Game Rules

You do not need to submit this question; instead, I provide you with the tools to check if your solution is correct. This is an important question (even though it is not graded) as you get to see and use one of the tools professional programmers use to test their code.

Utility classes are used to group a set of methods that perform related tasks. For example, java.lang.Math groups together mathematical methods, java.util.Arrays groups together methods that operate on arrays, and java.util.Collections groups together methods that operate on collections.

A possible use for a utility class is to group together methods that define the rules for a game, such as the Yahtzee game that was introduced in Lab 1.

As a brief reminder, Yahtzee is game played with dice where the goal is to try to roll certain combinations of values using five six-sided dice. In Yahtzee, there are six special combinations of dice values:

Name Description Examples
three-of-a-kind at least three dice having the same value 6-2-3-2-2 (has three 2s)
four-of-a-kind at least four dice having the same value 5-5-5-1-5 (has four 5s)
five-of-a-kind or Yahtzee all five dice having the same value 4-4-4-4-4
full house three-of-a-kind and a pair 2-2-3-3-3, 4-4-4-5-5
small straight at least four sequential dice
(roll must contain 1-2-3-4, 2-3-4-5, or 3-4-5-6)
3-1-3-4-2 (has 1-2-3-4)
3-5-3-4-2 (has 2-3-4-5)
6-5-3-4-6 (has 3-4-5-6)
large straight five sequential dice
(roll must contain 1-2-3-4-5 or 2-3-4-5-6)
5-1-3-4-2 (has 1-2-3-4-5)
6-5-3-4-2 (has 2-3-4-5-6)

Imagine that you are trying to implement the game of Yahtzee in Java. One of the problems that you must solve is to determine if a roll of the dice is one (or more) of the special combinations of dice values.

Create a new project and implement the utility class Yahtzee that has the API shown here. Here is some code to get you started; you should bring both files into the project (Eclipse) or working directory (plain editor):

You can run the jUnit test as soon as you have saved both files in your project:

You can implement the methods one at a time and run jUnit everytime you think you have completed a method. If your method passes the test, then you can be reasonably confident that your implementation is correct (although it may not be ideal). If your method does not pass the test, then you can go back and inspect and debug your method.

Notice that having tests written before you even begin implementing your class is a very powerful approach to code development. This approach to software development is called test-driven development, and is a hallmark of many modern approaches to software development.