CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n...

23
CS 100 Lecture 1 1 Welcome to CS 100 Welcome to CS 100 Goal: Learn to write intelligible, Goal: Learn to write intelligible, correct computer programs correct computer programs Language: Java (but the language Language: Java (but the language should not be the primary focus) should not be the primary focus) Software: CodeWarrior Software: CodeWarrior Plan: Compressed, intense version Plan: Compressed, intense version of a full-semester course. Be sure of a full-semester course. Be sure to keep up! to keep up!

Transcript of CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n...

Page 1: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 1

Welcome to CS 100Welcome to CS 100

Goal: Learn to write intelligible, Goal: Learn to write intelligible, correct computer programscorrect computer programs

Language: Java (but the language Language: Java (but the language should not be the primary focus)should not be the primary focus)

Software: CodeWarriorSoftware: CodeWarrior Plan: Compressed, intense version Plan: Compressed, intense version

of a full-semester course. Be sure to of a full-semester course. Be sure to keep up!keep up!

Page 2: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 2

Course StaffCourse Staff

Instructor: Lynette Millett, millett@csInstructor: Lynette Millett, millett@cs TAs:TAs:

– Wei Tsang Ooi, [email protected]– Alan Renaud, [email protected]– David Welte, [email protected]

Consultants:Consultants:– Rocky Chen, [email protected], Young

Ho Cho, [email protected], Amanda Waack, [email protected]

Page 3: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 3

LogisticsLogistics

Lectures every day (Mon-Fri)Lectures every day (Mon-Fri) Roughly two assignments per weekRoughly two assignments per week

– NO LATE ASSIGNMENTS NO LATE ASSIGNMENTS ACCEPTEDACCEPTED

0 0 or moreor more short quizzes per week short quizzes per week Partners: 1 partner allowed where Partners: 1 partner allowed where

specifiedspecified Highest level of academic integrity Highest level of academic integrity

expectedexpected

Page 4: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 4

Logistics continuedLogistics continued

Office hours: as noted on webOffice hours: as noted on web One prelim: Monday, July 19, 1999One prelim: Monday, July 19, 1999 Final Grades:Final Grades:

– 50% assignments50% assignments– 25% prelim25% prelim– 25% final25% final

First Assignment due this First Assignment due this THURSDAYTHURSDAY

Page 5: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 5

When Turning in When Turning in Assignments:Assignments:

No late assignments acceptedNo late assignments accepted Your name and Cornell ID# should be on Your name and Cornell ID# should be on

frontpage (in comments, if a program)frontpage (in comments, if a program) For programming assignments turn in For programming assignments turn in

printout of source code, sample output and printout of source code, sample output and diskdisk

For written assignments: please type. TAs For written assignments: please type. TAs reserve right not to grade illegible reserve right not to grade illegible homework.homework.

Page 6: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 6

Main Concepts TodayMain Concepts Today

What is an algorithm? What is an algorithm? Method and method callMethod and method call Reading this week: Chapter 1 Reading this week: Chapter 1

(skim), Chapters 2 and 3(skim), Chapters 2 and 3 HandoutsHandouts

Page 7: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 7

AlgorithmAlgorithm

A set of instructions for performing A set of instructions for performing some task that is:some task that is:– preciseprecise– unambiguousunambiguous– effectiveeffective– abstractabstract

In other words: well-specifiedIn other words: well-specified

Page 8: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 8

Examples of AlgorithmsExamples of Algorithms

A recipe for chocolate mousseA recipe for chocolate mousse Given this list of integers, tell me Given this list of integers, tell me

which ones are evenwhich ones are even Play a game of tic-tac-toe in a way Play a game of tic-tac-toe in a way

that you never losethat you never lose Find your Star Wars nameFind your Star Wars name

Page 9: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 9

Star Wars example: Star Wars example: First SW name: First SW name:

Write first 3 letters Write first 3 letters of your last name of your last name then attach first 2 then attach first 2 letters of your first letters of your first namename

Last SW name: Last SW name: Write first 2 letters Write first 2 letters of mother’s maiden of mother’s maiden name and attact name and attact first 3 letters of first 3 letters of hometownhometown

Lynette Millett born in Norway, ME to I. (Rogers) Millett becomes:

Mil+Ly = MillyRo+Nor = Ronor

Page 10: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 10

Ambiguous AlgorithmAmbiguous Algorithm

Take two pieces of breadTake two pieces of bread Put peanut butter on one side of Put peanut butter on one side of

one pieceone piece Put jelly on one side of the other Put jelly on one side of the other

piecepiece Put the pieces togetherPut the pieces together Output of this algorithm is. . . .Output of this algorithm is. . . .

Page 11: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 11

A Messy SandwichA Messy Sandwich Why? Why? The algorithm did not specify that the The algorithm did not specify that the

pieces of bread should be put together pieces of bread should be put together so that the peanut butter and jelly are so that the peanut butter and jelly are on the insideon the inside

