WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO...

43
1 WELCOME TO CIS 1068!

Transcript of WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO...

Page 1: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

1

WELCOME TO CIS 1068!

Page 2: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

2

This is a difficult course!

Page 3: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

3

How to do well in this course

n GO TO CLASSn DO THE READINGn Keep up with the assignments

q The course material is cumulativeq From a former student: “Procrastination will

eventually come around to bite you in the ass!”n If you don’t understand something, ask

questions (especially “WHY?”).

Page 4: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

4

We’re here to help

n Ask questionsn Email us, come by our office hoursn We may be nerdy, but we’re not too scary

Page 5: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

5

What is computer science?

n computers?n science?n programming?n late lonely nights in front of the computer?

ALGORITHMIC THINKING

al·go·rithm:a step-by-step procedure for solving a problem or accomplishing some end especially by a computer

n How does that relate to programming?

Page 6: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

6

Just like Legos…

Page 7: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO
Page 8: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO
Page 9: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

9

A Simple Model of a Computer

Page 10: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

10

The 6 Parts

Page 11: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

Random Access Memory (RAM, or just memory)n Volatile storage

q Each card can store up to a few gigabytes of dataq Storage only lasts while computer has power

n Organization: Matrixq Some number of columns, either 32 or 64q Each cell stores a binary integer (bit): a 1 or a 0q Each row has an address, or row number

Row Bit 1

Bit 2

Bit 3

Bit 4

Bit 5

Bit 6

Bit 7

… Bit 64

1 0 1 0 …2 1 1 0 …3 … … … …456…

Page 12: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

Central Processing Unit (CPU)

n Little chip, big deal: it does all the computingn Think of it as a place where some instructions

are stored, and carried out 1 by 1n Instructions can do basic math, and copy

data and move it around (and that’s pretty much it) Copy 1 to 7 *Start*

Add 4 and 7, store in 8Multiply 1 and 8…………

Page 13: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

Hard Disk (Disk, Hard Drive)

n Permanent (more or less) storageq Like memory, stores binary dataq Unlike memory, it doesn’t forget anything when

power is turned off.n Organization: files, blocks, etc.

q Complicated, we won’t get into itq The important point for us:

It’s slow! (compared to memory)

Page 14: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

Other computer components

n Monitorn Keyboardn Mousen Graphics processorsn Busses n Fann Disk controller n …n All sorts of things, but for this course, we’ll

just concentrate on a few of them.

Page 15: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

kilo 103 1,000mega 106 1,000,000giga 109 1,000,000,000tera 1012 1,000,000,000,000peta 1015 1,000,000,000,000,000exa 1018 1,000,000,000,000,000,000zetta 1021 1,000,000,000,000,000,000,000yotta 1024 1,000,000,000,000,000,000,000,000

“large” units

Page 16: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

milli 10

�3

micro 10

�6

nano 10

�9

pico 10

�12

femto 10

�15

atto 10

�18

zepto 10

�21

yocto 10

�24

units < 1

Page 17: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

some names for large numbers

kilo 10

3thousand

mega 10

6million

giga 10

9billion

tera 10

12trillion

peta 10

15quadrillion

exa 10

18quintillion

zetta 10

21sextillion

yotta 10

24septillion

Page 18: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

powers of 10 powers of 2

kilo 103 1,000 210 1,024mega 106 1,000,000 220 1,048,576giga 109 1,000,000,000 230 1,073,741,824tera 1012 1,000,000,000,000 240 1,099,511,627,776peta 1015 1,000,000,000,000,000 250 1,125,899,906,842,624exa 1018 1,000,000,000,000,000,000 260 1,152,921,504,606,846,976zetta 1021 1,000,000,000,000,000,000,000 270 1,180,591,620,717,411,303,424yotta 1024 1,000,000,000,000,000,000,000,000 280 1,208,925,819,614,629,174,706,176

kilo: 1,000 or 1,024?

usually use:n powers of 2 for storagen powers of 10 for just about everything else

Page 19: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

proposed prefixes for powers of 2

n haven’t exactly taken the world by storm

powers of 10 powers of 2

kilo 103 kibi 210 1,024mega 106 mebi 220 1,048,576giga 109 gibi 230 1,073,741,824tera 1012 tebi 240 1,099,511,627,776peta 1015 pebi 250 1,125,899,906,842,624exa 1018 exbi 260 1,152,921,504,606,846,976zetta 1021 zebi 270 1,180,591,620,717,411,303,424yotta 1024 yobi 280 1,208,925,819,614,629,174,706,176

Page 20: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

20

Basic Java programs

Page 21: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

21

Your first Java program!

n Java is a programming language.

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

}

n What does this code output (print to the user) when you run (execute) it?

Page 22: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

Java code and machine code

Notice: Java instructions don’t look like CPU instructions!

n CPU instructions (aka, machine code)q Written in binaryq Easy and fast for machines to work withq Hard for programmers to work with

n Java codeq Written in Java, a “high-level” (English-like)

programming languageq We use the Java compiler and virtual machine to

translate from Java to machine code

Page 23: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

23

Compiling a program

n Before you run a program, you must compile it.n compiler: Translates a computer program

written in one language (i.e., Java) to another language (i.e., byte code)

Compile (javac) Execute (java)

