Purpose: Writing programs that use methods and classes.
Goal: Complete as many of the exercise questions as you can. If you are keeping up, you need to do at least the core questions. The additional questions are more challenging and are designed to stretch the more confident programmers. Don't worry if you can't do them now, but be prepared to come back and try them later on.
Feedback: It is important that you get feedback on your exercise answers so that you know they are correct, that you are not making common mistakes, that the program code is properly presented and that you are confident you have solved the problem properly. To do this, get your answers reviewed by a lab demonstrator during lab sessions.
NOTE: You must keep all exercise answers as they form a record of your progress. After the exams you may be required to hand-in all exercises and coursework answers, as part of the course assessment process.
Q2.1 Consider displaying a large digit formed from star characters:
******
*
******
*
******
Write a Digit class that includes a set of instance methods for displaying each of the digits 0 to 9, dot and minus, in the way shown above. When one of the methods is called it should display one line of a large digit, with the line to display being given as a parameter (e.g., large2(3) would display the 3rd line of a large 2). To avoid duplicating sections of code, provide private utility methods to display things like a single line of stars ("******"), and call these utility methods from the digit methods.
Next write a number class that uses a Digit object to display numbers (create the Digit object in the constructir and a reference to it in an instance variable). Provide a method taking an integer parameter that displays the parameter value in big digits. For example, display(123) would output as:
* ****** ****** ** * * * ****** ****** * * * *** ****** ******
Also provide a method taking a double argument to output a floating point number:
* ****** ****** ** * * * ****** ****** * ** * * *** ** ****** ******
Again look for private utility methods to avoid duplicating sections of code.
Note: You can only output normal characters left-to-right, line-by-line, so think carefully about how your methods work. To output 123 you have to output the first line of the digit 1, followed by the first line of the 2 and then the first line of the 3, with spaces in between and a newline at the end of the line. This is then repeated for the second, third and subsequent lines. You cannot display a complete 1, followed by a 2 and then a 3.
Q2.2 Convert your answer to Q2.1 so that it runs as a drawing program.
Hint: You can draw an entire digit at one go but need a way of dealing with the coordinate system.
Q2.3 Consider a table that shows temperature conversions from Celsius to Fahrenheit, like this:
Temperature Conversion
----------------------
C F C F C F C F C F
0 32 1 33 2 35 3 37 4 39
5 41 6 42 7 44 8 46 9 48
10 50 11 51 12 53 13 55 14 57
...
and so on.
Write a temperature conversion table class that can display a table, allowing temperature range, the number of columns to display (one column is one C/F pair) and the title label to be specified. The columns of numbers should line up neatly. Make your own decisions over what methods are needed.
Next write a control class that uses the table class to display tables in response to user input (to specify the format of the table to display). Note, all input should be in the control class.
Q2.4 Write your own String class (class MyString), trying to implement as many methods from the real String class as possible. The characters representing a string should be stored in a char array. Your string class implementation should not make any use of the real String class (or class StringBuffer).