Title: Announcements
1Announcements
- For homework due Thursday, work alone -- do not
work in pairs
- New class location Olin 155
- Office hour oops! Lyn MW, 1115-1215
- Review Sessions
- Do not send mail that has html or attachments
2Todays Topics
- Brief review of yesterday
- Variables and constants
- Declaration of a variable
- Assignment to a variable
- Declaration of a constant
- The conditional (if) statement
- Input
3Review
- Algorithms
- Methods
- Method call/method invocation
- Importance of comments -- document your code
- See the course website for information on Java
programming style
- Any questions on homework yet?
4Introduction to Variables
- Think of a variable as a box into which a value
can be placed
- Declarations int xint timeOfDay
32
x
-532
1115
timeOfDay
5What Happens in a Declaration?
- A variable is a name for a location in memory
- When a variable is declared you instruct the
compiler to reserve enough space for the type of
variable youre declaring
- A variable can only store one value of its
declared type
6Variable Names -- Identifiers
- Begin with a letter, _, or
- Contain letters, digits, _, and
- Generally, we dont use _ or
- One convention first letter small, capitalize
words within the identifier
- timeOfDay
- f12
- f0
- restOfInput
7Assignment
- int timeOfDay 3 // timeOfDay
- timeOfDay 430// timeOfDay
- timeOfDay 30 timeOfDay 5// timeOfDay 465
3
430
465
8Assignment Syntax
-
- Note well does not mean equality in Java
- Think of x y as Assign y to x or x becomes
y or x gets the value of y
- Think of x y as x equals y
- The expression xy yields true if x and y
contain the same value, false otherwise
9Booleans
- Boolean is a data type like int
- It has two values true and false
boolean answer int x int y x 3 y 5 a
nswer (x y)
What value ends up in the variable answer ?
10Some history
was introduced as a sign for equality in the
1500s. It was chosen because nothing could be
more equal than 2 parallel lines.
The use of for assignment was started in Fortr
an in the early 1950s. C and C continued this
and introduced for equality. This has caused
great confusion and much wasted time on the part
of programmers the world over.
Algol 60 used for assignment and for equali
ty. Remember concepts, not syntax, but be care
ful of the syntax!
11Constants
- If data doesnt change throughout program, use
constant
- Helpful to name this valuefinal int
CS100_STUDENTS 78final double PI 3.14159
- Use uppercase to distinguish from variables,
whose values do change throughout the program
12Naming Conventions
- Short variable names make programs shorter, and
more manageable
- Long names can convey more meaning
- Name can almost never give full meaning, so
comment when the variable is declared.
- Avoid names such as thisIsTheVariableThatStores
TheNumberOfBooksInMyLibrary
13Conditional Statement -- If
- Conditional statements allows a choice of a
command based on some condition// Store the
maximum of x and y in zz xif (y x) z y
- Syntax if (boolean expression) statement
14Block statements in if statements
- Suppose you would like to execute more than one
thing based on the condition? // if x ! y,
store 0 in x, store y in zif (y ! x) x 0
z y OR// if x ! y, store 0 in x, store y
in zif (y ! x) x 0 z y
15More on block statements
- Consider
.
- and are used to delimit a sequence of
statements that are to act together, just as (
and ) are used in arithmetic expressions
- Many options for positioning and , just be
consistent
16Second Form of if statement
- Provide multiple options // Store maximum of x
and y in zif (x y) z x else z
y
17Example of Nested Ifs
- if (coin HEADS)
- if (choice RECEIVE)
- System.out.println(You won, will receive)
- else
- System.out.println(You won, will kickoff)
- else
- System.out.println(You lost.)
18Operator Syntax
- equal to
- ! not equal to
- greater than
- greater than or equal to
19Operator Precedence
- / then - then
- 14 8 / 2 is therefore 18 (not 11)
- Use parentheses to make things clear
- (14 8) / 2 11
- Must be an equal number of left and right parens
20Input (briefly)
- Classes that provide facilities for input and
export are available in package java.io
- Place the phrase import java.io.at the top of
your Java source file
- Ok if you dont understand all of this yet
- Read Section 3.3 in text for details
21Characters and Strings (briefly)
- char date type
- ASCII plus stuff UNICODE
- A character literal uses single quotes b
- char firstChar b
- String is not a primitive type, its a Class
- Strings represented as objects of the String
class
- String manipulation can get complicated -- see
text for more details
22Getting Input from user
- Import java.io.
- // Read a string from the user
- String message
- message stdin.readLine()
- // Read numeric input from the user
- String string1 int num1
- string1 stdin.readLine()
- num1 Integer.parseInt(string1)
23Example
- system.out.println(Whats your name?)
- name stdin.readLine()
- system.out.println(Your name is name)
- What is your name?
- Milly Lunor
- Your name is Milly Lunor
24Small Formatting Tricks
- Java uses the \ in output statements to indicate
formatting
- \t tab, \n newline, \ double quote, \ single
quote, \\ backslash
- Name\tDOB results in
- Name DOB
- foo\n\nbar results in
- foo
- bar
25Discussion Issues
- Java is strongly-typed. What good is that?
- Why use constants?
- Why would you use (a 0) vs. (a 1)
- Is Y2K a compile-time, run-time or logical error?
- If a system fails, who is responsible?