Computer Science
15-111 (Sections A & B), Spring 2007
Class Notes: 17-Jan-2007
Course Web Site, Syllabus, etc:
These should
be online by tomorrow. We will review
them in recitation, where you will also receive the programming portion of
Homework #2, which is due Monday.
My contact info:
Email me at
koz@cmu.edu.
My office is Doherty 4301-D (most easily found from Wean 8th
floor passage into Doherty). Office
hours are right after each class (including today).
Textbook:
For the
first 1/3rd or so of the course, we will use an online textbook:
Java by Dissection, 2nd
Edition, by Ira Pohl and Charlie McDowell
Download
the pdf for $5 (you may also buy the printed book for about $20) at:
http://www.lulu.com/content/267149
You can
find all the example code online at:
http://www.soe.ucsc.edu/~charlie/java/jbd/
Reading:
On Monday,
we covered Chapter 1 (Introduction).
This lecture, we are covering:
Chapter
2: Program Fundamentals
Appendix
A.1: Integer Binary Representation
Appendix
B.1: Operator Precedence Table (portions
thereof)
Quiz:
Our first
quiz, covering Chapters 1-3 and Appendices A-B, is on Wednesday 24-Jan (note:
we are moving quickly as this is mostly material you should have seen already).
Topic Outline:
1. Lexical Elements
White space, comments, keywords, identifiers, literals,
operators, punctuation
literal |
Value |
32 |
decimal
32 |
032
(octal) |
decimal
26 (3*8 + 2) |
0x32
(hexadecimal) |
decimal
50 (3*16 + 2) |
132l |
132 as a
long |
1.32f |
1.32 as a
float |
1.32d |
1.32 as a
double |
1.32 |
1.32 as a
double (default) |
2.17e-32 |
2.17 x 10-32 |
a |
The
character a (ASCII 97) |
a |
The
String a |
2. Primitive Data Types (8)
byte, short, int, long, float, double, char, boolean
Type |
Bits |
Min |
Max |
byte |
8 |
-128 |
127 |
short |
16 |
-32768 |
32767 |
char |
16 |
0 |
65536 |
Int |
32 |
-2147483648 |
2147483647 |
long |
64 |
About
-9.2 x 1018 |
About 9.2
x 1018 |
Type |
Bits |
Approximate Range |
Approximate Precision |
float |
32 |
+-10-45
to +-10+38 |
7 decimal
digits |
double |
64 |
+-10-324
to +-10+308 |
15
decimal digits |
3. User Input (Section 2.6)
Scanner (see: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html)
import, assert and AssertionException
Problems with sample code: no comments, throws
exceptions, terrible UI
4. printf
%c character
%d decimal
integer
%e floating-point
number in scientific notation
%f floating-point
number
%g the shorter of
e-format or f-format
%s string
Code |
Output |
System.out.printf("<%6.2f>",13.579); |
<
13.58> |
System.out.printf("%c",65); |
A |
System.out.printf(%d,3.2) |
Runtime exception! |
System.out.printf("%f",67); |
Runtime exception! |
5. ASCII codes you need to know:
0 null (in
some OSs signifies end-of-file)
7 alert/bell
(it really rings!)
9 tab
10 linefeed
13 carriage
return
32 (space)
48 0
57 9
65 A
90 Z
97 a
122 z
6.
Mixed mode arithmetic
a. Java arithmetic uses only int, long,
float, double (not char, byte, short)
b. Java uses numeric promotion (a kind of widening
conversion) to make both arguments the same type
c. Widening conversions are rarely
lossy, but can be
eg: System.out.println((int)(float)123456789); ΰ
123456792
d. Narrowing conversions only by
explicit casting. These often are lossy.
7. Arithmetic Errors
a. Overflow is unchecked! (results in gibberish)
b. Underflow is unchecked! (results in 0)
c. Floating point has no errors, just
+Inf, -Inf, and NaN
d. Floating point is approximate, so dont use ==
Code |
Output |
System.out.println(0/0); |
Runtime exception! |
System.out.println(0f/0); |
NaN |
System.out.println((1.3
* (1.8 / 1.3)) == 1.8); |
true |
System.out.println(((1.3
* 1.8) / 1.3) == 1.8); |
false |
8. Division operator (/)
Important: Integer
division truncates, it does not
round! (eg, 7 / 4 == 1, not 2)
9. Modulus operator (%)
a. Integers: remainder
(eg, 23 % 7 == 2)
b. The textbook is wrong about
floating-point modulus!!! Dont worry,
though, since nobody uses it (and neither should you, at least not without a
really wonderful comment in your code!!!).
10. Operator precedence and
associativity
Operator |
Associativity |
( ) ++ (postfix) -- (postfix) |
left-to-right |
+
(unary) - (unary) ++ (prefix)
-- (prefix) |
right-to-left |
new |
right-to-left |
* / % |
left-to-right |
+ - |
left-to-right |
= +=
-= *= /=
|
right-to-left |
Hint:
When in doubt, use parentheses!!!
11. Twos Complement
a. To negate: flip bits and add 1
b. So subtraction is addition (less
silicon!), and no +-0.
12. Base conversions (time permitting)
You should be comfortable converting from and to binary,
octal, decimal, and
hexadecimal, and doing so without a calculator.