Computers do Computers do exactlyexactly what they’re what they’re told or, worse, something told or, worse, something undefinedundefined

Precision is importantPrecision is important Any other ambiguities in that example?Any other ambiguities in that example?

Page 12: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 12

ProgramsPrograms

A program is an algorithm written in A program is an algorithm written in some language: English, C++, Java, . .some language: English, C++, Java, . .

““Computer program” is a program in a Computer program” is a program in a computer language such as Java computer language such as Java

Abstraction vs. RepresentationAbstraction vs. Representation– Algorithm -- ProgramAlgorithm -- Program– Number -- NumeralNumber -- Numeral– Object having “threeness” -- 3, III, three, |||Object having “threeness” -- 3, III, three, |||

Page 13: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 13

How does it work?How does it work? Algorithm given as sequence of Algorithm given as sequence of

instructions to execute in orderinstructions to execute in order In Java, each instruction is called a In Java, each instruction is called a

statementstatement Common statement in Java is a Common statement in Java is a

method call or method invocationmethod call or method invocation But, what’s a method? But, what’s a method? ““Method”, “procedure”, “function” Method”, “procedure”, “function”

often used interchangeablyoften used interchangeably

Page 14: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 14

Sample Method HeadingSample Method Heading// Draw a line from point (x1, y1) to point //(x2, y2)public void drawLine (int x1, int y1, int x2, int y2)

– Comment: explains the method precisely in Comment: explains the method precisely in English. Always begin with “//”English. Always begin with “//”

– Prefix modifiers public, void (will explain Prefix modifiers public, void (will explain later)later)

– Method name: drawLineMethod name: drawLine– Parameters: x1, y1, x2, y2, and their types: Parameters: x1, y1, x2, y2, and their types:

intint

Page 15: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 15

Method CallMethod Call

Call: Call: drawLine(3, 5, 1, 1);drawLine(3, 5, 1, 1); Command to be executed: Draw a Command to be executed: Draw a

line from point (3,5) to point (1,1)line from point (3,5) to point (1,1)

// Draw a line from point (x1, y1) to point //(x2, y2)public void drawLine (int x1, int y1, int x2, int y2)

Page 16: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 16

Another ExampleAnother Example

Call: Call: printMax(7, 83, 20*5-52);printMax(7, 83, 20*5-52); Command to be executed: print the Command to be executed: print the

largest of 7, 83 and 20*5-52largest of 7, 83 and 20*5-52 Note: the method name “printMax” Note: the method name “printMax”

has no meaning to Java. It is just a has no meaning to Java. It is just a string of characters. We could have string of characters. We could have called it aaa, pm, mymethod, etc. . .called it aaa, pm, mymethod, etc. . .

// Print the largest of x, y and zpublic void printMax (int x, int y, int z)

Page 17: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 17

Yet Another ExampleYet Another Example

Call: Call: printHello();printHello(); Command to be executed: In the Command to be executed: In the

output window, skip a line and output window, skip a line and print “Hello!”print “Hello!”

// In the output window, skip a line and print // “Hello!”public void printHello()

Page 18: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 18

Points to RememberPoints to Remember

A method can have zero parametersA method can have zero parameters Syntax of a method call is: Syntax of a method call is:

namename((list of 0 or more arguments list of 0 or more arguments separated by commasseparated by commas));;

Page 19: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 19

What does the Method do? What does the Method do?

To find out what a method does:To find out what a method does:– Make a copy of the specification (the Make a copy of the specification (the

comment in the heading)comment in the heading)– replace all parameters by the replace all parameters by the

appropriate arguments in the callappropriate arguments in the call This assumes the programmer This assumes the programmer

wrote good commentswrote good comments

Page 20: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 20

Understanding MethodsUnderstanding Methods

The way we have understood these The way we have understood these methods relies on comments that aremethods relies on comments that are– preciseprecise– understandableunderstandable– correctcorrect

Your programs will be judged partially Your programs will be judged partially on how well your documentation ison how well your documentation is

Page 21: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 21

ReadingReading

From now on, consult syllabus and/or From now on, consult syllabus and/or webpage. We will try to point out webpage. We will try to point out appropriate readings from the text, appropriate readings from the text, but you should use the text as a but you should use the text as a reference when solving homework reference when solving homework problems.problems.

URL: URL: http://www.cs.cornell.edu/cs100-su99http://www.cs.cornell.edu/cs100-su99

Page 22: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 22

Assignment W1Assignment W1

Series of questions requiring Series of questions requiring written answerswritten answers

Should be fairly straightforward if Should be fairly straightforward if you’ve done the readingyou’ve done the reading

Due Thursday July 1 at the Due Thursday July 1 at the beginning of classbeginning of class

Page 23: CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.

CS 100 Lecture 1 23

Registration FormRegistration Form

Please fill out and turn in before Please fill out and turn in before you leave class today. you leave class today.