CS211 Computers and Programming Instructor: David Gries, Olin 167 [email protected],...

27
CS211 Computers and Programming http://www.cs.cornell.edu/courses/c s211/2004sp/ Instructor: David Gries, Olin 167 [email protected] , [email protected] Staff coordinator: Stacey Shirk, Upson 403 [email protected] TAs: Each TA leads 1 or 2 recitation sections. Your TA is your main contact for the course; get to know TA well. Consultants: in Upson 304 Office hours: to be announced News Group: Use frequently. Learn about it in section. Education is one of the few things a person is willing to pay for and not get. William Lowe Bryan

Transcript of CS211 Computers and Programming Instructor: David Gries, Olin 167 [email protected],...

Page 1: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

CS211 Computers and Programminghttp://www.cs.cornell.edu/courses/cs211/2004sp/

Instructor: David Gries, Olin 167 [email protected], [email protected]

Staff coordinator: Stacey Shirk, Upson 403 [email protected]

TAs: Each TA leads 1 or 2 recitation sections. Your TA is your main contact for the course; get to know TA well.Consultants: in Upson 304Office hours: to be announcedNews Group: Use frequently. Learn about it in section.

Education is one of the few things a person is willing to pay for and not get. William Lowe Bryan

Page 2: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

CS211 Computers and ProgrammingLectures and sections

Lecture: Tues, Thurs. 10:05AM. Attendance expected.Lecture notes online. Print them before the lecture and bring them to lecture.Readings posted online, with lecture notes.

Sign up for one section. Attendance expected: sections may teach new material.It’s okay to switch sections; no permission needed.But end up sticking with one section.

Page 3: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

CS211 ObjectivesLearn about:

• Concepts in modern programming languages:– recursion, induction– classes, objects, inheritance, interfaces

• Efficiency of programs• Correctness issues; and testing/debugging• Data structures: arrays, lists, stacks, queues, priority

queues,trees, binary search trees, hash tables, graphs• Software engineering. How to design, organize, test,

debug, maintain large programs.• Practice in writing/testing/debugging programs

NOT A COURSE SIMPLY ON JAVA PROGRAMMING

Page 4: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

CS212

• one-credit project course.• one lecture per week.• substantial design and programming.• required for CS majors.• take with or after CS211. Taking 211-212 together advised.

AEW Workshop

• one-credit S/U course.• meets once per week for 2 hours.• grade depends solely on attendance.• students work together in a cooperative learning

environment.

Page 5: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Grades

Following are weights for parts of course. Subject to change:• Assignments: 39%• Exercises: 3%• Quizzes: 4%• Prelim 1: 13%• Prelim 2: 15%• Final exam: 25%• Course evaluation: 1%Generally, grades come out 1/3 A’s, 1/3 B’s, 1/3 C’s, but everyone can get an A if they do A work. Exams are most important factor.

Page 6: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Java bootcamp

You are expected to know what CS100 teaches about Java:• classes, objects, how we draw them.• instance methods & variables, static methods & variables,• subclasses, inheritance, overriding of methods• apparent and real class-types of a variable.

Java bootcamp will go over these things:• Olin 155, 7:30—10:30, Tuesday, 27 January

Page 7: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Assignments• Assignments done in teams of 1 or 2 students• You need not keep the same partner for every semester• See the website for academic integrity statement

Exercises• Pencil and paper things.• Do exercises alone — not partners allowed.

• In lecture. Will tell you precisely what they will cover.• Given to let you know what I think is important and

should be learned immediately.• Everyone is expected to get 100/100 on them.

Quizzes

Page 8: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

About me and my expectations

CS100J last semester, people either liked me or hated me:

I definitely appreciate Gries’s method of developing our knowledge from the foundations. Helped me, as a beginner.

An excellent instructor!

Gries was brilliant.

01 02 03 04 05 avMy effectiveness 19 30 48 56 14 3.1My willing to help 06 16 33 56 44 3.7

Get rid of Gries.

Gries should change his teaching methods.

Worst course I have had.

Page 9: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

The manila folder concept

I think the whole manila folder concept was brilliant. It definitely helped me understand the material better.

The folder analogy was very helpful for understanding all the OO concepts we learned.

The filing cabinet / manila folder concept made it easy to grasp the class / object relationship.

File/folder analogy for class/objects was pretty poor.

Manila folders bear no relation to actual programming. Stupid.

File/folder analogy screws everything up.

Page 10: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Invariant

Analysis of the invariant and its relation to a loop [was most helpful for my learning].Idea of invariant was hard to grasp, but when I did get it, everything came together!

