WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO...
Transcript of WELCOME TO CIS 1068! - Temple Universityjfiore/2017/spring/1068/handouts/slides/01... · WELCOME TO...
1
WELCOME TO CIS 1068!
2
This is a difficult course!
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?”).
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
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?
6
Just like Legos…
9
A Simple Model of a Computer
10
The 6 Parts
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…
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…………
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)
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.
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
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
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
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
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
20
Basic Java programs
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?
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
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)
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
25
Program execution
n The output is printed to the console.n Some editors pop up the console as another
window.
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");
}
}
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
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.
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:
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’”?
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.
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:
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.
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!"
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…
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…
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:
/ \ // \\ /// \\\
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 "
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.
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
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.
42
That thing called style…
n What is style?q Indentationq Capitalizationq Formatting / spacingq Structured codeq No redundancyq …
n Why is it important?
Reading
n Please read chapter 1n Don’t worry about the graphics supplement