CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W...

25
CompSci 100e 1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes http://www.cs.duke.edu/courses /fall10/ cps100e http://www.cs.duke.edu/forbes

Transcript of CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W...

Page 1: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.1

Welcome!

Program Design and Analysis II for Engineers

CompSci 100EFrench 2231

M, W 2:50-4:05

Professor Jeff Forbes

http://www.cs.duke.edu/courses/fall10/cps100e

http://www.cs.duke.edu/forbes

Page 2: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.2

Newsweek Most Important People 2010

1. Hamid Karzai and Stanley McChrystal

2. Steven Chu and Lisa Jackson3. Vladimir Putin4. Steve Jobs5. David Cameron6. Daphne Koller7. Republican Governors8. Brian Roberts9. Catherine Ashton10. Sue Naegle

http://www.youtube.com/watch?v=DX2_9ntOcQI

Page 3: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.3

Computer Science in a Nutshell?

Page 4: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.4

Computer Science in a Nutshell?

Page 5: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.5

Computer Science in a Nutshell?

What is Computer Science?

What are some of the subfields of CS?

What does a Computer Scientist do? Lots of scientists and engineers

What does a programmer do?

Page 6: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.6

What is Computer Science?

What is it that distinguishes it from the separate subjects with which it is related? What is the linking thread which gathers these disparate branches into a single discipline? My answer to these questions is simple --- it is the art of programming a computer. It is the art of designing efficient and elegant methods of getting a computer to solve problems, theoretical or practical, small or large, simple or complex. C.A.R. (Tony) Hoare

Page 7: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.7

Programming != Computer Science

What is the nature of intelligence? How can one predict the performance of a complex system? What is the nature of human cognition? Does the natural world 'compute'?

It is the interplay between such fundamental challenges and the human condition that makes computer science so interesting. The results from even the most esoteric computer science research programs often have widespread practical impact. Computer security depends upon the innovations in mathematics. Your Google search for a friend depends on state-of-the-art distributed computing systems, algorithms, and artificial intelligence.

http://www.post-gazette.com/pg/pp/04186/341012.stm

Page 8: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.8

Course Overview Active Lectures, Labs, Quizzes, Programs

Labs based on questions given out in previous week• Hands-on practice with programming• Discuss answers, answer new questions• More opportunities for questions to be answered.

Active Lectures based on readings, questions, programs

• In-class questions used to ensure understanding Programs

• Theory and practice of data structures and OO programming

• Fun, practical, tiring, … Exams/Tests

Semester: open book/note* Final: open book/note

Page 9: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.9

What’s this course really about?

Solving problems Understanding problem statements Designing solutions to the problems Implementing the problem solution in a

program Understanding and testing the results Documenting and presenting our results

How will we do it? Written classwork and homework Programming projects Algorithmic Programming Testing

Page 10: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.10

Tradeoffs

Simple, elegant, quick, efficient: what are our goals in programming? Don’t worry about getting it right the first time.

How do we decide what tradeoffs are important? Tension between generality, simplicity, elegance, …

Fast programs, small programs,.

Runtime, space, your time, CPU time…

Time vs. space

Programming, design, algorithmic, data-structural

Page 11: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.11

Visualizing Text Text Cloud aka Tag Cloud?

Number of occurrences/emphasis indicated by size of word

Great visual/statistic: http://chir.ag/phernalia/preztags/

http://www.nytimes.com/gst/mostsearched.html?period=30&format=tagcloud

• What information is stored in the URL of the NYTimes site above?

Page 12: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.12

Problem Solving and Programming

How many words are in a file? What’s a word? What’s a file? How do we solve this: simply, quickly,

…?• What’s the best we can do? Constraints?

How many different/unique words are in a file? How is this related to previous task?

How many words do two files have in common? Spell-checking, stemming, Did you

mean ..? How many codons does DNA have in common?

Page 13: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.13

Duke Contributions

Page 14: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.14

Languages

Machine languages. Natural languages.

High-level programming languages.

[ real newspaper headlines, compiled by Rich Pattis ]

Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do. - Donald Knuth

Kids Make Nutritious Snacks.Red Tape Holds Up New Bridge.Police Squad Helps Dog Bite Victim.Local High School Dropouts Cut in Half.

Pictures

Natural language (English)

Pseudo-code

Specific high-levelprogramming language

Machine Language

More easily

expressed

More precise

© Sedgewick & Wayne

Page 15: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.15

Why Java? Java features.

Widely used. Widely available. Embraces full set of modern abstractions. Variety of automatic checks for mistakes in programs.

Java economy. Mars rover. Cell phones. Blu-ray Disc. Web servers. Medical devices. Supercomputing. …

James Goslinghttp://java.net/jag

© Sedgewick & Wayne

Page 16: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.16

Why Java? Java features.

Widely used. Widely available. Embraces full set of modern abstractions. Variety of automatic checks for mistakes in programs. Buzzword-enabled

“Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language”

Caveat. No perfect language.

Our approach. Minimal subset of Java. Develop general programming skills that are applicable to:

C, C++, C#, Perl, Python, Ruby, Matlab, Fortran, Fortress, …

© Sedgewick & Wayne

Page 17: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.17

A Rich Subset of the Java Language

Primitive Numeric Types

!=

==>=<=

<>--

/

+

%

-

++

*

Integer.parseInt()