[the use of invariants] inhibited my natural programming ability.

Invariants are evil.

Worst part of course; failed to see their importance.

Page 11: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Order of topics

It was good to be exposed to the fundamental structure of Java early on …

Gries is probably right in teaching main concepts first before getting into loops …

Should be more focused on logic and algorithms [not OO].

Don’t start with OO; write a simple “Hello World” program.

Page 12: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

About Gries.

Teaching computer science since 1966.

Main area of research: programming methodology, the formal development of programs, calculational logic.

Cornell Weiss Fellow, awarded for contributions to undergraduate teaching.

Several international awards for contributions to computer science education.

Currently, 1/2 time Associate Dean for Undergraduate Programs (Engineering College).

I really am interested in teaching and care about you. I am here to serve. I am approachable and am willing to talk with any of you at almost any time, within my schedule constraints.

Page 13: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

About programming

Kline: More than anything, mathematics is method.

That applies to programming even more than mathematics.

Research has developed a formal methodology of program-ming, which, if practiced consciously, can lead to more effective programming and clearer programs, with fewer bugs.

We try to teach you some of that methodology in a practical, informal way. But it requires you to change you thinking habits, your way of thinking about the programming process.

Mark Twain: Nothing needs changing so much as the habits of others.

Page 14: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

About programming

Two ways you can proceed:(1) Hack away, and don’t think about good programming

practices. Just program in your “natural way”. Consequence, spend a lot of time testing/debugging and still end up with a buggy program.

(2) Learn to think in an organized, mathematical, disciplined fashion that leads to short, clear, well-documented programs that rarely have mistakes and that can even help you find solutions, solve problems.

(3) Consequence: YOU are in control of the process.

Student: [the use of invariants] inhibited my natural programming ability.

Reply: “Natural” abilities are rarely the best and are often the worst. They can always be improved.

Page 15: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Development of a loop: The problem

Consider problem of adding two positive integers: 4 7 2

+ 9 6 3 1 = 1 0 1 0 3

b[0..bs-1] = {2, 7, 4} bs = 3 c[0..cs-1] = {1, 3, 6, 9} cs = 4

Problem: write an algorithm to store in array d and int variable ds so that d[0..ds-1] is the sum of the integers in b[0..bs-1] and c[0..cs-1], and d[ds-1] != 0.Assume that all variables are already declared.

Digits of an integer are stored in an array with least significant digit first

Answer:

ds = 5

d[0..ds-1] = {3, 0, 1, 0, 1}

Page 16: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Development of a loop: the invariant

Pre: b[0..bs-1] and c[0..cs-1] are positive integersPost: d[0..ds-1] is the sum of the integers in b, c; d[k-1] != 0Use a loop. Need to know the definition of variables used within the loop (written as a loop invariant)

4 7 2 + 9 6 3 1 = 1 0 3

Invariant: d[0..k-1] is the sum of b[0..k-1] and c[0..k-1], with d[k] being the carry (0 or 1)

these have been calculatedcarry from previous pos.

Here, k = 2

Page 17: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Development of a loop: init

Pre: b[0..bs-1] and c[0..cs-1] are positive integersPost: d[0..ds-1] is the sum of the integers in b, c; d[k-1] != 0

k= 0; d[k]= 0;/* invariant:d[0..k-1] is the sum of b[0..k-1] and c[0..k-1], with

d[k] being the carry (0 or 1) */while ( ? ) {

?}When can the loop stop?How does it make progress toward termination?

4 7 2 + 9 6 3 1= 1 0 3in this case, k = 2

Page 18: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Development of a loop:loop condition, progress toward termination

Pre: b[0..bs-1] and c[0..cs-1] are positive integersPost: d[0..ds-1] is the sum of the integers in b, c; d[k-1] != 0

k= 0; d[k]= 0;/* invariant:d[0..k-1] is the sum of b[0..k-1] and c[0..k-1], with

d[k] being the carry (0 or 1) */while ( k < bs || k < cs) {

k= k + 1;}How to keep invariant true?

4 7 2 + 9 6 3 1= 1 0 3in this case, k = 2

Page 19: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Development of a loop: the repetend

Pre: b[0..bs-1] and c[0..cs-1] are positive integersPost: d[0..ds-1] is the sum of the integers in b, c; d[k-1] != 0

k= 0; d[k]= 0;/* invariant:d[0..k-1] is the sum of b[0..k-1] and c[0..k-1], with

d[k] being the carry (0 or 1) */while ( k < bs || k < cs) { Calculate d[k] and the carry into d[k+1];

k= k + 1;}How do we refine the first statement of the repetend?

