lecture 1: overview of programming tami meredith

37
CSCI1226 Introduction to Computing Science and Programming Lecture 1: Overview of Programming Tami Meredith

Upload: howard-cross

Post on 17-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Slide 1

CSCI1226Introduction to Computing Science and Programming Lecture 1: Overview of Programming Tami MeredithComputersHardware (Physical devices)CPU = Central Processing UnitMemoryInput and Output (I/O) DevicesSoftware (Programs)Operating System, e.g., Linux, OS-X, WindowsApplications, e.g., MS Word, Photoshop, Web-BrowsersUsersIT Professionals, e.g., Administrators, ProgrammersCasual Users, e.g., everyone elseDigital SystemsEverything is stored as numbers!Different encodings usedASCII or Unicode for characters, RGB or CMYK for coloursLayers of encodings, e.g., Unicode in PDFBecomes very complicated e.g., jpg (pictures), mpg (movies), mp3 (music), pdf (documents), exe (programs), BinaryAll numbers are stored in base 2 (Binary)2 digits: 0 and 1Easily manipulated with electronics0 = no power1 = power flowing or presentCan be added, subtracted, manipulated the same as decimal (base 10)Can represent negative numbersCan NOT represent all numbers (e.g., 1/3, PI)

Binary in ActionCounting: Binary: 0, 1, 10, 11, 100, 101, 110, 111, 1000, Decimal: 0, 1, 2, 3, 4, 5, 6, 7, 8,

Base 2242322212016s8s4s2s1sBase 1010410310210110010000s1000s100s10s1sMemoryVarious kindsVolatile = loses contents at power offNon-Volatile = always there until erased, persistentRAM = Random Access MemoryVolatileMain memory where executing programs are keptThe Memory HierarchyMemory TermsBit = smallest unit of memory, hold a value of 0 or 1Byte = 8 bits of memory (holds values 0 to 255)Word = 64 bits = 8 bytes Typical number in a computerNumber of bits a register can holdSize of the bus number of bits the CPU works withKiloByte = 210 Bytes = 1024 BytesMegaByte = 220 Bytes = 1 048 576 BytesGigaByte = 230, TeraByte = 240, PetaByte = 250, ...

ProgrammingA program is a set of instructions on how to do something.Programs are written in a programming language such as Java, C, Fortran, Visual Basic, Lisp, Pascal, Cobol ...Computers CAN NOT do anything with Java codeThus, there is a sequence of events we must follow before a program can be executed or run"Must translate the Java code into something the computer can useTranslationCompiling: Done by a compiler (a special program)Turns a program into something a computer can executeDoes the whole program at once (more or less)Executes quickly after being compiledCreates an executable for a specific platformInterpretation: Done by an interpreterTakes one line at a time, and executes itSlower, but more portableProgram can be run anywhere the interpreter runsMove the interpreter and you move everything it executesJavaJava is complicated, uses both a compiler and an interpreterCompile to ByteCode using javacInterpret ByteCode using the jvmSupposed to get some of the advantages of both but in reality is inefficient and kind of clunkyWrite once, run everywhere is actually, Write once, debug everywhereBut, its what we are stuck with ...

Translation and ExecutionProgramming Process (Technical)TextEditorCompiler(javac)Interpreter (JVM: java)UserLibrariesOperating System (Windows)OutputInputJDKWhat is a Program?Programming is just like cooking or assembling a lawn mowerNeed a procedure and materials

Recipe + Ingredients = NUM NUMS! (usually)Instructions + Parts = Working MowerAlgorithm + Data = Program

AlgorithmInstructions The VERBS or actions/commands in a descriptionSet of steps to accomplish a goalAlways supposed to finishWill find the solution if one existsTells you what to do with the parts/ingredients/data

POUR half a cap of shampoo into handLATHER shampoo into wet hairRINSE with waterDataWhat we manipulateThe nouns in a descriptionIn a computer it is all encoded into numbers (digital)E.g., ASCII: A=65, B=66, C=67, ...Can encode: Letters, numbers, colours, pictures, music, documents, ... *ANYTHING*Encodings can be combinedWe are not going to worry to much about them for now, except for ... (next slide)Some Common Data TypesStrings: Sequences of charactershi there, Hello, Bob!, x, RESULTIntegers: Whole numbers... -3, -2, -1, 0, 1, 2, 3, ...Floating Point Numbers: Things with a decimal1.0, 3.14, .333, 100000.0000001Booleans: Truth values for testsTrue, False (thats it, just two Boolean values)Arrays: Ordered lists of thingsWill get to these later in the course ...Syntax and moreJava is a language with its own lexicon (dictionary of allowable words) and syntax (grammar)Learning Java is NOT learning programming any more than learning English makes one a writer!Algorithms and Data are the Plots and Characters of a good story writerYou are now writers! Programming is a creative endeavour that is more than simply typing something into a computerWriting a StoryGrammar is not everythingGood grammar does not make a great writer, but it is a necessary componentBeing good with Java does not make a great programmer, but it is a necessary componentKnowing what to write mattersGood writers create great plots and charactersGood programmers create great algorithms and data structuresProgramming and writing are similarly creative!PHP, VisualBasic, C, Java, ...Languages come into existence and eventually become obsoleteLanguages change constantly, stuff is added and removed or improvedWhile its good to know a language well (since you can use it better) Im more interested in teaching you to PROGRAM in Java, than in teaching you to use Java for programmingYou can bring Cheat Sheets to the exams! Learn to use Java not waste time memorising the details of JavaTrying to develop skills that are independent of JavaAllow you to use other languages in the workplaceProgramming Translating the solution to some problem into a format that a computer can use.Any trained monkey can program! well, almost ...Java is just a tool (Java is our Grammar)Also need to learn plots (algorithms), characters & places (data), eventsWe are not going to focus on Java, we are going to focus on solving problems, designing solutions, implementing the solution (programming), and testing the solution.

