Homework #2


Problem 1 (20 points)

Assume the following requirements: "Packets are sent from one location to another. Packets have a certain weight. Locations are characterized by their transportation facilities, e.g. railway stations, airports and highway connections. Some locations are neighbored, i.e. there exists a direct transportation route between these locations. The transportation route between the locations has a certain length, i.e. the distance between the locations. Planes, trains, and trucks are used for transportation; each plane / train / truck may load a maximum packet weight. For each packet we want to know where it is, i.e. at which location or transport (plane, train, truck)."

(i) Draw a class diagram for this problem; identify the semantic relationships and cardinalities. (10 points)

(ii) Draw the sequence diagram corresponding to a packet being sent from one location to another. (10 points)


Problem 2 (10 points)

Given the following UML write the corresponding Java code.


Problem 3 (20 points)

Integer is part of the Java API. Suppose you attempt to extend the Integer class and add a new method that returns the integer as a String that is written in hexadecimal.

(i) Explain why you're having trouble doing it. (5 points)

(ii) Alright, so the people who wrote the code had their reasons to not want you to extend the class. Give an example of how things could go very wrong if they didn't do it this way. (5 points)

(iii) Recommend a solution to the problem that doesn't involve subclassing. (10 points)


Problem 4 (10 points)

Random is part of the Java API. It has various methods to generate random integers, random doubles, etc. However, Random does not have a method that will generate a random integer between two specified integers (like 3 and 11). That would be very useful in many applications. Your task is to create a class called ImprovedRandom that can do everything that Random can do, but also adds this handy method. Use inheritance to extend Random.

Note: Your class is supposed to do everything that Random can do, so pay careful attention to what constructors should be possible. For example if Random can be instantiated with a seed parameter, then yours should be able to do the same.


Problem 5 (10 points)

StringTokenizer is part of the Java API. This class takes a string filled with words and, each time the method nextToken() is called, it returns the next word in the string. Create a new class named ImprovedStringTokenizer that acts just like a StringTokenizer but has a new method that returns all of the words in a single array.

For example, if the string is "This class is easy", then your new method will return an array where the 0th element is "This", the 1st element is "class", the 2nd element is "is", and the 3rd element is "easy".


Important: Don't forget to re-read the syllabus to make sure you understand all the deliverables for assignments in this class, including this one.



$Id: hw2.html,v 1.1 2015/02/14 16:42:02 virgil Exp $