Top Banner
COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00- 12:15 Peabody Hall 218
47

COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

COMP 14Introduction to Programming

Mr. Joshua StoughJanuary 19, 2005

Monday/Wednesday 11:00-12:15

Peabody Hall 218

Page 2: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Announcements

• Registration – Today is last day to add– Karen Thigpen ( [email protected] )–

• HW 0 due today 11:59pm• • Please pay attention to what you are

supposed to name the file.– Questions I responded to?–

• Office hours: W 9:30, F 12:00, by appointment

Page 3: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Questions from Last Time• Note announcements on

Blackboard

• Do I need a laptop for COMP 14?

• Do I need Internet access for COMP 14?

Page 4: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Last Time in COMP 14

• Syllabus• Honor Code• Blackboard• jGRASP

• Due today– Homework 0– Pledge form

• bring to front at end of class• turn in to Sitterson front desk by 5pm today

Page 5: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Today in COMP 14

• Parts of the computer– hardware vs. software– CPU and memory

• Binary numbers

• What is an algorithm?

• Book references: Chapter 1

Page 6: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Reading Check-Up

1. - computer components including the CPU, main memory, I/O devices, and secondary storage

2. - the brain of the computer, containing the CU, PC, IR, ALU, and ACC

3. - computer instructions to solve a problem

4. The digits 0 and 1 are calledor the shortened term

hardware

CPU

program

binary digits

bits

Page 7: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Before Programming...

• You need a basic understanding of how a computer works– if you’re going to drive a car, you need to

know that it runs on gasoline...

• Understanding the pieces will help you understand what your programs actually do

• Plus, we want you to be able to talk intelligently about computers

Page 8: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Hardware vs. Software

A computer is made up of hardware and software

Hardware Software• CPU

- ex: 1 GHz Pentium III• 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

Page 9: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Hardware Organization

motherboard

CPU

memory

hard drive

Page 10: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Central Processing UnitCPU• 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

Page 11: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

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

Page 12: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Main MemoryWith 100 Cells

Each memory cell has a numeric address, which uniquely identifies it

Page 13: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

CPU and Main Memory

Chip that executesprogram instructions

Primary storage areafor programs and datathat are in active use(RAM)

All programs must be brought into main memory before execution

Page 14: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

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

Page 15: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Secondary Storage

Secondary memorydevices providelong-term storage

Information is movedbetween main memoryand secondary memoryas needed

Page 16: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Input Devices

• Definition: devices that feed data and computer programs into computers

• Examples:– Keyboard– Mouse– Secondary Storage

Page 17: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Output Devices

• Definition: devices that the computer uses to display results

• Examples:– Printer– Monitor– Secondary Storage

Page 18: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Input/Output Devices

I/O devices facilitateuser interaction

Page 19: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Hardware Components

Page 20: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Opening Notepad• Use the mouse to select

Notepad• The CPU requests the

Notepad application• Notepad is loaded from

the hard drive to main memory

• The CPU reads instructions from main memory and executes them one at a time

• Notepad is displayed on your monitor

Page 21: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Questions

1. - points to the next instruction to be executed

2. - a unique location in memory

3. - stores information permanently

4. Instructions executed by the CPU must be first loaded from

program counter (PC)

address

secondary storage

main memory

Page 22: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

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

Page 23: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

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

Page 24: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Application Programs

• Written using programming languages

• Perform a specific task• Run by the OS• Example programs:

– Word Processors– Spreadsheets– Games

Page 25: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Questions

Classify the following pieces of software as operating system or application:

1. Microsoft Windows 20002. Microsoft PowerPoint3. Linux4. Your COMP 14 programs

OS

OS

app

app

Page 26: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

It’s All About Data

• Software is data– numbers, characters– instructions, programs

• Hardware stores and processes data– read, write– add, subtract, multiply, divide

Page 27: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

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– represented as series of 0 and 1

