Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 111 Introduction to Computation September 15, 2009.

Similar presentations


Presentation on theme: "CPSC 111 Introduction to Computation September 15, 2009."— Presentation transcript:

1 CPSC 111 Introduction to Computation September 15, 2009

2 Administrative stuff Have you done Lab 0? Ready for Lab 1? Remember, attitudinal survey to be done by Friday October 18 to get credit! Readings Ch. 1 Ch 2.1-2.10 Ch 4.1

3 Review What is Computer Science Why it might be frustrating Strategies for success

4 From the previous lecture Computer 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

5 From the previous lecture Telling 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.

6 From the previous lecture What 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

7 How to avoid frustration Study 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.

8 Don’t wait until the last minute to get help

9 Bad things happen while learning a new skill. Start homework early; give yourself time for mistakes.

10 Don’t be too ambitious with your course load. You can’t slack off in this class, even for a few days.

11 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

12 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 computers work.

13 Computer hardware overview

14 Input Devices Storage Devices (Mass Memory) Output Devices Memory (Primary/Random Access - RAM)

15 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

16 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

17 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

18 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

19 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

20 Computer hardware overview

21 Memory Some of computer programming is resource management. As beginning programmers, the resource that you’ll be concerned with most is memory. But don’t worry…most programming languages do a lot of the work for you.

22 Memory Memory is divided into many memory locations (or cells), used to store programs and data 9278 9279 9280 9281 9282 9283 9284 9285 9286 Each memory cell has a numeric address, which uniquely identifies it

23 Storing Information 9278 9279 9280 9281 9282 9283 9284 9285 9286 Large values are stored in consecutive memory locations10011010 Each 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

24 Memory A memory (RAM) module made of memory chips

25 2 40 = 1024 4 TBterabyte 2 30 = 1024 3 GBgigabyte 2 20 = 1024 2 MBmegabyte 2 10 = 1024KBkilobyte 2 0 = 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

26 Memory Macintosh SE in 1987 1 megabyte (Mb) of memory MacBook Pro in 2007 2 gigabytes (Gb) of memory 1000 times more memory capacity in 20 years 1000 times greater processing speed Approximately the same price

27 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

28 CDs/DVDs: the surface consists of smooth areas and pits representing 0’s and 1’s respectively. Also goes well with binary system! Mass storage/long-term memory

29 More on the CPU It continuously follows the fetch-decode-execute cycle: fetch decode execute 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

30 30 The Central Processing Unit (SKIP) The speed of a CPU is controlled by the system clock The system clock generates an electronic pulse at regular intervals The pulses coordinate the activities of the CPU The speed is usually measured in gigahertz (GHz)

31 31 What can be represented by a byte? (SKIP) A single bit can represent two possible states, like a light bulb that is either on (1) or off (0) Permutations of bits are used to store values, each permutation can represent a particular item How many unique items can be represented with N bits?

32 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 for a computer.

33 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

34 Programs and programming languages Here’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.

35 Machine languages The first programming languages were machine languages - the most primitive kind. Here’s a sample machine language instruction: 00000000001000100011000000100000 What do you suppose it means?

36 Machine languages The first programming languages were machine languages - the most primitive kind. Here’s a sample machine language instruction: 00000000001000100011000000100000 add 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.

37 Machine languages The first programming languages were machine languages - the most primitive kind. Here’s a sample machine language instruction: 00000000001000100011000000100000 add 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

38 Assembly Languages These languages were called assembly languages. They were just direct mappings of machine language instructions onto helpful mnemonics and abbreviations. Here’s a sample assembly language instruction that corresponds to our machine language instruction: add r1,r2,r6 00000000001000100011000000100000 add what’s to what’s and put it unimportant details for us in this in this in this register register register

39 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 00000000001000100011000000100000 add what’s to what’s and put it unimportant details for us in this in this in this register register register assembler assembly language machine language

40 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)

41 High-Level Languages This led to the development of what are called high-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 people to use, though they’re still a long way from English. A single high-level instruction gets more work done than a machine or assembly language instruction. Most high-level languages can be used on different computers. High-level language for this course is Java: modern, highly used, portable, safe

42 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 C 3)add those two values together 4)store that result in memory in a location called A

43 Compiler A program written in a high-level language is converted to machine language instructions by another program called a compiler (well, not always). The high-level instruction: A = B + C becomes at least four machine language instructions! 00010000001000000000000000000010 load B 00010000010000000000000000000011 load C 00000000001000100011000000100000 add them 00010100110000000000000000000001 store in A compiler high-level language machine language

44 Your high-level language is Java (can skip this and next if behind) Java was developed by Sun Microsystems in the early 1990s. It was intended as a computer-independent (or “platform independent”) programming language for set-top boxes in cable TV networks, but Sun decided not to go into the set-top box business. Then the World Wide Web became the next big thing, and Sun, already being heavily involved in providing networked computer systems, saw an opportunity...

45 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 computer platforms...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.

46 The Programming Language in this Course is Java Your Program.java (Java) Your Program.class (Java Bytecodes) Windows PCMacintoshSPARC Server java JVM on Unix java JVM on Windows java JVM on MacOS javac Compiler

47 To Summarize Program development Use an editor to create your Java program (often called source 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.

48 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

49 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!"); }

50 Let’s see how it works In 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 Eclipse in most labs and homeworks.

51 A sample Java application program Comments 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!"); }

52 A sample Java application program Comments 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!"); }

53 A sample Java application program Comments 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!"); }

54 Let’s see how it works We use in class a Java development environment called Dr. Java (www.drjava.org). And you'll work with an environment called Eclipse in some labs.

55 A sample Java application program This 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!"); }

56 A sample Java application program The 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.

57 A sample Java application program Note: 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!"); }

58 A sample Java application program public class Oreo { public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); } The words we use when writing programs are called identifiers (except those inside the quotes).

59 A sample Java application program public class Oreo { public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); } Kurt created this identifier.

60 A sample Java application program public class Oreo { public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); } Other programmers have contributed a printing program which is part of a huge library of useful programs that comes with Java - they chose these identifiers.

61 A sample Java application program public 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.

62 Reserved words You should get familiar with these, but you don’t need to memorize all 52 for an exam abstract do if private throw boolean double implements protected throws break else import public transient byte enum instanceof return true case extends int short try catch false interface static void char final long strictfp volatile class finally native super while const float new switch continue for null synchronized default goto package this The words in yellow are words you'll use frequently in the next few weeks

63 63 Java Identifiers An identifier must start with a letter and be followed by zero or more letters and/or digits. 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 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 digit

65 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?)


Download ppt "CPSC 111 Introduction to Computation September 15, 2009."

Similar presentations


Ads by Google