Top Banner
Computer Programming Computer Programming Chapter 1 Chapter 1
30

Computer Programming Chapter 1. Computers Personal computers desktop, laptop, and notebook machines web-surf, chat, write letters/papers,... Embedded.

Dec 25, 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: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

Computer ProgrammingComputer Programming

Chapter 1Chapter 1

Page 2: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

ComputersComputers

Personal computersPersonal computers desktop, laptop, and notebook machinesdesktop, laptop, and notebook machines web-surf, chat, write letters/papers, ...web-surf, chat, write letters/papers, ...

Embedded systemsEmbedded systems games consoles, cell phones, cars, ... games consoles, cell phones, cars, ...

» might not even notice you’re using a computer!might not even notice you’re using a computer!

ServersServers web servers, file servers, cloud computing, ...web servers, file servers, cloud computing, ...

Page 3: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

Computer NetworksComputer Networks

Computers send messages to each otherComputers send messages to each other phone to web server: can I have this page?phone to web server: can I have this page? web server to phone: here it is.web server to phone: here it is.

Network typesNetwork types local, wide, satellite; wireless; peer-to-peerlocal, wide, satellite; wireless; peer-to-peer ““J” drive on lab computer J” drive on lab computer file server file server

Cloud computingCloud computing save data files (save data files (e.g. e.g. pictures); run programspictures); run programs

Page 4: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

What is a Computer?What is a Computer?

A machine that stores information and A machine that stores information and instructions for its own operationinstructions for its own operation

Hardware = the Hardware = the machinemachine part part Software = the Software = the storedstored stuff stuff Computer program = a set of instructionsComputer program = a set of instructions

programmable = can set/change what it doesprogrammable = can set/change what it does

Page 5: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

Computer Hardware...Computer Hardware...

Input devices (data into the computer)Input devices (data into the computer) mouse, keyboard, microphone, touch screen, ...mouse, keyboard, microphone, touch screen, ...

Output devices (data out of the computer)Output devices (data out of the computer) monitor, speakers, printer, ...monitor, speakers, printer, ...

Exercise:Exercise: name some input and output devices/modes forname some input and output devices/modes for

» iPodiPod» video game systemsvideo game systems

Page 6: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

...Computer Hardware......Computer Hardware...

CPU (Central Processing Unit)CPU (Central Processing Unit) process the data (add, multiply, move, ...)process the data (add, multiply, move, ...) understands the instructionsunderstands the instructions may have extra CPUs (GPU, for example)may have extra CPUs (GPU, for example)

Main MemoryMain Memory remember what the computer’s working onremember what the computer’s working on volatile = lost when power goes outvolatile = lost when power goes out small(ish) capacity (megabytes/gigabytes)small(ish) capacity (megabytes/gigabytes)

Page 7: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

...Computer Hardware...Computer Hardware

Secondary storageSecondary storage remember things when the power’s offremember things when the power’s off large capacity (gigabytes/terabytes/...)large capacity (gigabytes/terabytes/...) Internal:Internal:

» hard disk, flash cardhard disk, flash card External:External:

» USB drive, CD, DVD, ...USB drive, CD, DVD, ...» punch cards, tape, floppy disk, ...punch cards, tape, floppy disk, ...

Page 8: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

What is “Memory”What is “Memory”

Where information is storedWhere information is stored your user data (photos, papers, messages, ...)your user data (photos, papers, messages, ...) your programs (browsers, word processors, ...)your programs (browsers, word processors, ...)

Parts of memoryParts of memory bits: each either a 0 or a 1 (“bits: each either a 0 or a 1 (“binary digitbinary digit”)”) bytes: 8 bitsbytes: 8 bits

» each byte has an address (is each byte has an address (is addressableaddressable)) everythingeverything is represented with bits and bytes is represented with bits and bytes

Page 9: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

SoftwareSoftware

Data & instructionsData & instructions programs (instructions) manipulate dataprograms (instructions) manipulate data all represented with bits/bytesall represented with bits/bytes

Data hierarchyData hierarchy 8 bits 8 bits byte byte 1 or more bytes 1 or more bytes data value ( data value (fieldfield)) 1 or more data values/objects 1 or more data values/objects object ( object (recordrecord)) data may be stored on secondary memory (data may be stored on secondary memory (filefile))

Page 10: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

Representing Data ValuesRepresenting Data Values

Usually requires multiple bytesUsually requires multiple bytes the letter ‘A’the letter ‘A’ this colour the number 65the number 65 the number 65.0the number 65.0

the String “65.0”the String “65.0”