• 0 - low voltage• 1 - high voltage

– can be copied exactly– ex: music on a compact disc

Page 28: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

H i , H e a t h e r .

72 105 44 32 72 101 97 116 104 101 114 46

Representing Text Digitally• All information in a computer is

digitized, broken down and represented as numbers.

Corresponding upper and lower case Corresponding upper and lower case letters are separate characters.letters are separate characters.

Page 29: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

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?

Page 30: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

1 bit01

2 bits

00011011

3 bits

000001010011100101110111

4 bits

00000001001000110100010101100111

10001001101010111100110111101111

Each additional bit doubles the number of possible permutations

Bit Permutations

Page 31: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

21 = 2 items

22 = 4 items

23 = 8 items

24 = 16 items

25 = 32 items

1 bit ?

2 bits ?

3 bits ?

4 bits ?

5 bits ?

How manyitems can be

represented by

Bit Permutations

• Each permutation can represent a particular item

• There are 2N permutations of N bits– N bits are needed to represent 2N

unique items

Page 32: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Binary Numbers

• N bits to represent 2N values• N bits represent values 0 to 2N-1• Example: 5 bits

– 32 unique values (0-31)– 00000 = 0– 11111 = 31

24 23 22 21 20

16 + 8 + 4 + 2 + 1

Page 33: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Decimal to Binary

114 1110010

Number Place Value Digit114 26 = 64 1

50 25 = 32 118 24 = 16 12 23 = 8 02 22 = 4 02 21 = 2 10 20 = 2 0

Page 34: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

QuestionsBinary Numbers

• What’s the maximum value a 6-bit number can represent?

• What’s the decimal representation of 111010?

• What’s the binary representation of 35?

63

58 = 32+16+8+2

100011

Page 35: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

KB 210 = 1024

MB 220 (over 1 million)

GB 230 (over 1 billion)

TB 240 (over 1 trillion)

Unit Symbol Number 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:

Page 36: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Programming LanguagesEvolution• 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

Page 37: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Assembly and Machine Language

Page 38: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Programming LanguagesEvolution

• High-level languages make programming easier

• Closer to spoken languages• Examples:

– Basic – FORTRAN– COBOL– C/C++– Java

Page 39: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

From Java to Machine Language• Computers understand only 0 and 1 (machine

language)• Compiler translates source code into machine

code

• Java compiler translates source code (file ending in .java) into bytecode (file ending in .class)– bytecode is portable (not machine-specific)

• Java interpreter reads and executes bytecode– different Java interpreters for different types of CPUs

and operating systems (OS)• Intel/Windows, Motorola/Mac OS X, Intel/Linux

Page 40: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

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

Page 41: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

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

Page 42: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Problem-Analysis-Coding-Execution

most important stepwithoutcomputer

withcomputer

Page 43: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

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

Page 44: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Resulting Algorithm

1. Get the change in cents2. Find the number of half-dollars3. Calculate the remaining change4. Find the number of quarters5. Calculate the remaining change6. Find the number of dimes7. Calculate the remaining change8. Find the number of nickels9. Calculate the remaining change10.The remaining change is the number of

pennies.

Page 45: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Program 1 and Homework 1• Smith Center Banners

– posted on Blackboard

• Book problems: Ch.1 and first part of Ch. 2.

• Make sure program compiles and runs• Don't forget to answer questions that are

asked in instructions• Due Wednesday 26th at 11:59pm

• Turn in pledges.

Page 46: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

What’s Ahead...

• Java Basics

• Reading: Ch 2 (pgs. 21-37)

• Program 1 due on Wednesday, 26th • Homework 1 due on Wednesday,

26th

Page 47: COMP 14 Introduction to Programming Mr. Joshua Stough January 19, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Remember

• Pledge form

• Attend recitation. Bring paper copies of the programs for that day that would help you.

• Follow instructions in the assignment. Follow the file naming format.

• Jar your code files.