V22.0101 (Honors) ......Homework Assignment 2 ................ Fall 2006
Base Conversion with Array of Objects

Assigned: Mon Sept 18.
Due: Tue Oct 3 at midnight.

Please note that I will be out of town Sept 21 - Oct 1 (I will probably read email occasionally). Prof Evan Korth will teach the class Sept 25 and 27; he will cover arrays, including two dimensional arrays. Also please notice that on Mon Oct 2, class will be held in Room CIWW 101, in a special lecture combined with one of the regular sections. I will introduce Frame Windows, which are used for GUI programming, in this lecture.

If you can, start work on this right away so that if you have questions, you can come to see me (or send email) before I go away.

This homework consists of ONLY ONE question. I'm assigning the TicTacToe question as a separate assignment due later.

The homework builds on Homework 1, adding aspects of objects and arrays. But also, you should take the opportunity to correct the faults that I pointed out in your Homework 1 submission. Things to watch out for are

You should now handle bases up to 36, using the letters A through Z for digits after 9. For example, the decimal number 35 is Z in base 36, the decimal number 36 is 10 in base 36, and the decimal number 59 is 1N in base 36.

Suppose that we want to prompt the user for a bunch of numbers input in various bases, and that we want to save this information in an array of objects, of a class that you implement called Number, so that we can print out the numbers in either the original bases or in any other specified base. Each Number object will need two data fields (instance variables), to save the original base and the number converted to the int format. This will be enough information to be able to print out the representation of the numbers either in the original bases or in any specified converted base. (We could also save the original string, but this takes more storage and we don't need it as long as we have the original base saved.)

Start by implementing the Number class, with 2 private data fields (instance variables), for storing the original base and the int representation of the number.

Then write a constructor for the Number class that sets these data fields, passing the original base and original string as parameters. The constructor should call str2int, since it needs to determine the corresponding int value, and then set the data fields accordingly. Also write a second constructor that only has one parameter (the string) that calls the first constructor passing 10 as the base (thus 10 is used as the default base).

Then write another method that returns a string, converting the int field value back to a string using a specified base by calling int2str. The methods str2int and int2str should be incorporated into the Number class and they should be private, so that they cannot be called from outside the class. They could be static, as in Homework 1, in which case they cannot access the data fields of the class, or non-static, in which case they can access and/or set the data fields directly.

Finally the Number class will need a method that returns the value of the base, since the data field is private and cannot be accessed directly outside the class.

Then write another method outside the Number class to prompt the user for string and base input and, for each number input by the user, construct a new object of the Number class, which you store in an array of Number objects. After the user enters a bunch of numbers (as before use a while loop which is terminated when the user enters the empty string), ask the user to enter a new base and then use a loop to convert and print all the numbers that were entered in the new base. Nest this in another loop to allow the user to do this for several different bases. Finally print all the original numbers and bases again, to show that that information was not lost during the convert-and-print stage. Exactly how you organize the program is up to you, but it should have all the aspects mentioned above, it should be well documented, and the prompts to the user (me!) for input should be clear enough so that it is obvious what the program expects from the user.

Notice that only one public class is allowed per file, and this must be the class with the main method. See the template provided here. See also p.218-219.