outputsource code(Hello.java)

byte code(Hello.class)

Page 24: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

The Java Virtual Machine (JVM, or VM)n The Java Virtual Machine executes byte code

q Use the “java” command to execute itq It only understands byte code (“.class” files)

n The VM makes Java a bit different from older programming languages (C, C++)q It’s an extra step; compilers for other languages

directly produce machine codeq It’s slowerq But it allows the same byte code to run on any

machine with a VM

Page 25: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

25

Program execution

n The output is printed to the console.n Some editors pop up the console as another

window.

Page 26: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

26

Another Java program

public class Hello2 {

public static void main(String[] args) {

System.out.println("Hello, world!");

System.out.println();

System.out.println("This program produces");

System.out.println("four lines of output");

}

}

Page 27: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

27

Writing your own Java programs

public class <name> {public static void main(String[] args) {

<statement>;<statement>;…<statement>;}

}

n Every executable Java program consists of a classq that contains a method called main

n that contains the statements (commands) to be executed

Page 28: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

28

Syntax

n syntax: The set of legal structures and commands that can be used.

n Examples:q Every basic statement ends with a semi-colon.q The contents of a class occur between curly

braces.

Page 29: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

29

Syntax Errors

n syntax error: A problem in the structure of a program.

1 public class Hello {2 pooblic static void main(String[] args) {3 System.owt.println("Hello, world!")4 }5 }

2 errors found:File: Hello.java [line: 2]Error: Hello.java:2: <identifier> expectedFile: Hello.java [line: 3]Error: Hello.java:3: ';' expected

compiler output:

Page 30: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

30

Finding syntax errors

n Error messages do not always help us understand what is wrong:

File: Hello.java [line: 2]

Error: Hello.java:2: <identifier> expected

pooblic static void main(String[] args) {

n Why can’t the computer just say “You misspelled ‘public’”?

Page 31: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

31

First lesson in computer science

n Computers are stupid.n Computers can’t read minds.n Computers don’t make mistakes.n If the computer is not doing what you want,

it’s because YOU made a mistake.

Page 32: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

32

More on syntax errors

n Java is case-sensitiveq Hello and hello are not the same

1 Public class Hello {

2 public static void main(String[] args) {

3 System.out.println("Hello, world!");

4 }

5 }

1 error found:File: Hello.java [line: 1]Error: Hello.java:1: class, interface, or enum expected

compiler output:

Page 33: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

33

System.out.println

n System.out.println: A statement to print a line of output to the console.q pronounced “print-linn”

n Two ways to use System.out.println:System.out.println("<message>");q Prints the given message as a line of text to the console.

System.out.println();

q Prints a blank line to the console.

Page 34: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

34

Strings

n string: A sequence of text characters.q Start and end with quotation mark characters

n Examples:

"hello"

"This is a string"

"This, too, is a string. It can be very long!"

Page 35: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

35

Details about strings

n A string may not span across multiple lines."This is not

a legal string."

n A string may not contain a “ character.q The ‘ character is okay."This is not a "legal" string either."

"This is 'okay' though."

n This begs the question…

Page 36: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

36

Escape sequences

n A string can represent certain special characters by preceding them with a backslash \ (this is called an escape sequence).q \t tab characterq \n newline characterq \" quotation mark character

n Example:System.out.println("Hello!\nHow are \"you\"?");

n Output:Hello!How are "you"?

n which brings up another question…

Page 37: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

37

Questions

n What is the output of each of the following println statements?

System.out.println("\ta\tb\tc");System.out.println("\\\\");System.out.println("'");System.out.println("\"\"\"");System.out.println("C:\nin\the downward spiral");

n Write a println statement to produce the following line of output:

/ \ // \\ /// \\\

Page 38: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

38

Questions

n What println statements will generate the following output?

This program prints aquote from the Gettysburg Address.

"Four score and seven years ago,our 'fore fathers' brought forth on this continenta new nation."

n What println statements will generate the following output?

A "quoted" String is'much' better if you learnthe rules of "escape sequences."

Also, "" represents an empty String.Don't forget to use \" instead of " !'' is not the same as "

Page 39: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

39

Comments

n comment: A note written in the source code to make the code easier to understand.q Comments are not executed when your program runs.q Most Java editors show your comments with a special color.

n Comment, general syntax:/* <comment text; may span multiple lines> */or,// <comment text, on one line>

n Examples:/* A comment goes here. *//* It can even span

multiple lines. */// This is a one-line comment.

Page 40: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

40

Comments: Where do you go?

n … at the top of each file (also called a "comment header"), naming the author and explaining what the program does

n … at the start of every method, describing its behavior

n … inside methods, to explain complex pieces of code

Page 41: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

41

Comments: Why?

n Comments provide important documentation.

n Later programs will span hundreds or thousands of lines, split into many classes and methods.

n Comments provide a simple description of what each class, method, etc. is doing.

n When multiple programmers work together, comments help one programmer understand the other's code.

Page 42: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

42

That thing called style…

n What is style?q Indentationq Capitalizationq Formatting / spacingq Structured codeq No redundancyq …

n Why is it important?

Page 43: WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO CIS 1068! 2 This is a difficult course! 3 How to do well in this course nGO TO

Reading

n Please read chapter 1n Don’t worry about the graphics supplement