Chapter 2
CS 200 - Programming I - Summer 2020
Mark Powers
Variables
● A name and a value
int x = 10;
int y = x + 2
x 10
int z = 20;
y 12
z 20
Variables
● A name and a value
int x = 10;
int y = x + 2
x 10
int z = 20;
● Assignment operator ‘=’ y 12
int x = 10;
int y; z 20
x + 2 = y;
● = is more like
x=x+5
Variables
● A name and a value
int x = 10;
int y = x + 2
x 10
int z = 20;
● Assignment operator ‘=’ y 12
int x = 10;
int y; z 20
x + 2 = y;
● = is more like
x x+5
Variables - Names
● Should start with a lowercase letter, each new word start with a capital letter
● Should name something that tells what it is used for
● Case sensitive
int x
int width
int Width
int theWidth
● Cannot use a reserved word
Variables - Declaration vs Assignment
● Declaration: TYPE NAME
○ int width
○ int q
● Assignment: Assigning a value
○ width = 10
○ q = false
● May be combined on same line
○ int width = 10
○ int width;
width = 10;
Integers
● Numbers are infinite
● Computers have a limited amount of memory
● A bit stores 1 or 0
1 bits -> 2 2 bits -> 4 3 bits -> 8 4 bits -> 16 N bits
0 00 000 0000
1 01 001 0001 ?
10 010 0010
11 011 0011
100 0100
101 ...
110
111
Integers
● Numbers are infinite
● Computers have a limited amount of memory
● A bit stores 1 or 0
1 bits -> 2 2 bits -> 4 3 bits -> 8 4 bits -> 16 N bits
0 00 000 0000
1 01 001 0001 2^N
10 010 0010
00 011 0011
100 0100
101 ...
110
111
Integers
● What happens if we add 3 and 6?
000 0
○ No way to represent 9 001 1
● What about 1 + 7? 010 2
011 3
● Combinations are reused 100 4
101 5
110 6
111 7
Integers
● What happens if we add 3 and 6?
000 0
○ No way to represent 9 001 1
● What about 1 + 7? 010 2
011 3
● Combinations are reused 100 4
101 5
int i = 2000000000; 110 6
111 7
int j = 2000000000;
[Link](i);
[Link](j);
[Link](i + j);
Floating point (decimal)
● Finite number of bits
● Cannot represent all numbers
● Infinity, -Infinity, NaN
Character
● Represents a single character
● Single character in single quotes ‘
○ ‘A’
● Strings can be many characters, and are in double quotes
Scanner scnr = new Scanner([Link]);
String name = [Link]();
// What type is name if I enter one letter?
char c = [Link](0);
Primitive Types - 1 byte = 8 bits
char 2 bytes Character ‘a’ , ‘A’ Single character in
single quotes ‘_’
boolean 1 bit True or false value true, false
byte 1 byte integer
short 2 bytes integer
int 4 bytes integer integer
long 8 bytes integer Integer ending with l
or L
float 4 bytes Floating point Decimal value
ending with F
double 8 bytes Floating point Decimal value
Literal
● Data typed literally into a program
○ int x = 100
○ String name = “Mark”
○ long y = 12345678900000
Type conversion
● Value can fit into larger type - implicit
○ int x = 10;
long y = x;
○ int x = 10;
float y = x;
● Programmer must cast - explicit
○ long x = 10;
int y = (int) x;
○ float x = 10.5;
int y = (int) x;
byte short int long float double
char
Arithmetic Operations
● +-*/%
● Precedence order
● Division truncates if
both operands are
Integer types
10/3 vs -10/3
● % gets division remainder
● Different operands change to
downstream type
10.0 / 3
10/3.0
Arithmetic Details
● [Link](5 + 5);
● [Link](5 + 5.0);
● [Link](5.0 + 5);
● [Link](5 / 2);
● [Link](5.0 / 2);
● [Link](5 / 2.0);
Arithmetic Operations
● 3 * 5 / (3.0 + 3)
○ 2
○ 3.0
○ 2.5
● 2*5/3+3
○ 6
○ 6.3333333
○ 1
Arithmetic Operations Problem
● Given a time in minutes, convert it into hours and minutes
● Example:
Enter minutes: 140
2 hours and 20 minutes
● Use integer division and remainder
Arithmetic Operations Problem
● Given a start time and an end time, how much time has passed? Assume 24
hour time
● Example:
Enter hour: 3
Enter minute: 40
Enter hour: 9
Enter minute: 10
5 hours and 30 minutes
● Use integer division and remainder
Arithmetic operation problem
Scanner scnr = new Scanner([Link]);
[Link]("Enter hour:");
int hour1 = [Link]();
[Link]("Enter minute:");
int minute1 = [Link]();
[Link]("Enter hour:");
int hour2 = [Link]();
[Link]("Enter minute:");
int minute2 = [Link]();
int totalMinutesPassed = (hour2-hour1)*60 + (minute2 - minute1);
int hoursPassed = totalMinutesPassed / 60;
int minutesPassed = totalMinutesPassed % 60;
[Link](hoursPassed + " hours and " +minutesPassed + "
minutes");
Constants and Magic Numbers
● Avoid having literal numbers in multiple places
● Make it easier to understand what the number means
○ int hoursPassed = totalMinutesPassed / 60;
○ final int MINUTES_PER_HOUR = 60;
int hoursPassed = totalMinutesPassed / MINUTES_PER_HOUR;
● Constants are labeled final and written in caps with _
Operation Associativity
● Java must evaluate in some order
● “1 + 2 = “ + 1 + 2
Operation Associativity
● Java must evaluate in some order
● “1 + 2 = “ + 1 + 2
● “1 + 2 = 1“ + 2
Operation Associativity
● Java must evaluate in some order
● “1 + 2 = “ + 1 + 2
● “1 + 2 = 1“ + 2
● “1+2 = 12”
Operation Associativity
● Java must evaluate in some order
● “1 + 2 = “ + 1 + 2
● “1 + 2 = 1“ + 2
● “1+2 = 12”
● “1 + 2 = “ + (1 + 2)
● “1 + 2 = “ + 3
● “1 + 2 = 3”
Methods
● A named section of code that can be reused easily
● Can also be used to group code
● [Link]("Enter hour:");
int hour1 = [Link]();
[Link]("Enter minute:");
int minute1 = [Link]();
[Link]("Enter hour:");
int hour2 = [Link]();
[Link]("Enter minute:");
int minute2 = [Link]();
Methods
public static void main(String[] args){
...
int hour1 = getHour(scnr);
int minute1 = getMinute(scnr);
int hour2 = getHour(scnr);
int minute2 = getMinute(scnr);
...
}
public static int getHour(Scanner scnr){
[Link]("Enter hour:");
return [Link]();
}
Methods
public static void main(String[] args){
...
//[Link]("Enter hour:");
int hour1 = getHour(scnr); Method Call
int minute1 = getMinute(scnr);
Argument: the value to be stored in the
int hour2 = getHour(scnr); corresponding parameter
int minute2 = getMinute(scnr);
... Parameter: the variable where argument
value is stored
}
public static int getHour(Scanner scnr){
[Link]("Enter hour:");
return [Link]();
Method Definition
}
Methods
public static void main(String[] args){
...
[Link]("Enter hour:");
int hour1 = getHour(scnr);
int minute1 = getMinute(scnr);
int hour2 = getHour(scnr);
int minute2 = getMinute(scnr);
...
}
public static int getHour(Scanner scnr){
[Link]("Enter hour:"); Method value: value the
return [Link](); method call results in
}
Methods
public static void main(String[] args){
int num = 5;
int squaredNum = square(num);
}
public static int square(int num){
int x = num * num;
return x;
}
Methods
public static void main(String[] args){
int num = 5;
int squaredNum = square(num);
}
public static int square(int num){
int x = num * num;
return x; Local variable: only
accessible within method it
} is declared in
Methods
public static void main(String[] args){
int num = 5;
int squaredNum = square(num);
}
public static int square(){
int x = num * num;
return x;
}
Methods
public static void main(String[] args){
int num = 5;
int squaredNum = square(num);
}
public static int square(){ Since num is also a local
variable in main, we cannot
int x = num * num; access it in square
return x;
}
Methods
public static void main(String[] args){
int num = 5;
int squaredNum = square(num);
}
public static int square(int number){
int x = number * number;
return x;
}
Methods
public static void main(String[] args){
int num = 5;
int squaredNum = square(num);
}
public static int square(int number){
int x = number * number;
return x;
}
Name does not matter, since
value is passed in, not the
variable
Methods
public static void main(String[] args){
int num = 5;
square(num);
}
public static int square(int number){
int x = number * number;
return x;
}
Methods
public static void main(String[] args){
int num = 5;
square(num);
}
public static int square(int number){
int x = number * number;
return x; Method (returned) value is
never used, so no variable is
} changed in main.
API Methods
● Application Programming Interface
● Existing methods in existing classes.
● Built in Math class
○ round, floor, abs
○ [Link]
● Static methods in API are called with class name
○ [Link](3.14159)
● Parts of API
○ Return type, method name, parameters, description
● Rather than needing full code, we can just use the description
Assignment right vs left side
● Left hand side MUST be a variable
● Right hand side can be any expression with the same type as left hand side
○ ___ = [Link](10.1);
○ ___ = x + 10;
○ int n = ____;
Testing
● Can check the return value of a method is what we expect for a given value
● Can write test to the description of a method
● Do not change the return type, name, or parameter types of a given method
public static int square(int number){
int x = number * number;
return x;
}
public static void testSquare(){
if(square(3) != 9){
[Link](“testSquare failed”);
} else {
[Link](“testSquare passed”);
}
}