[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]
COMP2100/2500
Homework 1Note: For Exercises 1–3, see Lecture 2 for details.)
Exercise 1
Get hold of a lab notebook (either a proper book or a ring binder) and get it set up ready to use for COMP2100.
Exercise 2
Start recording all time you spend on COMP2100 work on a Time Recording Log form.
Exercise 3
Before your lab, complete a Weekly Time Use Summary, breaking your time use down by day and by the categories given in Lecture 2.
Exercise 4
Write the following program, making a note of how long it takes you to complete it. (You do not have to follow the PSP while you write this program. Wait until you have learned the initial process in Lecture 3 and Lab 1.)
Write an Java program to print out a table of all characters with character codes in the ranges 32–126 and 160–255.
Your final output should look exactly like this:
32 64 @ 96 ` 160 192 À 224 à 33 ! 65 A 97 a 161 ¡ 193 Á 225 á 34 " 66 B 98 b 162 ¢ 194 Â 226 â 35 # 67 C 99 c 163 £ 195 Ã 227 ã 36 $ 68 D 100 d 164 ¤ 196 Ä 228 ä 37 % 69 E 101 e 165 ¥ 197 Å 229 å 38 & 70 F 102 f 166 ¦ 198 Æ 230 æ 39 ' 71 G 103 g 167 § 199 Ç 231 ç 40 ( 72 H 104 h 168 ¨ 200 È 232 è 41 ) 73 I 105 i 169 © 201 É 233 é 42 * 74 J 106 j 170 ª 202 Ê 234 ê 43 + 75 K 107 k 171 « 203 Ë 235 ë 44 , 76 L 108 l 172 ¬ 204 Ì 236 ì 45 - 77 M 109 m 173 205 Í 237 í 46 . 78 N 110 n 174 ® 206 Î 238 î 47 / 79 O 111 o 175 ¯ 207 Ï 239 ï 48 0 80 P 112 p 176 ° 208 Ð 240 ð 49 1 81 Q 113 q 177 ± 209 Ñ 241 ñ 50 2 82 R 114 r 178 ² 210 Ò 242 ò 51 3 83 S 115 s 179 ³ 211 Ó 243 ó 52 4 84 T 116 t 180 ´ 212 Ô 244 ô 53 5 85 U 117 u 181 µ 213 Õ 245 õ 54 6 86 V 118 v 182 ¶ 214 Ö 246 ö 55 7 87 W 119 w 183 · 215 × 247 ÷ 56 8 88 X 120 x 184 ¸ 216 Ø 248 ø 57 9 89 Y 121 y 185 ¹ 217 Ù 249 ù 58 : 90 Z 122 z 186 º 218 Ú 250 ú 59 ; 91 [ 123 { 187 » 219 Û 251 û 60 < 92 \ 124 | 188 ¼ 220 Ü 252 ü 61 = 93 ] 125 } 189 ½ 221 Ý 253 ý 62 > 94 ^ 126 ~ 190 ¾ 222 Þ 254 þ 63 ? 95 _ 191 ¿ 223 ß 255 ÿ(Well, almost exactly. Depending on the system, sometimes character 173 doesn't print.)
Notice the gap where character 127 (the DEL character) would otherwise be.
Hints
In Eiffel this was easy, but Java manages to make it tricky. Partly this is because Java has full support for Unicode, making all characters from all languages available. In principle this exercise would be just as easy (or difficult) printing Chinese, Thai, Greek, Russian or Korean characters.
Printing the left half of the table (characters 32 to 126) is easy. The trick is to use a cast, to convert a variable of type int into a char.
You will need some nested loops. That's the main point of the exercise, and it's no different if you only do the left half of the table. So if you want to stop there, that's fine. Those who are interested, read on.
If you use the same method that worked for the left half and just continue it happily to do the right half as well, you'll probably end up with a whole lot of question marks (?).
There are a couple of ways to finish the job. The “sneaky” one is to cast the int to a byte rather than a char, and then set your terminal (where the output is written) to character encoding ISO-Latin-1 (otherwise known as ISO-8859-1). The “proper” way uses classes OutputStreamWriter (in which you can set the character encoding to ISO-8859-1) and PrintWriter. Then the cast to char works the way it should. If you take the time to figure this out, you should have no trouble some time in the future writing software for an international market.
When you have completed this program, print out a copy of the source code and the output, and store them in your lab notebook (either by sticking them in or by punching holes and putting them in your ring binder).
[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]
Copyright © 2005, Ian Barnes, The Australian National University
Version 2005.3, Thursday, 24 February 2005, 09:29:49 +1100
Feedback & Queries to
comp2100@cs.anu.edu.au