CSE 160
Lab 10
Due Tuesday, April 21, 2009


Reading:
Read Chapters 9, and 10 in the Objects First text.  Work along with it, using BlueJ.


A) int to String

1) Design a recursive algorithm for converting an int to a string of its English spelled-out wording. For example numberString(59) should return the string "FIFTY_NINE", and numberString(513782) should return:
            "FIVE_HUNDRED_THIRTEEN_THOUSAND_SEVEN_HUNDRED_EIGHTY_TWO"
Have it produce all caps and use underbars between words, as above. It can return "MANY" if the argument is a million or more.

2) Write a CodeGen class which, when given N, prints code to the the Java console, which you can cut and paste into your enum class to have it do arithmetic modulo N.  For example, if N=100, it prints out the following: (but the dot dot dot parts are filled in)

    ZERO, ONE, TWO, THREE, ..., NINTY_NINE, ERROR;

    public Num successor() {
       if (this==ZERO) return ONE;
       if (this==ONE) return TWO;
       if (this==TWO) return THREE;
       ...
       if (this==NINTY_EIGHT) return NINTY_NINE;
       if (this==NINTY_NINE) return ZERO;
       return ERROR;
    }


3) Copy your generated code from the console into your enum class from last assignment and test that you can do arithmetic modulo 100.


B) Using the Comparable interface.

1) Read the JavaDocs for the Comparable interface.  If you create a class which implements Comparable, you must create an x.compareTo(y) method which returns 0 if x=y, returns something negative if x<y, and returns something positive if x>y. You can decide what you mean by <, =, and >, as long as it is consistent for a total ordering of elements of the class. Then you can use various methods for sorting and searching which work on any Comparable objects.  Look up the JavaDocs for Collections.sort() and Collections.binarySearch() for examples.

2) String implements Comparable, in the usual alphabetic manner, e.g., "aa" < "z".  Make a Test class which creates a List of a dozen or so Strings, of various lengths, not alphatized.  Use the above method to sort them, and print out the sorted list. Make a loop to read a String from the console and use binarySearch() to print out its index in the list or "Not Found" if the input string is not in the list.

3) Now make your own class MyString which implements Comparable with a different ordering. Have a constructor which creates a MyString from a String argument. Implement compareTo() in such a way that short ones come before long ones, and the usual alphabetic order applies between ones of the same length. So unlike with strings, now "aa" > "z". Repeat the above steps: Create a list of the same MyString objects, sort them, print out the sorted list, and search in the list, but now your ordering will be used.


C) Foxes and Rabbits

1) From the text, do exercise 10.47 (in the 4th edition, which is 10.42 in the 3rd edition), to create an Actor class of which Animal is a subclass.

2) From the text, do exercise 10.51 (in the 4th edition, which is 10.46 in the 3rd edition), to create a new type of Actor with its own behavior (and a new color). For example a radioactiveWombat object might move randomly and kill everything within a given radius of itself. But you can decide the type of object and behavior to implement. It should do something interesting when you do runLongSimulation().

Submit your .jar files for all three parts.