The "dangling if" problem
What can be improved about the following code?
if (x < 0) cfw_
System.out.println("x is negative");
else if (x >= 0) cfw_
System.out.println("x is non-negative");
The second if test is unnecessary and can
* For both sections of CS101 combined.
Strings as user input
The Scanner's next method reads a word (or token) of input as a
Foxtrot by Bill Amend
Precondition and Postcondition Comments
If a returned value is the only postcondition, the
postcondition often is not stated (as it is fairly obvious).
The statement of the precondition an
Fence post analogy
We print n numbers but need only n - 1 commas.
This problem is similar to the task of building a fence with
lengths of wire separated by posts.
often called a fencepost problem
If we repeatedly place a p
If you did not pick up your exam, see me at
the end of class
HW6 due Wednesday 9/24, 11:59PM
Tutorial #4 due Thursday 9/25, 11:59PM
Be careful what you tell a programmer
A note about late days
If you never turn in
multiplication tutor program
Ask user to solve problems with random
numbers from 1-20.
The program stops after an incorrect answer.
14 * 8 = 112
5 * 12 = 60
8 * 3 = 24
5 * 5 = 25
20 * 14 =
assertion: A statement that is either true or false.
Java was created in 1995.
The sky is purple.
23 is a prime number.
10 is greater than 20.
x divided by 2 equals 7. (depends on the value o
Invalid user input
Recall: When the token doesn't match the type the Scanner
tries to read, the program crashes.
Scanner console = new Scanner(System.in);
System.out.print("How old are you? ");
int age = console.next
char vs. int
All char values are assigned numbers internally by the computer,
called ASCII values.
'A' is 65, 'B' is 66, ' ' is 32
'a' is 97, 'b' is 98, '*' is 42
Conveniently, the alphabet is in order ('b' < 'j')
HW 9 due Wednesday night
Read section 6.3
Quick review of File I/O
To read a text file, we can create a Scanner object on a
File object that is conn
Exercise: Sec;on a=endance ques;on
Assume that CS1101 was a 4 credit lab class (3 credits for
lecture, 1 credit for a weekly lab).
Lets say the weekly labs are run by the course TAs. They
Use an array to solve the weather problem:
How many days' temperatures? 7
Day 1's high temp: 45
Day 2's high temp: 44
Day 3's high temp: 39
Day 4's high temp: 48
Day 5's high temp: 37
Day 6's high temp: 46
Objects and References
Recall that when you declare a variable of class type, that
variable is a reference variable.
You sGll need to create an object via the new operaGon.