Double.parseDouble()

Parsing

Math.max()Math.min()

Math.pow()Math.sqrt()

Math.PIMath.abs()

Math.log()

Math.sin()

Math Library

Math.exp()

Math.cos()System.out.println()

System.out.print()

System.out.printf()

System

for

if

Flow Control

while

else

!

||

true

Boolean

&&

false

;,

(

{

Punctuation

)

}

a[i]

new

a.length

Arrays

matches()charAt()

length()

+

String

compareTo()

""

booleanchar

long

int

Built-In Types

String

double

equals()toString()

main()new

public

class

Objects

private

static

© Sedgewick & Wayne

Page 18: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.18

Programming in Java

Programming in Java. Create the program by typing it into a text

editor, andsave it as HelloWorld.java

HelloWorld.java

/******************************************* * Prints "Hello, World" * Everyone's first Java program. *******************************************/

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); }}

© Sedgewick & Wayne

Page 19: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.19

Programming in JavaProgramming in Java. Create the program by typing it into a text editor, and

save it as HelloWorld.java Compile it using Eclipse or by typing at the command-

line:javac HelloWorld.java

This creates a Java bytecode file named: HelloWorld.class

command-line

% javac HelloWorld.java

(or click the Save button in Eclipse)

© Sedgewick & Wayne

Page 20: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.20

Java Bytecode0000000 312 376 272 276 \0 \0 \0 . \0 035 \n \0 006 \0 017 \t

0000020 \0 020 \0 021 \b \0 022 \n \0 023 \0 024 007 \0 025 007

0000040 \0 026 001 \0 006 < i n i t > 001 \0 003 ( )

0000060 V 001 \0 004 C o d e 001 \0 017 L i n e N

0000100 u m b e r T a b l e 001 \0 004 m a i

0000120 n 001 \0 026 ( [ L j a v a / l a n g

0000140 / S t r i n g ; ) V 001 \0 \n S o u

0000160 r c e F i l e 001 \0 017 H e l l o W

0000200 o r l d . j a v a \f \0 007 \0 \b 007 \0

0000220 027 \f \0 030 \0 031 001 \0 \f H e l l o ,

0000240 W o r l d 007 \0 032 \f \0 033 \0 034 001 \0 \n

0000260 H e l l o W o r l d 001 \0 020 j a v

0000300 a / l a n g / O b j e c t 001 \0 020

0000320 j a v a / l a n g / S y s t e m

0000340 001 \0 003 o u t 001 \0 025 L j a v a / i

0000360 o / P r i n t S t r e a m ; 001 \0

0000400 023 j a v a / i o / P r i n t S t

0000420 r e a m 001 \0 007 p r i n t l n 001 \0

0000440 025 ( L j a v a / l a n g / S t r

0000460 i n g ; ) V \0 ! \0 005 \0 006 \0 \0 \0 \0

0000500 \0 002 \0 001 \0 007 \0 \b \0 001 \0 \t \0 \0 \0 035

0000520 \0 001 \0 001 \0 \0 \0 005 * 267 \0 001 261 \0 \0 \0

0000540 001 \0 \n \0 \0 \0 006 \0 001 \0 \0 \0 \f \0 \t \0

0000560 013 \0 \f \0 001 \0 \t \0 \0 \0 % \0 002 \0 001 \0

0000600 \0 \0 \t 262 \0 002 022 003 266 \0 004 261 \0 \0 \0 001

0000620 \0 \n \0 \0 \0 \n \0 002 \0 \0 \0 017 \0 \b \0 020

0000640 \0 001 \0 \r \0 \0 \0 002 \0 016

0000652

HelloWorld.class

© Sedgewick & Wayne

Page 21: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.21

Problem 1: Text Clouds Text clouds: A simple yet powerful idea

Visualization of most frequently occurring words within some body of text

Color or font size indicates word frequency

What is involved with generating text clouds? Steps? Issues? See SimpleWordCount.java and

SimpleCloudMaker.java

Page 22: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.22

Problem 2: Bad Cryptography

Given a list of numbers (e.g. [1,2,3]) What is the maximum product you can obtain if

you increment one value by one?

Page 23: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.23

Problem 3: Shogi

Shogi, also known as Japanese Chess, is a two-player board game

A silver general can move one square diagonally or one square directly up, giving it five possibilities.

Given a start position (sx, sy), what are the minimal number of steps required to move to (gx, gy)

What values of (sx, sy) and (gx, gy) are easy to deal with?

What cases may be mirrors of each other? Abstraction: What do we need to worry about and

what can we safely ignore?

Page 24: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.24

Problem 4: Data processing

Scan a large (~ 107 bytes) file Print the words together with counts of how often

they occur Need more specification?

How do you do it?

What is we only wanted the top k (say 20) words?

Page 25: CompSci 100e1.1 Welcome! Program Design and Analysis II for Engineers CompSci 100E French 2231 M, W 2:50-4:05 Professor Jeff Forbes .

CompSci 100e 1.25

Possible solutions

1. Use heavy duty data structures (Knuth) Hash tries implementation Randomized placement Lots o’ pointers Several pages

2. UNIX shell script (Doug Mclroy)tr -cs “[:alpha:]” “[\n*]” < FILE | \sort | \uniq -c | \sort -n -r -k 1,1

3. See SimpleWordCount.java• Which is better?

K.I.S.?