Same byte values != same dataSame byte values != same data similarsimilar data != data != samesame data data

00000000 01000001

00000000 01000001

00000000 01000001

00000000

0000000000000000

01000000 000000000101000001000000

00000000 000000000000000000000000

00000000 000001000000000000000000

00110110 00110101 00101110 00110000

Page 11: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

Representing ObjectsRepresenting Objects

Complex objects require lots of dataComplex objects require lots of data some of which may be other objects (parts)some of which may be other objects (parts)

Name: (String)CreatorName: (String)FavoriteColour: (byte)MonthBorn: (byte)DayBorn: (byte)Height: (byte)Weight: (byte)FaceShape: (byte)...

MiiAvatar:

Page 12: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

Users and ProgrammersUsers and Programmers

We are computer program usersWe are computer program users use a program already on the computeruse a program already on the computer download a program and use itdownload a program and use it

The program was created by a programmerThe program was created by a programmer sometimes by a sometimes by a teamteam of programmers of programmers you’re going to learn to be a programmeryou’re going to learn to be a programmer

» then you can use programs you made by yourself!then you can use programs you made by yourself!

Page 13: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

ProgramsPrograms

Instructions the computer can followInstructions the computer can follow Machine languageMachine language executablesexecutables

Computers understand this – humans, notComputers understand this – humans, not

High-level languagesHigh-level languages source codesource code Humans can use theseHumans can use these Need to be translated to machine languageNeed to be translated to machine language

LOTS of different languagesLOTS of different languages lots of different lots of different kindskinds of languages of languages

Page 14: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

Programming LanguagesProgramming Languages

Programmers create programsProgrammers create programs Programs are instructions to the computerPrograms are instructions to the computer

compare: recipes are instructions to cookscompare: recipes are instructions to cooks

Generally we Generally we writewrite instructions instructions but computers don’t understand Englishbut computers don’t understand English

» or any other or any other naturalnatural language language manymany special languages for programming special languages for programming

» programmingprogramming languages languages

Page 15: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

Example LanguagesExample Languages

FORTRANFORTRAN LISPLISP ALGOLALGOL COBOLCOBOL SNOBOLSNOBOL PL/IPL/I BASICBASIC APLAPL

PascalPascal SmalltalkSmalltalk cc PrologProlog SchemeScheme ModulaModula SQLSQL AdaAda

C++C++ PrographPrograph HyperTalkHyperTalk PerlPerl PythonPython JavaJava JavascriptJavascript C#C#

and lots, lots more!

Page 16: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

5.5. Rem calculate an averageRem calculate an average

10.10. sum = 0sum = 0

20.20. count = 0count = 0

30.30. print(“Enter a number: ”)print(“Enter a number: ”)

40.40. input(n)input(n)

50.50. if n<0 goto 90if n<0 goto 90

60.60. sum = sum + nsum = sum + n

70.70. count = count + 1;count = count + 1;

80.80. goto 30goto 30

90.90. ave = sum/countave = sum/count

100.100. print(“Average = ”, ave)print(“Average = ”, ave)

BASIC

an old beginners’ language

Page 17: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

Program Average(Input, Output);Program Average(Input, Output);

var sum, count, n: integer;var sum, count, n: integer;beginbegin sum := 0;sum := 0; count := 0;count := 0; repeatrepeat write(“Enter a number: ”);write(“Enter a number: ”); read(n);read(n); if n >= 0 then if n >= 0 then beginbegin sum := sum+n; sum := sum+n; count := count+1;count := count+1; endend until n < 0;until n < 0; writeln(“Average = ”, sum/count)writeln(“Average = ”, sum/count)end.end.

Pascal

a “structured” language

Page 18: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

#include <iostream>#include <iostream>using namespace std;using namespace std;

void main() {void main() { int sum = 0;int sum = 0; int count = 0;int count = 0; int n;int n;

cout << “Enter a number: ”;cout << “Enter a number: ”; cin >> n;cin >> n; while (n > 0) {while (n > 0) {

sum += n; count++;sum += n; count++; cout << “Enter a number: ”;cout << “Enter a number: ”; cin >> n;cin >> n;

}}

cout << “Average = ” << sum/count;cout << “Average = ” << sum/count;}}

C++

we used this language in 1226 until a few years ago

Page 19: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

import java.util.Scanner;import java.util.Scanner;

