CSE1030Z Lab 02

Thu Jan 17 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 L2R 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 US style automobile fuel economy numbers to metric fuel consumption numbers.

In the United States, automobile fuel efficiency is measured using fuel economy, or miles per US gallon (the distance one can drive using one US gallon of gasoline). In Canada, automobile fuel efficiency is measured in fuel consumption, or litres per one hundred kilometres (the amount of gasoline needed to travel 100 km). This is very annoying when doing research to purchase a new car in Canada when so many automobile reviews are written for the US audience.

Write a simple utility class that can convert from US style fuel economy numbers (miles per gallon) to fuel consumption numbers (litres per 100 km).

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 FuelEfficiency
  2. an attribute named LITRES_PER_GALLON having the correct value (the number of litres in one US gallon)
  3. a method named toLitresPerHundredkm that performs the work described in its contract

The "tricky" part of this question is working out the mathematical equation(s) needed to perform the conversion correctly. An approximate conversion is given by:

milesPerGallon = 235.215 / litresPerHundredkm

but you should implement the exact formula using LITRES_PER_GALLON (DistanceUtility might help you out here, too). If you use the approximate conversion then your method will not pass the automated test used to check your code.

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 L2R FuelEfficiency.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.