Thurs, Mar. 30
Return midterms, go over, distribute lab4 worksheets
Note makeup midterm Tuesday after class, max score 85, second chance for some who need it
Printing Numbers
int i = 12;
This is
represented in binary in memory: 12
= 8
+ 4, so this is binary 00000…001100, 32 bits in all.
When we print this number out, it
reaches our screens in
ASCII codes: ASCII
‘1’ = 0x31, ASCII ‘2’
= 0x32, and these are both held in single bytes (i.e. 8 bits).
However, it is important to realize
that there are three
stages in this progression, not just two.
First the binary int is turned
into a Java String, with two Java chars ‘1’ and
‘2’, then System.out.print()
turns the String into two ASCII bytes on the way out.
What do the chars look like in the
String?
They are Unicode
characters, each 16 bits long.
This
allows Java to handle character sets for many languages, including
Asian
languages that have thousands of different characters.
Don’t worry, they still have the same order
as ASCII: 0, 1, 2, etc, all in a row in the sequence.
In fact all of ASCII is in its same order at
the beginning of the Unicode sequence, so everything you might know
about ASCII
holds for Unicode, except the idea that there are 128 characters.
The fact that the digit characters are all in a row means we can compute the char code from the numeric digit value:
code for '0' = 0x30
code for '1' = 0x30 + 1 = 0x31
code for '2' = 0x30 + 2 = 0x32
...
in general--
charCode = charCode0 + digitValue.
That's how far we got before the midterim.
How to express this in Java?
'0' represents the char for 0, represented by Unicode 0x0030
Turns out Java will cast '0' to int 0x30 for us in addition with an int.
Thus the expression '0' + 2 has int value 0x32.
And we can cast back: char ch = (char)('0' + 2); will give us ch = '2'
Thus we can do this formula in Java:
char charCode = (char)('0' + digitValue);
We now consider the first part of
this conversion, from int
(binary) to String.
The basic trick:
12%10 = 2, the last digit
and 12/10 =
1, the first digit
Try a bigger number:
249
249%10 = 9,
last digit
249/10 =
24, not a digit, go further:
24%10 = 4,
last digit of 24, middle of 249
24/10 = 2,
top digit
This works fine, but generates digits
in opposite order than
we want added to String. What
to do?
Could push them on a stack, pop off
in opposite order.
But recursion
gives us this stack without
setting up a data structure—let’s try
it.
Cast the problem in a recursive way:
n = 249 has
higher digits (24) and last digit
(9)
If we could find the String for the
n-1 higher digits, we
could concatenate a ‘9’ to it, which is just n%10,
and get the answer.
Visually:
<------------String for
n---------------------------> Ex: "249"
<------String
for (n/10)----->
<-String
for n%10--> Ex: "24" + "9"
Str(n) = Str(n/10)
+ Str(n%10) recursive
formula
base cases: Str(n)
= string of one digit char, n < 10
Now the book does the easier case of just printing out the numbers, rather than delivering a String representation.
In that case, we want to print out the rep for n/10, followed by the last digit, n%10.
We only need to do the n/10 case if n is over 10:
Look at Figure 7.2, pg. 258.
Trace for n = 249:
printDecimal(249)
printDecimal(24)
printDecimal(2) prints '2'
return to n=24 level, print '4'
return to n=249 level, print '9'
Then the text goes on to generalize to other bases.
Intro to pa4, soon available, due Apr. 10