public class Average {public class Average { public static void main(String[] args) {public static void main(String[] args) { int sum = 0;int sum = 0; int count = 0;int count = 0; int n;int n;

System.out.print(“Enter a number: ”);System.out.print(“Enter a number: ”); Scanner kbd = new Scanner(System.in);Scanner kbd = new Scanner(System.in); n = kbd.nextInt();n = kbd.nextInt(); while (n > 0) {while (n > 0) { sum += n; count++;sum += n; count++; System.out.print(“Enter a number: ”);System.out.print(“Enter a number: ”); n = kbd.nextInt();n = kbd.nextInt(); }}

System.out.print(“Average = ”);System.out.print(“Average = ”); System.out.println(sum/count);System.out.println(sum/count); }}}}

Java

this is the language we use in 1226 now

Page 20: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

average(List, Average) :-average(List, Average) :-

sumList(List, Sum),sumList(List, Sum),length(List, Length),length(List, Length),Average is Sum / Length.Average is Sum / Length.

sumList([], 0).sumList([], 0).sumList([Num | MoreNums], Total) :-sumList([Num | MoreNums], Total) :-sumList(MoreNums, SubTotal),sumList(MoreNums, SubTotal),Total is Num + SubTotal.Total is Num + SubTotal.

Prolog

a logic-programming language

Page 21: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

averageaverage

^(self^(selfinject: 0inject: 0into: [:element :tempsum |into: [:element :tempsum |

tempsum + element])tempsum + element]) //self size.self size.

Smalltalk

an object-oriented language

Page 22: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

a graphical programming tool

AppInventor

Page 23: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

Pseudocode / AlgorithmsPseudocode / Algorithms

Program is instructions for computerProgram is instructions for computer recipe is instructions for cookrecipe is instructions for cook

Can be in any programming languageCan be in any programming language recipe can be in English, French, Korean, ...recipe can be in English, French, Korean, ...

Generally Generally startstart in a mixture of English and in a mixture of English and some generic programming languagesome generic programming language called called pseudocodepseudocode (“almost code”) (“almost code”) make an make an algorithmalgorithm (steps to solve the problem) (steps to solve the problem)

Page 24: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

to Find the Average of a Listto Find the Average of a List

1.1. create the count variable & set it to zerocreate the count variable & set it to zero

2.2. create the sum variable & set it to zerocreate the sum variable & set it to zero

3.3. for each number in the listfor each number in the lista)a) add it to the sumadd it to the sumb)b) add one to the countadd one to the count

4.4. divide the sum by the count to get the divide the sum by the count to get the averageaverage

Typically when we write an algorithm, we number the steps in the order they’re to be done

Page 25: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

VariablesVariables

Algorithm may need to remember thingsAlgorithm may need to remember things numbers, names, numbers, names, etc.etc.

Values are stored in Values are stored in variablesvariables variablevariable = = may change its valuemay change its value

Each variable remembers a particular valueEach variable remembers a particular value count:count: how many numbers we added up so farhow many numbers we added up so far sum:sum: what the total is so farwhat the total is so far average:average: the number we’re looking for!the number we’re looking for!

Page 26: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

ExerciseExercise

Write an algorithm to calculate the area of a Write an algorithm to calculate the area of a rectanglerectangle what more information do you need?what more information do you need?

» where will you get it?where will you get it? write pseudo-codewrite pseudo-code

Page 27: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

Programming ProgramsProgramming Programs

We We useuse programs to programs to writewrite programs programs need to write the need to write the codecode (can use Notepad)(can use Notepad) need to need to compilecompile (translate) the code (translate) the code ((javacjavac)) need to need to run run the code the code ((javajava))

IDE: Integrated Development EnvironmentIDE: Integrated Development Environment use to write, compile and runuse to write, compile and run JCreator, NetBeans, ...JCreator, NetBeans, ...

Page 28: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

Example IDE (NetBeans)Example IDE (NetBeans)

Type program here

Run program here

Listof

projects

Programparts

Page 29: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

For This CourseFor This Course

Prefer you use JCreator or NetbeansPrefer you use JCreator or Netbeans (I find JCreator a little “friendlier”)(I find JCreator a little “friendlier”)

Accessing files on J-drive can be slowAccessing files on J-drive can be slow may want to use a USB drivemay want to use a USB drive

» may want to transfer files to J-drive at end of labsmay want to transfer files to J-drive at end of labs

Can get JCreator and NetBeans at homeCan get JCreator and NetBeans at home If you have a cs account, you can use itIf you have a cs account, you can use it

Page 30: Computer Programming Chapter 1. Computers  Personal computers  desktop, laptop, and notebook machines  web-surf, chat, write letters/papers,...  Embedded.

Questions?Questions?