Chapter 3
CS 200 - Programming I - Summer 2020
Mark Powers
Base 10 (decimal) to Base 10
Consider 6301
Break input into digits 6 3 0 1
Get value of each digit 6 * 10^3 3 * 10^2 0 * 10^1 1 * 10^0
in new base 6 * 1000 3 * 100 0 * 10 1*1
Add the values together: 6301
0123456789
Base 2 (binary) to Base 10
Consider 1011 base 2
Break input into digits 1 0 1 1
Get value of each digit 1 * 2^3 0 * 2^2 1 * 2^1 1 * 2^0
in new base 1*8 0*4 1*2 1*1
Add the values together: 8 + 0 + 2 + 1 = 11 base 10
01
Base 10 to Base 2
Consider 21 base 10
32 16 8 4 2 1
Find largest power of 2 that fits: 16
Subtract value that fit and repeat: 21 - 16 = 5
Base 10 to Base 2
Consider 21
32 16 8 4 2 1
Find largest power of 2 that fits: 16
Subtract value that fit and repeat: 21 - 16 = 5
Find largest power of 2 that fits: 4
Subtract value that fit and repeat: 5 - 4 = 1
Base 10 to Base 2
Consider 21
32 16 8 4 2 1
Find largest power of 2 that fits: 16
Subtract value that fit and repeat: 21 - 16 = 5
Find largest power of 2 that fits: 4
Subtract value that fit and repeat: 5 - 4 = 1
Find largest power of 2 that fits: 1
Subtract value that fit and repeat: 1 - 1 = 0
Reached 0, so now we fill in the table
Base 10 to Base 2
0 1 0 1 0 1
Consider 21
32 16 8 4 2 1
Find largest power of 2 that fits: 16
Subtract value that fit and repeat: 21 - 16 = 5
Find largest power of 2 that fits: 4
Subtract value that fit and repeat: 5 - 4 = 1
Find largest power of 2 that fits: 1
Subtract value that fit and repeat: 1 - 1 = 0
Reached 0, so now we fill in the table
Base 10 to Base 2
0 1 0 1 0 1
Consider 21
32 16 8 4 2 1
Find largest power of 2 that fits: 16
Subtract value that fit and repeat: 21 - 16 = 5
Find largest power of 2 that fits: 4
Subtract value that fit and repeat: 5 - 4 = 1
Find largest power of 2 that fits: 1
Subtract value that fit and repeat: 1 - 1 = 0
Reached 0, so now we fill in the table
We can add leading 0s to pad to needed length: 00010101
Any base N
● N -> 10 Do first process computing value of each digit
● 10 -> N Find largest power of N that fits
Hexadecimal - Base 16
● 0123456789ABCDEF
● Byte: 8 bits or 2 hexademical digits
● 0101 1001 0000 0 1000 8
0001 1 1001 9
0010 2 1010 A
0011 3 1011 B
0100 4 1100 C
0101 5 1101 D
0110 6 1110 E
0111 7 1111 F
Hexadecimal - Base 16
● 0123456789ABCDEF
● Byte: 8 bits or 2 hexademical digits
● 0101 1001 -> 59 0000 0 1000 8
0001 1 1001 9
0010 2 1010 A
0011 3 1011 B
0100 4 1100 C
0101 5 1101 D
0110 6 1110 E
0111 7 1111 F
Hexadecimal - Base 16
● 0123456789ABCDEF
● Byte: 8 bits or 2 hexademical digits
● 0101 1001 -> 59 0000 0 1000 8
● Computers can only store 0 and 1 0001 1 1001 9
but hex digits are easier to read 0010 2 1010 A
0011 3 1011 B
0100 4 1100 C
0101 5 1101 D
0110 6 1110 E
0111 7 1111 F
HTML Color codes
● One byte for each color
● Each color has red, green, and blue
● #ff0000
○ Red: 11111111
○ Green: 00000000
○ Blue: 00000000
● #ff1a22
○ Red: 11111111
○ Green: 00011010
○ Blue: 00100010
Characters
● char is 2 bytes
● [Link](‘A’ + 1);
Characters
● char is 2 bytes
● [Link](‘A’ + 1);
● [Link]( (char) (‘A’ + 1) )
Characters
● Characters for digits stored with different values
● [Link](‘0’ + 1)
● Use the method [Link]()
● [Link]([Link](“0”) + 1)
Strings
● String name = “Mark”;
[Link]([Link]());
[Link]([Link](0));
Static vs Instance
● [Link]()
○ Called on an object (NOT [Link]())
● [Link]()
○ Called on the class name
● Check if documentation has static modifier by return type
Constructors
● Use new keyword
● Create an object instance
○ new Scanner(), new Random()
○ String can be created with literal (we don’t need to say new String())
● Can call instance methods on created object
Scanner scnr = new Scanner([Link]);
int num = [Link]();
String
● int num = “Hello”.length();
[Link](num);
● [Link](“Hello”.length());
String
● int num = “Hello”.length();
[Link](num);
● [Link](“Hello”.length());
● String text = “Hello, World”
[Link](0, 5);
[Link](text);
String
● Strings cannot be changed, so methods return a new string
● String text = “Hello, World”
String text2 = [Link](0, 5);
[Link](text2);
Random
● Used to generate a sequence of random numbers
● Random randGen = new Random();
int rand1 = [Link](5); // Generates a number from 0 to 4
int rand2 = [Link](5); // Generates a number from 0 to 4
int rand3 = [Link](5); // Generates a number from 0 to 4
// How to generate a random number from 6 to 16?
Random
● Used to generate a sequence of random numbers
● Random randGen = new Random();
int rand1 = [Link](5); // Generates a number from 0 to 4
int rand2 = [Link](5); // Generates a number from 0 to 4
int rand3 = [Link](5); // Generates a number from 0 to 4
// How to generate a random number from 6 to 16?
int rand4 = [Link](16 - 6 + 1) + 6;
● The number of values in our range is the argument, and we can shift it
Random
● Random randGen = new Random();
int rand1 = [Link](4) * 2 - 5;
[Link](4) *2 - 5
3
Random
● Random randGen = new Random();
int rand1 = [Link](4) * 2 - 5;
[Link](4) *2 - 5
0 0
1 2
2 4
3 6
Random
● Random randGen = new Random();
int rand1 = [Link](4) * 2 - 5;
[Link](4) *2 - 5
0 0 -5
1 2 -3
2 4 -1
3 6 1
Random Seed - Repeatable sequence
● Random randGen = new Random();
[Link]([Link](5));
[Link]([Link](5));
[Link]([Link](5));
○ What is printed?
● Random randGen = new Random(SEED);
[Link]([Link](5));
[Link]([Link](5));
[Link]([Link](5));
○ What is printed?
Scanner
● [Link]
● Scanner scnr = new Scanner([Link]);
● Scanner scnr = new Scanner(“Hello, Word”);
● Input is stored as a long sequence of characters
● Keeps track of position in sequence
● Tokens are separated by whitespace
Scanner
● Position starts are beginning
● next() gets the token, advances the position, skips leading whitespace
● Scanner scnr = new Scanner(“Hello, World”);
[Link]([Link]());
[Link]([Link]());
H e l l o , W o r l d
Scanner
● Position starts are beginning
● next() gets the token, advances the position, skips leading whitespace
● Scanner scnr = new Scanner(“Hello, World”);
[Link]([Link]()); // Prints “Hello,”
[Link]([Link]());
H e l l o , W o r l d
Scanner
● Position starts are beginning
● next() gets the token, advances the position, skips leading whitespace
● Scanner scnr = new Scanner(“Hello, World”);
[Link]([Link]()); // Prints “Hello,”
[Link]([Link]()); // Prints “World”
H e l l o , W o r l d
Scanner
● nextLine() gets everything up until a \n, and advances position past \n
● Scanner scnr = new Scanner(“Hello,\nWorld\n”);
[Link]([Link]());
[Link]([Link]());
H e l l o , \n W o r l d \n
Scanner
● nextLine() gets everything up until a \n, and advances position past \n
● Scanner scnr = new Scanner(“Hello,\nWorld\n”);
[Link]([Link]()); // Prints “Hello,“
[Link]([Link]());
H e l l o , \n W o r l d \n
Scanner
● nextLine() gets everything up until a \n, and advances position past \n
● Scanner scnr = new Scanner(“Hello,\nWorld\n”);
[Link]([Link]()); // Prints “Hello,“
[Link]([Link]()); // Prints “”
H e l l o , \n W o r l d \n
Scanner
● nextLine() gets everything up until a \n, and advances position past \n
● Scanner scnr = new Scanner(“Hello,\nWorld\n”);
[Link]([Link]()); // Prints “Hello,“
[Link]([Link]()); // Prints “”
[Link]([Link]()); // Prints “World”
H e l l o , \n W o r l d \n
Scanner
● nextInt() like next() but requires next token to be an int
● nextDouble()
● Will throw InputMismatchException if next token isn’t int or double
Dice roller
● Given the size of a dice, output a random number
● Example:
What size die would you like to roll? 6
You rolled a 3
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
public static void red(){
[Link](“red”);
}
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
public static void red(){
[Link](“red”);
}
main
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
public static void red(){
[Link](“red”);
}
main
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
public static void red(){
[Link](“red”); blue
}
main
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
public static void red(){
[Link](“red”); blue
}
main
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
red
public static void red(){
[Link](“red”); blue
}
main
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
red
public static void red(){
[Link](“red”); blue
}
main
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
red
public static void red(){
[Link](“red”); blue
}
main
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
public static void red(){
[Link](“red”); blue
}
main
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
public static void red(){
[Link](“red”); blue
}
main
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
red
public static void red(){
[Link](“red”); blue
}
main
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
red
public static void red(){
[Link](“red”); blue
}
main
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
red
public static void red(){
[Link](“red”); blue
}
main
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
public static void red(){
[Link](“red”); blue
}
main
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
public static void red(){
[Link](“red”);
}
main
Method calls and stack
public static void main(){
blue();
}
public static void blue(){
red();
[Link](“blue”);
red();
}
public static void red(){
[Link](“red”);
}
Method calls and stack
public static void main(){
int num = 3;
blue(num + 1);
}
public static void blue(int num1){
red(num1 + 1);
[Link](“blue ” + num1);
int num2 = num1 * 2;
red(num2 + 1);
}
public static void red(int num){ main:
num: 3
[Link](“red” + num);
}
Method calls and stack
public static void main(){
int num = 3;
blue(num + 1);
}
public static void blue(int num1){
red(num1 + 1);
[Link](“blue ” + num1);
int num2 = num1 * 2;
blue:
red(num2 + 1); num1: 3 + 1
}
public static void red(int num){ main:
num: 3
[Link](“red” + num);
}
Method calls and stack
public static void main(){
int num = 3;
blue(num + 1);
}
public static void blue(int num1){
red(num1 + 1);
[Link](“blue ” + num1);
int num2 = num1 * 2;
blue:
red(num2 + 1); num1: 4
}
public static void red(int num){ main:
num: 3
[Link](“red” + num);
}
Method calls and stack
public static void main(){
int num = 3;
blue(num + 1);
}
public static void blue(int num1){
red(num1 + 1); red:
num: 4 + 1
[Link](“blue ” + num1);
int num2 = num1 * 2;
blue:
red(num2 + 1); num1: 4
}
public static void red(int num){ main:
num: 3
[Link](“red” + num);
}
Method calls and stack
public static void main(){
int num = 3;
blue(num + 1);
}
public static void blue(int num1){
red(num1 + 1); red:
num: 5
[Link](“blue ” + num1);
int num2 = num1 * 2;
blue:
red(num2 + 1); num1: 4
}
public static void red(int num){ main:
num: 3
[Link](“red” + num);
}
Method calls and stack
public static void main(){
int num = 3;
blue(num + 1);
}
public static void blue(int num1){
red(num1 + 1);
[Link](“blue ” + num1);
int num2 = num1 * 2;
blue:
red(num2 + 1); num1: 4
}
public static void red(int num){ main:
num: 3
[Link](“red” + num);
}
Method calls and stack
public static void main(){
int num = 3;
blue(num + 1);
}
public static void blue(int num1){
red(num1 + 1);
[Link](“blue ” + num1);
int num2 = num1 * 2; blue:
red(num2 + 1); num1: 4
num2: 8
}
public static void red(int num){ main:
num: 3
[Link](“red” + num);
}
Method calls and stack
public static void main(){
int num = 3;
blue(num + 1);
}
public static void blue(int num1){
red(num1 + 1); red:
num: 8 + 1
[Link](“blue ” + num1);
int num2 = num1 * 2; blue:
red(num2 + 1); num1: 4
num2: 8
}
public static void red(int num){ main:
num: 3
[Link](“red” + num);
}
Method calls and stack
public static void main(){
int num = 3;
blue(num + 1);
}
public static void blue(int num1){
red(num1 + 1); red:
num: 9
[Link](“blue ” + num1);
int num2 = num1 * 2; blue:
red(num2 + 1); num1: 4
num2: 8
}
public static void red(int num){ main:
num: 3
[Link](“red” + num);
}
Method calls and stack
public static void main(){
int num = 3;
blue(num + 1);
}
public static void blue(int num1){
red(num1 + 1);
[Link](“blue ” + num1);
int num2 = num1 * 2; blue:
red(num2 + 1); num1: 4
num2: 8
}
public static void red(int num){ main:
num: 3
[Link](“red” + num);
}
Method calls and stack
public static void main(){
int num = 3;
blue(num + 1);
}
public static void blue(int num1){
red(num1 + 1);
[Link](“blue ” + num1);
int num2 = num1 * 2;
red(num2 + 1);
}
public static void red(int num){ main:
num: 3
[Link](“red” + num);
}
Primitive vs Reference Types
● Primitive values are stored directly in the variable
● Reference values are stored in the heap, and the variable stores a reference
● Primitive types: 8 built in, start with lowercase letter
● Reference types: All others, start will uppercase letter
public static void main(String args[]){
int age = 23;
String name = “Mark”;
stuff(page, name);
}
public static void stuff(String s, int i){ “Mark”
main:
// Do something here age: 23
} String name:
Primitive vs Reference Types
● Primitive values are stored directly in the variable
● Reference values are stored in the heap, and the variable stores a reference
● Primitive types: 8 built in, start with lowercase letter
● Reference types: All others, start will uppercase letter
public static void main(String args[]){
int age = 23;
String name = “Mark”;
stuff(page, name);
stuff:
} i: 23
String s:
public static void stuff(String s, int i){ “Mark”
main:
// Do something here age: 23
} String name:
Primitive vs Reference Types
● Primitive values are stored directly in the variable
● Reference values are stored in the heap, and the variable stores a reference
● Primitive types: 8 built in, start with lowercase letter
● Reference types: All others, start will uppercase letter
public static void main(String args[]){
int age = 23;
String name = “Mark”; STACK HEAP
stuff(page, name);
stuff:
} i: 23
String s:
public static void stuff(String s, int i){ “Mark”
main:
// Do something here age: 23
} String name:
Primitive vs Reference Types
● All objects (created with “new”) and Strings are on heap
● Local variables and parameters stored on stack
STACK HEAP
stuff:
i: 23
String s:
“Mark”
main:
age: 23
String name:
Null
● Keyword for no reference
● String s1 = null;
String s2 = “null”;
String s3 = “”;
STACK HEAP
s1:
s2: “null”
s3:
“”
Null
● Keyword for no reference
● String s1 = null;
String s2 = “null”;
String s3 = “”;
[Link]([Link]()); // 4
[Link]([Link]()); // 0
[Link]([Link]()); // ?
STACK HEAP
s1:
s2: “null”
s3:
“”
Null
● Keyword for no reference
● String s1 = null;
String s2 = “null”;
String s3 = “”;
[Link]([Link]()); // 4
[Link]([Link]()); // 0
[Link]([Link]());
STACK HEAP
NullPointerException
whenever you do . on a
null reference s1:
s2: “null”
s3:
“”
Wrapper class - Reference version of a primitive type
● Boxing: Convert to reference type
○ new Integer(10);
● Unboxing: Convert to primitive type
boolean Boolean
○ Integer i = new Integer(10);
int num = [Link](); byte Byte
● Autoboxing/autounboxing
char Character
○ Compiler automatically will convert
Between the two float Float
○ Integer i = 10;
int num = i; int Integer
● Used later with data structures long Long
● Now used to show reference/primitive short Short
● Methods: [Link](), etc.
double Double
County lakes printer
● [Link]
Lake Name County Size (Acres)
Lake Kegonsa Dane 3200
Lake Koshkonong Dane 10595
Lake Mendota Dane 9781
Lake Monona Dane 3359
Lake Waubesa Dane 2074
Lake Wingra Dane 336
● Output name and sq miles (sq miles = ac * 0.0015625)
● Kegonsa 5
Koshkonong 16.5546875
...