cpsc 111 introduction to computation september 15, 2009
TRANSCRIPT
CPSC 111Introduction to Computation
September 15, 2009
Administrative stuff
Have you done Lab 0?
Ready for Lab 1?
Remember, attitudinal survey to be done by Friday October 18 to get credit!
ReadingsCh. 1Ch 2.1-2.10Ch 4.1
Review
What is Computer Science
Why it might be frustrating
Strategies for success
From the previous lectureComputer science is about processes
A process is a procedure in action
A procedure is a collection of instructions intended to result in useful behaviour
A program is a procedure that’s written in a programming language
An algorithm is a fancy word for procedure
From the previous lectureTelling someone how to do something as simple as making fried eggs may lead to unexpected results if we're not real precise with our instructions.
Writing computer programs can be even more challenging because it requires us to communicate with a degree of detail and precision that’s way beyond our everyday experience.
This can give rise to frustration in new students.
From the previous lectureWhat will you learn here?
How to get a computer to do your bidding:
• How to represent solutions to problems as procedures or algorithms
• How to represent those procedures as programs written in a programming language
• How to get the computer to turn your programs into processes that do useful stuff
How to avoid frustrationStudy your book frequently (don’t just read it).
Play with the programs from your book and from class.
Go to the labs.
Go to the tutorials.
Ask questions.
Practice, practice, practice.
Don’t wait until the last minute to get help
Bad things happen while learning a new skill. Start homework early; give yourself time for mistakes.
Don’t be too ambitious with your course load. You can’t slack off in this class, even for a few days.
Today’s Lecture
Brief overview of computer hardware
Machine/assembly languages, high-level programming languages and how to go from one to another
Introduction to Java - How to write, run and compile a simple Java program
When we say
“Computer science is the study of what computers do, not of what they are.”
Does it mean that we’re not going to talk about the computer and how it works?
No, we need to know something about this too, but it won’t be the main emphasis of this course.
So we'll now take a quick look at how computerswork.
Computer hardware overview
CPU (Central Processing Unit)Consists of one or few electronic components (chips)
Each chip contains a huge number of structural elements (transistors). These enable the complex network of electrical signals that make computing possible
> 50 millions of transistors in a Pentium 4
CPU (Central Processing Unit)
Locates program instructions in memory and executes them
Carries our arithmetic operations
Exchanges data with storage devices and input/output devices
Digital Information• Computers store all information digitally:
– numbers– text– graphics and images– video– audio– program instructions
• In some way, all information is digitized - broken down into pieces and represented as numbers
Representing Text Digitally• For example, every character is stored as a
number, including spaces, digits, and punctuation
• Corresponding upper and lower case letters are separate characters
H i , H e a t h e r .
72 105 44 32 72 101 97 116 104 101 114 46
Binary Numbers
• Digitized information it is represented and stored in memory using the binary number system (base 2 system)
• Binary number system: each digit is either 0 or 1.
• A single binary digit (0 or 1) is called a bit (more on this later)
• This simple binary system goes hand-in-hand with the digital hardware devices seen earlier
Computer hardware overview
Memory
Memory is divided into many memory locations (or cells), used to store programs and data
927892799280928192829283928492859286
Each memory cell has a numeric address, which uniquely identifies it
Storing Information
927892799280928192829283928492859286
Large values arestored in consecutivememory locations
10011010Each memory cell stores a set number of bits (usually 8 bits, or one byte)
each bit can be thought of as an electronic switch that is either off or on representing a 0 or a 1.
. When data is stored in a memory location, the data that was previously stored there is overwritten and destroyed
Memory
A memory (RAM) module made of memory chips
240 = 10244TBterabyte
230 = 10243GBgigabyte
220 = 10242MBmegabyte
210 = 1024KBkilobyte
20 = 1byte (8 bits)
# of bytesSymbolUnit
Units of memory storage• Every memory device has a storage capacity, indicating the
number of bytes it can hold
• Capacities are expressed in various units
Memory
Macintosh SE in 19871 megabyte (Mb) of memory
MacBook Pro in 20072 gigabytes (Gb) of memory
1000 times more memory capacity in 20 years1000 times greater processing speedApproximately the same price
Mass storage/long-term memory
A hard disk drive without its protective case
Coated with magnetic material can be polarized to one of two extremes to represent a 0 or a 1 (goes well with binary system!)
Read/write heads that can detect and change polarization patterns on the platters
CDs/DVDs: the surface consists of smooth areas and pits representing 0’sand 1’s respectively.
Also goes well with binary system!
Mass storage/long-term memory
More on the CPU• It continuously follows the fetch-decode-execute cycle:
fetch
decodeexecute
• Program Counter: special memory location holding the memory address of the next instruction to be executed
• Other special memory locations, called registers, are used by the CPU to store the current instruction and necessary data for processing
What can be represented by a byte?256 different characters from your keyboard(Java actually uses 2 bytes to represent a character...how many characters is that?)
256 different shades of gray in a b&w image
256 colors or shades of color in a color image
256 frequencies or tones to be played through a speaker
256 of anything that can be represented as discrete entities.
A byte can also represent a part of an instruction fora computer.
Today’s Lecture
Brief overview of computer hardware
Machine/assembly languages, high-level programming languages and how to go from one to another
Introduction to Java - How to write, run and compile a simple Java program
Programs and programming languagesHere’s where we begin to look at the art and science of computer programming.
You are going to encounter lots of new terminology and some new ideas and concepts that will make more sense as you gain programming experience.
Remember: practice, practice, practice.
Don’t panic.
Machine languagesThe first programming languages were machine languages - the most primitive kind.
Here’s a sample machine language instruction:
00000000001000100011000000100000
What do you suppose it means?
Machine languagesThe first programming languages were machine languages - the most primitive kind.
Here’s a sample machine language instruction:
00000000001000100011000000100000add what’s to what’s and put it unimportant details for us in this in this in this register register register
Remember what’s a register? It’s one of very few special purpose memory locations inside your computer's CPU where the “real” computation can be done.
Machine languagesThe first programming languages were machine languages - the most primitive kind.
Here’s a sample machine language instruction:
00000000001000100011000000100000add what’s to what’s and put it unimportant details for us in this in this in this register register register
It is quite difficult to program this way
So people created languages that were a bit more readable
Assembly Languages
These languages were called assembly languages.They were just direct mappings of machine languageinstructions onto helpful mnemonics and abbreviations.
Here’s a sample assembly language instruction that corresponds to our machine language instruction:
add r1,r2,r6
00000000001000100011000000100000add what’s to what’s and put it unimportant details for us in this in this in this register register register
Assembler
A program written in assembly language is converted into a corresponding set of machine language instructions by another program called an assembler.
add r1,r2,r6
00000000001000100011000000100000add what’s to what’s and put it unimportant details for us in this in this in this register register register
assemblerassembly language machine language
Machine/Assembly Languages
Both machine and assembly languages pose big challenges for programmers:
• they’re difficult to read and write
• they’re difficult to remember
• each instruction does very little, so it takes lots of instructions just to get something simple done
• every machine or assembly language was good for only one type of computer - learning to program an IBM computer wasn’t the same as learning to program a Honeywell or Burroughs computer (ask your parents, or your grandparents)
High-Level LanguagesThis led to the development of what are calledhigh-level languages. You may have heard the names of some of them: Fortran, COBOL, Lisp, C++, C#, Perl, Python
High-level languages are intended to be easier for peopleto use, though they’re still a long way from English.
A single high-level instruction gets more work done thana machine or assembly language instruction.
Most high-level languages can be used on differentcomputers.
High-level language for this course is Java: modern, highly used, portable, safe
High-Level Languages
Here’s an example of a high-level instruction:
A = B + C
This tells the computer to 1) go to main memory and find a value stored in a location called B 2) go to main memory and find a value stored in a location
called C3) add those two values together4) store that result in memory in a location called A
Compiler
A program written in a high-level language is convertedto machine language instructions by another program called a compiler (well, not always).
The high-level instruction: A = B + Cbecomes at least four machine language instructions!
00010000001000000000000000000010 load B
00010000010000000000000000000011 load C
00000000001000100011000000100000 add them
00010100110000000000000000000001 store in A
compilerhigh-level language machine language
Your high-level language is Java“Hmmm, we have a language that’s been designed to be used on different computer platforms in big networks...the World Wide Web is a big network of lots of different computerplatforms...let’s make Java the programming language of the Internet!”
And for some good reasons that we can talk about later if you want to know, that’s exactly what happened.
The Programming Language in this Course is Java
Your Program.java(Java)
Your Program.class(Java Bytecodes)
Windows PC Macintosh SPARC Server
javaJVM on Unix
javaJVM on Windows
javaJVM on MacOS
javac Compiler
To Summarize
Program development
Use an editor to create your Java program (often calledsource code -- “code” is used interchangeably with“program” or “instructions” in the computer world).
Another program, e.g., a compiler, translates the source code into the target language or object code, which is often machine language.
Finally, your computer can execute the object code.
Today’s Lecture
Brief overview of computer hardware
Machine/assembly languages, high-level programming languages and how to go from one to another
Introduction to Java - How to write, run and compile a simple Java program
A sample Java application program
//*******************************************************// Oreo.java Author: Kurt Eiselt// // Demonstrating simple Java programming concepts while// revealing one of Kurt's many weaknesses//*******************************************************
public class Oreo{ //***************************************************** // demand Oreos //***************************************************** public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); }}
Let’s see how it worksIn class, Dr Conati will use a Java development environment called Dr. Java (www.drjava.org).
Today, I will use a basic editor (emacs), and directly call the javac compiler and the java runtime environment
And you'll work with an environment called Eclipsein most labs and homeworks.
A sample Java application programComments are ignored by the Java compiler
//*******************************************************// Oreo.java Author: Kurt Eiselt// // Demonstrating simple Java programming concepts while// revealing one of Kurt's many weaknesses//*******************************************************
public class Oreo{ //***************************************************** // demand Oreos //***************************************************** public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); }}
A sample Java application programComments could also look like this
/* Oreo.java Author: Kurt Eiselt Demonstrating simple Java programming concepts while revealing one of Kurt's many weaknesses*/
public class Oreo{ /* demand Oreos */ public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); }}
A sample Java application programComments are important to people, but not to the compiler. Here’s what the compiler sees:
public class Oreo{ public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); }}
A sample Java application programThis whole thing is the definition of a class.
A class is a package of instructions that specify what kinds of data will be operated on and what kinds of operations there will be.
All the Java programs you write will consist of one or more classes. More about that later.
public class Oreo{ public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); }}
A sample Java application programThe inner part is the definition of a method.
A method is a group of Java statements (instructions) that has a name and performs some task.
Here the name is “main”.
public class Oreo{ public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); }}
All the Java programs you create will have a main method. It’s where the execution of the program begins.
A sample Java application programNote: These definitions of class and method are incomplete at best, but they'll suffice for now. We'll expand on these definitions in the days to come.
public class Oreo{ public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); }}
A sample Java application programpublic class Oreo{ public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); }}
The words we use when writing programs are calledidentifiers (except those inside the quotes).
A sample Java application programpublic class Oreo{ public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); }}
Kurt created this identifier.
A sample Java application programpublic class Oreo{ public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); }}
Other programmers have contributed a printing programwhich is part of a huge library of useful programs thatcomes with Java - they chose these identifiers.
A sample Java application programpublic class Oreo{ public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); }}
These are special identifiers in the Java language called reserved words - don’t use them in other ways.
Reserved wordsYou should get familiar with these, but you don’t need to memorize all 52 for an exam
abstract do if private throwboolean double implements protected throwsbreak else import public transientbyte enum instanceof return truecase extends int short trycatch false interface static voidchar final long strictfp volatileclass finally native super whileconst float new switchcontinue for null synchronizeddefault goto package this
The words in yellow are words you'll use frequently in the next few weeks
63
Java Identifiers• An identifier must start with a letter and be followed by zero
or more letters and/or digits.
Java letter
Java letter
Java digit
• Java letter: an alphabetic character (upper or lower case), a dollar symbol ($) or an underscore (_).
• Java digit: one of the digits 0 through 9.
64
Identifiers (cont’d)
• Which of the following identifiers are NOT valid?
1. userName 2. user_name 3. $cash 4. 2ndName 5. first name 6. user.age 7. _note_ 8. note2
Java letter
Java letter
Java digit
Identifiers
Java is case sensitive.
Oreo oreo OREO 0reo
are all different identifiers, so be careful. This is a common source of errors in programming.
(Note that the last one isn’t a valid identifier. Why?)