Homework #3

Problem 1 (10 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. Explain why you're having trouble doing it.

Problem 2 (20 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.

The other thing you have to do for this problem is to write a tester for your new class. This is not a unit-test (you have to write unit-tests anyway), instead a wrapper program that uses your class and validates that the sequence of numbers it generates appears to be random.

Problem 3 (20 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".

Problem 4 (40 points)

In this problem you'll get to write a little bit of Ruby code. For simplicity, take the problem described in HW-1 and produce a Ruby implementation.

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.


Problems #1, #2, and #3 were inspired by an assignment posted here.

$Id: hw3.html,v 1.1 2011/03/23 17:20:36 virgil Exp $