Jupyter at Bryn Mawr College |
|||
Public notebooks: /services/public/dblank / CS206 Data Structures / 2016-Spring / Notebooks |
Topics:
To create a variable, there is always an associated "type", and sometimes a "value."
TYPE NAME;
TYPE NAME = VALUE;
The first form:
The second form does two things:
int x; // These do not initialize
String s;
boolean first_time;
byte b;
double total;
void println(Object thing) {
System.out.println(thing);
}
void print(Object thing) {
System.out.print(thing);
}
{
println(x);
println(s);
println(first_time);
println(b);
println(total);
}
int x = 5;
String s = "Hello";
boolean first_time = true;
byte b = 0;
double total = 0.0;
Java is composed of "expressions", "statements" and "blocks".
// expressions
1 + 2
true
(1 + 2) * 3
// statements
int x = 1 + 2 * 3;
x = 1 /2;
// blocks
{
printf("Hello");
printf("World");
}
What's a bit?
All data, numbers, music, movies, etc. are at the bottom made up of 0s and 1s.
We group bits into groups:
We can represent integers by allowing each position in a byte to represent the numbers of powers of two:
2 to the 7 | 2 to the 6 | 2 to the 5 | 2 to the 4 | 2 to the 3 | 2 to the 2 | 2 to the 1 | 2 to the 0 | Integer |
---|---|---|---|---|---|---|---|---|
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 255 |
import java.lang.Math;
(Math.pow(2, 7) * 1 +
Math.pow(2, 6) * 1 +
Math.pow(2, 5) * 1 +
Math.pow(2, 4) * 1 +
Math.pow(2, 3) * 1 +
Math.pow(2, 2) * 1 +
Math.pow(2, 1) * 1 +
Math.pow(2, 0) * 1)
double bin2int(int b8, int b7, int b6, int b5,
int b4, int b3, int b2, int b1) {
return (Math.pow(2, 7) * b8 +
Math.pow(2, 6) * b7 +
Math.pow(2, 5) * b6 +
Math.pow(2, 4) * b5 +
Math.pow(2, 3) * b4 +
Math.pow(2, 2) * b3 +
Math.pow(2, 1) * b2 +
Math.pow(2, 0) * b1);
}
bin2int(0, 1, 0, 1, 0, 1, 1, 1)
int bin2int(int b8, int b7, int b6, int b5,
int b4, int b3, int b2, int b1) {
double d = (Math.pow(2, 7) * b8 +
Math.pow(2, 6) * b7 +
Math.pow(2, 5) * b6 +
Math.pow(2, 4) * b5 +
Math.pow(2, 3) * b4 +
Math.pow(2, 2) * b3 +
Math.pow(2, 1) * b2 +
Math.pow(2, 0) * b1);
return ((int)d);
}
bin2int(1, 1, 1, 1, 0, 0, 1, 1)
Fill out the following table:
2 to the 7 | 2 to the 6 | 2 to the 5 | 2 to the 4 | 2 to the 3 | 2 to the 2 | 2 to the 1 | 2 to the 0 | Integer |
---|---|---|---|---|---|---|---|---|
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | |
1 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | |
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | |
7 | ||||||||
22 | ||||||||
113 | ||||||||
256 |
Signed integers:
Unsigned integers:
Make a new function bin2signedint
that will turn a series of bits into a signed integer.
Two "nibbles", each have a range between 0 and 15
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15
0001 1010
The first nibble (on the left) is worth 16 value, the second nibble is worth one value
1111 1111
(15 * 16) + 15 = 255
Nibbles in Binary, Decimal, and Hexadecimal
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
1111 15 F
Type | Value | Size | Range |
---|---|---|---|
boolean | true or false | 1 byte | true or false |
char | character (unicode) | 2 bytes | Any Unicode character, 0 to 65,535 |
byte | integer | 1 byte | -128 to 127, or 0 to 255 |
short | integer | 2 bytes | -32,768 to 32,767 |
int | integer | 4 bytes | -2,147,483,648 to 2,147,483,647 |
long | integer | 8 bytes | -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 |
float | floating-point | 4 bytes | 1.4e-45 to 3.4e38 (+/-) |
double | floating-point | 8 bytes | 4.94e-324 to 1.79e+308 (+/-) |
int x = 1;
x = "hello"; // ERROR!
int y = 1;
y = 2;
class SomeType {
SomeType() {
// constructor code!
}
}
SomeType thing = new SomeType();
class SomeOtherType {
static void main() {
System.out.println("Hi!");
}
}
SomeOtherType.main();
Sometimes called "control blocks" or "flow of control"
if (TEST) {
STATEMENT;
...
} else if (TEST) {
STATEMENT;
...
} else {
STATEMENT;
...
}
if (x == 0) {
STATEMENT;
...
} else if (x == 1) {
STATEMENT;
...
} else {
STATEMENT;
...
}
Straightforward:
i = i + 1;
i = i - 1;
Incrementors and decrementors:
// post increment
i++;
i--;
// pre increment
++i;
--i;
Incrementors and decrementors are expressions:
x = i++ + 7;
x = ++i + 7;
for (INIT; TEST; UPDATE) {
STATEMENT;
...
}
for (int i = 0; i < 10; i = i+1) {
System.out.println(i);
}
INIT;
while (TEST) {
STATEMENT;
...
UPDATE;
}
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
}
INIT;
do {
STATEMENT;
...
UPDATE;
} while (TEST);
boolean getStatus() {
counter++;
return (counter != 10);
}
boolean trapped;
int counter = 0;
do {
System.out.println("Help!");
trapped = getStatus();
} while (trapped);