The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 24, 2005
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 2 …from Last Time Homework 1 due at midnight ♦ Fill in form with personal info ♦ Getting started with jGRASP ♦ Compile and Run test programs ♦ Any problems? ♦ Disable test for new versions
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 3 Today (ch. 1) Parts of the computer ♦ hardware vs. software ♦ CPU and memory Binary numbers What is an algorithm?
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 4 ________- computer components including the CPU, main memory, I/O devices, and secondary storage ________ - the brain of the computer, containing the CU, PC, IR, ALU, and ACC ________ - computer instructions to solve a problem The digits 0 and 1 are called _________ or the shortened term ________ Reading Check-Up hardware CPU program binary digits bits
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 5 An Overview of the History of Computers 1950s: Very large devices available to a select few. 1960s: Large corporations owned computers. 1970s: Computers got smaller and cheaper. 1990s: Computers got cheaper and faster and were found in most homes.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 6 Hardware vs. Software A computer is made up of hardware and software Hardware Software CPU - ex: 2 GHz Pentium IV input/output - keyboard - monitor - network card main memory - ex: 256 MB RAM secondary memory - ex: 20 GB hard drive operating systems - Windows XP - Mac OS X applications - games - Microsoft Word - Internet Explorer
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 7 Hardware Organization motherboard CPU memory hard drive
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 8 Central Processing Unit Control Unit (CU) ♦ "the brain" of the CPU Program Counter (PC) ♦ points to the next instruction to be executed Instruction Register (IR) ♦ holds the currently executing instruction Arithmetic Logic Unit (ALU) ♦ carries out all arithmetic and logical ops Accumulator (ACC) ♦ holds the results of the operations performed by the ALU
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 9 Main Memory Ordered sequence of cells AKA Random Access Memory (RAM) Directly connected to the CPU All programs must be brought into main memory before execution When power is turned off, everything in main memory is lost
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 10 Main Memory With 100 Cells Each memory cell has a numeric address, which uniquely identifies it
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 11 CPU and Main Memory Chip that executes program instructions (processor) Primary storage area for programs and data that are in active use (RAM) All programs must be brought into main memory before execution
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 12 Secondary Storage Provides permanent storage for information Retains information even when power is off Examples of secondary storage: ♦ Hard Disks ♦ Floppy Disks ♦ ZIP Disks ♦ CD-ROMs ♦ Tapes
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 13 Secondary Storage Secondary memory devices provide long-term storage Information is moved between main memory and secondary memory as needed
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 14 Input Devices Definition: devices that feed data and computer programs into computers Examples: ♦ Keyboard ♦ Mouse ♦ Secondary Storage
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 15 Output Devices Definition: devices that the computer uses to display results Examples: ♦ Printer ♦ Monitor ♦ Secondary Storage
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 16 Input/Output Devices I/O devices facilitate user interaction
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 17 Hardware Components
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 18 Opening MS Word Use the mouse to select MS Word The CPU requests the MS Word application MS Word is loaded from the hard drive to main memory The CPU reads instructions from main memory and executes them one at a time MS Word is displayed on your monitor
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 19 Questions ____________- points to the next instruction to be executed ____- a unique location in memory ____________- stores information permanently Instructions executed by the CPU must be first loaded to ________ program counter (PC) address secondary storage main memory
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 20 Software Categories Operating System ♦ controls all machine activities ♦ provides the user interface to the computer ♦ first program to load when a computer is turned on ♦ manages computer resources, such as the CPU, memory, and hard drive ♦ examples: Windows XP, Linux, Mac OS X Application ♦ generic term for any other kind of software ♦ examples: word processors, missile control systems, games
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 21 Operating System (OS) OS monitors overall activity of the computer and provides services Written using programming language Example services: ♦ memory management ♦ input/output ♦ storage management
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 22 Application Programs Written using programming languages Perform a specific task Run by the OS Example programs: ♦ Word Processors ♦ Spreadsheets ♦ Games
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 23 Questions Classify the following pieces of software as operating system or application: 1.Microsoft Windows Microsoft PowerPoint 3.Linux 4.Your COMP 14 programs OS app
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 24 It’s All About Data Software is data ♦ numbers, characters ♦ instructions, programs Hardware stores and processes data ♦ read, write ♦ add, subtract, multiply, divide
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 25 Analog vs. Digital Analog ♦ continuous wave forms ♦ ex: sound, music on an audio tape Digital ♦ the information is broken down into pieces, and each piece is represented separately ♦ is ultimately represented as series of 0s and 1s for low voltage and high voltage ♦ can be copied exactly ♦ ex: music on a compact disc
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 26 H i, H e a t h e r Representing Text Digitally All information in a computer is digitized, broken down and represented as numbers. Corresponding upper and lower case letters are separate characters.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 27 Language of a Computer Machine language: the most basic language of a computer A sequence of 0s and 1s ♦ binary digit, or bit ♦ sequence of 8 bits is called a byte Every computer directly understands its own machine language ♦ why can't Windows programs run on Apple computers?
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 28 1 bit bits bits bits Each additional bit doubles the number of possible permutations Bit Permutations
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie = 2 items 2 2 = 4 items 2 3 = 8 items 2 4 = 16 items 2 5 = 32 items 1 bit ? 2 bits ? 3 bits ? 4 bits ? 5 bits ? How many items can be represented by Bit Permutations Each permutation can represent a particular item There are 2 N permutations of N bits ♦ N bits are needed to represent 2 N unique items
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 30 Binary Numbers N bits to represent 2 N values N bits represent values 0 to 2 N -1 Example: 5 bits ♦ 32 unique values (0-31) ♦ = 0 10 ♦ = = 31
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 31 Converting Decimal to Binary Let’s convert 114 to binary Repeatedly divide by 2 and write down the remainder: 114/2=57remainder 0 57/2=28remainder 1 28/2=14remainder 0 14/2=7remainder 0 7/2=3remainder 1 3/2=1remainder 1 1/2=0remainder 1 Write the remainders in reverse order: = Proof – rewrite and add the equations above: 114=(((((((1*2)+1)*2+1)*2+0)*2+0)*2+1)*2+0)= =1*2 6 +1*2 5 +1*2 4 +0*2 3 +0*2 2 +1*2 1 +0*2 0
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 32 Questions: Binary Numbers What’s the maximum value a 6-bit number can represent? What’s the decimal representation of ? What’s the binary representation of 35? = *1+2 4 *1+2 3 *1+2 2 *0+2 1 *1+2 0 *0= =
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 33 KB2 10 = 1024 MB2 20 (over 1 million) GB2 30 (over 1 billion) TB2 40 (over 1 trillion) UnitSymbolNumber of Bytes kilobyte megabyte gigabyte terabyte Storage Capacity Every memory device has a storage capacity, indicating the number of bytes (8 bits) it can hold Various units:
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 34 Machine Language Early computers programmed in machine language (only 0s and 1s) Assembly languages were developed to make programmer’s job easier Assembler: translates assembly language instructions into machine language
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 35 Assembly and Machine Language
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 36 Programming Languages High-level languages make programming easier Closer to spoken languages Examples: ♦ Basic ♦ FORTRAN ♦ COBOL ♦ C/C++ ♦ Java
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 37 To run a Java program: 1.Java instructions need to be translated into an intermediate language called bytecode. 2.The bytecode is interpreted into a particular machine language. Java and Machine Language
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 38 Compiler Compiler: A program that translates a program written in a high-level language into the equivalent machine language. ♦ (In the case of Java, this machine language is the bytecode.) Java Virtual Machine (JVM): A hypothetical computer developed to make Java programs machine independent.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 39 A Java Program public class ASimpleJavaProgram { public static void main(String[] args) { System.out.println("My first Java program."); System.out.println("The sum of 2 and 3 = " + 5); System.out.println("7 + 8 = " + (7 + 8)); } Sample Run: My first Java program. The sum of 2 and 3 = = 15
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 40 Processing a Java Program Program: written in Java using the Editor and compiled to Bytecode using the Compiler. Loader: transfers the compiled code (bytecode) into main memory and loads the necessary Libraries. Interpreter: reads and translates each bytecode instruction into machine language and then executes it.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 41 Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: ♦ understand the problem ♦ dissect the problem into manageable pieces ♦ design a solution ♦ consider alternatives to the solution and refine it ♦ implement the solution ♦ test the solution and fix any problems that exist
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 42 Algorithm Sequence of instructions used to carry out a task or solve a problem May be written in either English or pseudocode ♦ outline of a program that could be translated into actual code May need refinement as you work Always write out your algorithm before you begin programming
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 43 Problem-Analysis-Coding-Execution most important step without computer with computer
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 44 Algorithm Design Example Problem: Convert change in cents to number of half- dollars, quarters, dimes, nickels, and pennies to be returned. Example: ♦ given 646 cents ♦ number of half-dollars: divide 646 by 50 quotient is 12 (number of half-dollars) remainder is 46 (change left over) ♦ number of quarters: divide 46 by 25 quotient is 1 (number of quarters) remainder is 21 (change left over) ♦ number of dimes, nickels, pennies ♦ result: 12 half-dollars, 1 quarter, 2 dimes, 0 nickels, 1 penny
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 45 Resulting Algorithm 1.Get the change in cents 2.Find the number of half-dollars 3.Calculate the remaining change 4.Find the number of quarters 5.Calculate the remaining change 6.Find the number of dimes 7.Calculate the remaining change 8.Find the number of nickels 9.Calculate the remaining change 10.The remaining change is the number of pennies.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 46 Exercise Execution of c=2*a+b in a computer
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 47 To do Read ch. 2 Exercise 10 in ch. 1 (algorithm design)
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 48 What’s Ahead... Java Basics Homework 1 due tonight Homework 2 assigned Monday Quiz Tuesday: computers and Java basics