First to the keyboard = Last to finish!

Thinking versus TypingGood writers have :Outlines, plot summaries, character descriptionsGood programmers have:Program architectures, plans, notes, outlinesJust because there is a computer in front of you, there is no need to instantly start banging away (often without direction) on the keyboardThe computer doesnt get lonely if you dont use it right away, it wont rust, it wont cry or be sad ... I promise!Think first, Type later!

23Typing is NOT ProgrammingDo not go to the lab and just start typing,This is the surest way to a C, D, or F

READ the task descriptionUNDERSTAND what you are supposed to doTHINK and figure out how to do itDESIGN a solution that should workType it inTEST and EVALUATE, IMPROVE your programProgramming Process (Practical)Identify the problem Figure out what we need to doDetermine the data we have and needSelect/design algorithms that manipulate the data in a manner that we can use(sometimes we swap 2 and 3 experience tells us when)Translate the algorithms and data into Java (or any programming language)Execute the program and evaluate the resultsIf they are not correct, figure out why, fix, and retest (repeat as often as necessary)Concepts Well come back to these a lotControl FlowThe sequence/order that things happen inWe use this to build algorithmsData ManagementKnowing where in memory the data we need is storedLanguage LibrariesThe set of Routines/Procedures/Functions/Methods provided by Java for manipulating dataE.g., Find a specific word in a sentence, Square Root, Write some text to the screenBuilding blocks to save us work!

How To Learn ProgrammingRead a book (bad) ...Take a course (good) ...Find examples of programs (e.g., on the web) and start to try and figure out what they do (BEST)!

All the great programmers Ive met learned to program using method 3. Its what were going to do a lot of ... It works and its easy!

My First Program: hello.java// My First Program// By Tami Meredith

public class hello{ public static void main (String[] args) { System.out.println("Hello World!"); }}Formatting, Lesson 1// My First Program// By Tami Meredith

public class hello{ public static void main (String[] args) { System.out.println("Hello World!"); }}Everything after // is a comment to the reader (shown in red) and is ignored by the computerCurly braces { and } are matched in pairs witheach { being lined up with its matching }The First RulesCASE MATTERSSystem.out.println is not system.out.printlnEvery character is importantSystem.out.println("Hello World");Note: " and not or Every delimiter must have a matching delimiter{ and }, and , ( and ), [ and ], etc.

Successful programming requires extremeATTENTION TO DETAIL!Good PracticeComments are good! Use plenty! Computers ignore them and so they have no impact ever on performance

Blank lines, indents, are FREE! You dont get a discount or prize for having the messy and hard to read code

CLARITY before efficiency. Make it easy to understand and maintain. Simple is usually better ... (to a point)The Value of Style// My First Program// By Tami Meredith

public class hello{ public static void main (String[] args) { System.out.println("Hello World!"); }}

--------------------- Versus -----------------------

public class h{public static void main(String[]a){System.out.println("Hello World!");}}

TradeoffsAll of computing requires the management of tradeoffs:Complexity vs. Clarity, SimplicityPerformance vs. Size and Memory UseEase of use vs. Feature RichnessThere is no correct balance or solution! Finding the right mix of elements is an art formProgrammers are like musicians, we can all play an instrument, but not all of us will be good at it ...Not EVERYONE is a Great ProgrammerAthletes: NBA, NHL, NFL players, Huskies Football, ...Academics: Mathematicians, Physicists, Rocket ScientistsArtists: Successful Authors, Painters, Popular Musicians/SingersLingerie/Swimsuit Models, Cosmeticians, Ballerinas & Dancers

All have something in common ...

They require innate skills or qualities that cant be taught, that we somehow acquire or are born with -- we can't be all these things!Programming is something that all of you can do, but is not going to be easy for some of you Some of you are not going to be that good at it THATS OK!This CourseHelps you learn if you are gifted as a programmerTeaches you important analytical and thinking skills (which is why it is a substitute for calculus)Helps you to understand how computers work and what they can and cant doGives you some insight into what programmers and technical staff (e.g., system administrators) can do for youTeaches you to focus on details and manage multiple levels of complexityExercises Programming is learned by repetition and practiceThere will be lots of in-class exercisesBring PAPER and PENCIL to classBring your textbook for referencePlease don't use a laptop for the exercisesYou learn more if you do them on paperYou get better practice for exams (where you can't use a laptop)To DoGet the textbookRead Chapter 1 (Material from this class), make notes, write down your questions and bring them to the next classIgnore Object-Oriented Programming (in 1.3)Read Chapter 2 to prepare for the next classIgnore the Graphics Supplement for all chapters, please