CS 136 - Lecture 1
-
Why take CS 136?
-
The team
-
Sample problems
-
Your responsibilities
-
Design vs. Coding from Tomassia
-
Java syntax for Pascal features
-
Sample program
Why take CS 136?
Learn about designing correct and efficient algorithms and data structures
-
How to implement algorithms and data structures in Java.
-
How to evaluate and visualize data structures and algorithms.
-
How to design large programs (in an object-oriented way) so that it
is easy to modify them.
The team
Course web page: http://www.cs.williams.edu/~andrea/cs136/
Sample problems
-
Write a backtracking algorithm to find a path from Williamstown to San
Diego.
-
Find the shortest path from Williamstown to San Diego on interstate system
(and do it efficiently).
-
Schedule final exams so there are no conflicts.
-
Design and implement a scientific calculator.
-
Design and implement a simulator that lets you study traffic flow in a
city or airport.
Your responsibilities
-
Come to lectures.
-
Do the reading in advance.
-
After lectures review notes and study examples carefully until you understand
them.
-
Come to labs well-prepared with good designs. Most important time.
-
Don't remain confused.
-
Come see me during office hours when have questions on lecture material.
-
Come see me or TA's when have questions about program.
-
Head off problems before they become severe.
-
Follow honor code guidelines.
I can present material to you to make it easier for you to learn.
-
I cannot "teach" you.
-
CS is not a spectator sport.
Design vs. Coding
Data Structure and Algorithm Design goals:
- Correctness
- Efficiency
Implementation Goals:
-
Robustness (produce correct output for all inputs - including erroneous
input)
-
Adaptability (can evolve over time with new requirements - Y2K
problem)
-
Reusability (use same code in multiple situations)
Java syntax for Pascal features
-
boolean,
-
char (16 bit),
-
int (32 bit),
-
long (64 bit),
-
float (32 bit),
-
double (64 bit);
-
Distinguish int and long
constants by adding "L" for long - e.g. 126L
-
Distinguish float and double constants by adding "F" for float -
e.g., 3.1415F
-
Strings: "This is a string", + used for concatenation: "This is "+"a string."
-
Results of string + anything is string: "#"+17 is same as "#17"
Brief review of the syntax for "traditional" constructs:
-
assignment, "if", "for", "while", declarations, ==, !=, etc.
-
Declarations not separated from rest of code
-
type goes before variablename
-
can be mixed with statements
-
can be initialized in declarations
-
assignment x := e; => x = e;
-
block begin ... end => {...}
-
"if" statement if => drop "then"
-
"while" statement while => drop "do"
-
"repeat" statement repeat .. until cond => do {...}
while !cond;
-
"for" statement for i := 10 to 20 do => for (int i
= 10; i <= 20; i++)
-
Semicolons are used as line terminator, not separator.
// starts comments which go to end of line
/* multi-line comments
are done like this */
Sample Program
class SillyProgram{
public static void main(String[] args){
// void as return type means this is a procedure
int sum = 0;
for (int count = 1; count <= 10; count++){
sum = sum + count;
}
if (sum > 50){
System.out.println("Sum is big: "+ sum);
// + sum converts sum to string and concatenates
} else {
System.out.println("Sum is little: "+ sum);
}
}
}
We'll see later why "class", "public", "static" are used here.
Read: Chapter 3 of Core Java for more
Java details.