| Homework #4 | CMSC 131 |
| Due Tuesday July 5, 3:00 pm | Object-Oriented Programming I |
| Type of homework: Open | Summer 2005 |
Objective
In this homework you will practice defining classes and testing modules for classes you define.
Overview
For this assignment you will implement a class called Road which represents a road. The class implements several methods for the manipulation of Road objects. More details about the class are provided in the Specifications section. You may want to take a look at the Sample Run section before you read the detailed description. It presents a driver program (and expected output) that makes use of the Road class. Although this homework is considered an open homework, try to complete it on your own. Future homeworks will be closed homeworks. Please see the Open/Closed Policy on the class web page.
In addition to the program, you must submit a testing class, and a time log.
This homework will be graded as follows:
Code Distribution
You can access the files associated with this homework by checking out the project labeled p4. In the distribution you will find:
Road Class
Overview
The goal of this project is for you to provide a full implementation of the Road class. This class represents a road by keeping track of the road's number, name, and length.
Variables
Instance Variables
The Road class defines two private String reference variables called name and number representing a road's name and number, respectively. In addition, a private double variable called length represents a road's length.
Static Constants
The class defines the following constants:
Name Type Access Specifier Value DEFAULT_NUMBER String private "NONUMBER" DEFAULT_NAME String private "NONAME" DEFAULT_LENGTH double private 0.0 BAR_FORMAT int public 1 TAG_FORMAT int public 2
Private Non-Static Methods
Public Non-Static Methods
<roadOutput> → <roadNumber>|"<roadName>"|<roadLength>
<roadNumber>, <roadName>, and <roadLength> represent the road's number, name, and length respectively.
For example, a road named "Interstate 95", whose number is "I95" and length is 20.5 miles has the following format:
I95|"Interstate 95"|20.5
Public Static Method
In the following description <roadNumber>, <roadName>, and <roadLength> represent the instance variables associated with a Road object. The possible format choices are:
- BAR_FORMAT → The format defined by <roadOutput> above.
- TAG_FORMAT →
"Road Number: "<roadNumber>
"Road Name: "<roadName>
"Road Length (miles): "<roadLength>
createRoute - This method has as parameters two Road references named roadOne and roadTwo. This method will create a new Road object based on the parameters. The new road's number will be the concatenation of roadOne's number and roadTwo's number, separated by a comma. Likewise, the new road's name will be the concatenation of oadOne's name and roadTwo's name, separated by a comma. The new road's length will be the sum of roadOne's length and roadTwo's length.
Requirements (Read Carefully)
. Submission (Please Read)
The following is a sample run of a Driver class that relies on the Road class you must implement. The Driver is provided along with the expected output. Keep in mind that the submit server tests don't use this driver. This Driver is to help you understand the functionality associated with the different methods of the Road class.
Driver
public class Driver {
public static void main(String[] args) {
Road roadOne = new Road();
System.out.println(roadOne);
System.out.println("*****Comparison*****");
Road roadTwo = new Road("I95", "Interstate 95", 20.5);
Road roadThree = new Road("27", "Baltimore Ave.", 8);
System.out.println(roadTwo);
System.out.println(roadThree);
if (roadTwo.compareTo(roadThree) > 0)
System.out.println("roadTwo longer than roadThree");
else
System.out.println("roadTwo shorter or equal to roadThree");
System.out.println("*****Duplicate*****");
Road roadFour = new Road(roadThree);
System.out.println(roadFour);
System.out.println("*****Formatted*****");
String formatted = Road.format(roadFour, Road.TAG_FORMAT);
System.out.println(formatted);
if (roadThree.equals(roadFour))
System.out.println("Roads are considered the same.");
else
System.out.println("Roads are different.");
System.out.println("*****Merging Roads*****");
Road roadFive = Road.createRoute(roadTwo,roadThree);
System.out.println(roadFive);
System.out.println("=====");
Road roadSix = Road.createRoute(roadThree,roadTwo);
System.out.println(roadSix);
System.out.println("=====");
Road roadSeven = new Road("210", "Rhode Island Ave.", 34);
Road roadEight = Road.createRoute(roadSix, roadSeven);
System.out.println(roadEight);
System.out.println("=====");
}
}
Output
NONUMBER|"NONAME"|0.0
*****Comparison*****
I95|"Interstate 95"|20.5
27|"Baltimore Ave."|8.0
roadTwo longer than roadThree
*****Duplicate*****
27|"Baltimore Ave."|8.0
*****Formatted*****
Road Number: 27
Road Name: Baltimore Ave.
Road Length (miles): 8.0
Roads are considered the same.
*****Merging Roads*****
I95,27|"Interstate 95,Baltimore Ave."|28.5
=====
27,I95|"Baltimore Ave.,Interstate 95"|28.5
=====
27,I95,210|"Baltimore Ave.,Interstate 95,Rhode Island Ave."|62.5
=====
Challenge Problem
Remember that you are not required to implement the following problem. Please visit the course web page for information regarding challenge problems.
The challenge problem for this homework consists of adding a non-static method name partOfRoute. This method will return true if the current object represents a road that is part of the route represented by the Road reference parameter, and false otherwise.