4 7 2 + 9 6 3 1= 1 0 3in this case, k = 2

Page 20: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Development of a loop: the repetend

Pre: b[0..bs-1] and c[0..cs-1] are positive integersPost: d[0..ds-1] is the sum of the integers in b, c; d[k-1] != 0

k= 0; d[k]= 0;/* invariant:d[0..k-1] is the sum of b[0..k-1] and c[0..k-1], with

d[k] being the carry (0 or 1) */while ( k < bs || k < cs) { if (k < bs) d[k]= d[k] + b[k]; if (k < cs) d[k]= d[k] + c[k]; d[k+1]= d[k] / 10; d[k]= d[k] % 10; k= k + 1;}Upon termination, is most significant digit 1?

4 7 2 + 9 6 3 1= 1 0 3in this case, k = 2

Page 21: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Final loop

// Pre: b[0..bs-1] and c[0..cs-1] are positive integers// Post: d[0..ds-1] is the sum of the integers in b c; d[k-1] != 0

k= 0; d[k]= 0;/* invariant:d[0..k-1] is the sum of b[0..k-1] and c[0..k-1], with

d[k] being the carry (0 or 1) */while ( k < bs || k < cs) { if (k < bs) d[k]= d[k] + b[k]; if (k < cs) d[k]= d[k] + c[k]; d[k+1]= d[k] / 10; d[k]= d[k] % 10; k= k + 1;}if (d[k] != 0) k= k+1;

4 7 2 + 9 6 3 1= 1 0 1 0 3in this case, k = 4

Page 22: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Final loop without annotation

k= 0; d[k]= 0; while ( k < bs || k < cs) { if (k < bs) d[k]= d[k] + b[k]; if (k < cs) d[k]= d[k] + c[k]; d[k+1]= d[k] / 10; d[k]= d[k] % 10; k= k + 1; } if (d[k] != 0) k= k+1;

Page 23: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

int ds= 0; while (bs != b.length || ds != c.length) { if (bs == b.length && cs != c.length) {

d[ds]= d[ds] + c[cs]; if (d[ds] >= 10) {d[ds]= d[ds]-10; d[ds+1]= 1;} else d[ds+1]= 0; ds= ds+1; cs= cs+1; } if (bs != b.length && cs == c.length) { d[ds]= d[ds] + b[bs]; if (d[ds] >= 10) {d[ds]= d[ds]-10; d[ds+1]= 1;} else d[ds+1]= 0; ds= ds+1; bs= bs+1;

} else { d[ds]= b[bs] + c[cs] + d[ds];

if ( … ) { …} else … }ds= ds + 1;

one of the better student sols.

• d[0] not initialized.

• bs, cs not initialized; should not be changed; not solving stated problem.

• repetend far too complicated.

• at end, increase ds only if d[ds] = 1.

Page 24: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Design of larger programs important, too

Sam Loyd’s 8-puzzle:1 2 3

4 5 6

7 8

Try to get to this final configuration

4 3

7 1 5

8 2 6

4 3

7 1 5

8 2 6

4 3 5

7 1

8 2 6

three different configurations

Page 25: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Questions concerning the 8-puzzle

Suppose we want to write a program to “play” the puzzle.• What operations should program support?• How do we represent configurations?• How do we specify an initial config.?• What algorithm will solve the puzzle?• What kind of GUI makes sense?• Should program work for 3 x 3 or a more general n x n?

4 3

7 1 5

8 2 6

4 1 3

7 5

8 2 6

4 3

7 1 5

8 2 6

E

W

N S

Page 26: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Math behind the 8-puzzle

Each configuration is called a state.

State Y is adjacent to state X if Ycan be reached from X in one move.

State transition diagram shows allstates, with labeled arrows betweenadjacent states.

4 3

7 1 5

8 2 6

4 1 3

7 5

8 2 6

4 3

7 1 5

8 2 6

E

W

N S

Page 27: CS211 Computers and Programming  Instructor: David Gries, Olin 167 gries@cs.cornell.edu, dgries@twcny.rr.comgries@cs.cornell.edu.

Graphs

State transition diagram is an example of a graph.Nodes of graph: the states.Edges of graph: connections between nodes.Edges may be labeled.

Examples of graphs: airline routes,roadmaps.

Path problems:• Is there a path from node X to node Y?• Shortest path?• Is there a cycle (loop) from X to X?

We cover graphs later in the course.

4 3

7 1 5

8 2 6

4 1 3

7 5

8 2 6

N S