Upload
anonymous-dd3kahswk
View
217
Download
0
Embed Size (px)
DESCRIPTION
chap
Citation preview
Chapter 1Introduction to Object-Oriented Programming
Chapter 1 TopicsOverview of Object-Oriented ProgrammingHow is Java Code Converted into a Form that a Computer Can use?Interpreting Code vs Executing CodeCompilation, Interpretation, and ExecutionKinds of Instructions Inside the ComputerProblem-Solving Techniques
What is Computer Programming?It is the process of specifying the data types and the operations for a computer to apply to data in order to solve a problem
Programming Life Cycle Phases Problem-Solving
Implementation
Maintenance
Problem-Solving PhaseANALYZE the problem and SPECIFY what the solution must do
Develop a GENERAL SOLUTION (ALGORITHM) to solve the problem
VERIFY that your solution really solves the problem
Sample Problem
A programmer needs an algorithm to determine an employees weekly wages
How would the calculations be done by hand?
One Employees Wages During one week an employee works 52 hours at the hourly pay rate $24.75How much is the employees wages? Assume a 40.0 hour normal work weekAssume an overtime pay rate factor of 1.5
Weekly Wages, in GeneralIf hours is over 40.0, then
wages = (40.0 * payRate) + (hours - 40.0) * 1.5 *payRate
otherwise,
wages = hours * payRate RECALL EXAMPLE ( 40 x $ 24.75 ) + ( 12 x 1.5 x $ 24.75 ) = $1435.50
A class is . . .a description of the representation of a specific kind of object, in terms of data and behaviorDate classdata: month, day, yearoperations to set and return month, day, year
An Object is . . .an instance of a classa Date objectJune232004
An Algorithm is . . .instructions for solving a problem in a finite amount of time using a finite amount of data
A Program is . . .an algorithm written for a computer that defines classes of objects and orchestrates their interactions to solve a problemobjects work together to create an application (or program) that solves a problem
Employees Weekly WagesObjects: Employee, pay rate, hours worked, wagesAlgorithm: 1. Get the employees hourly pay rate 2. Get the hours worked this week 3. Calculate this weeks regular wages 4. Calculate this weeks overtime wages (if any) 5. Add the regular wages to overtime wages (if any) to determine total wages for the week
A Programming Language is . . .a language with strict grammatical rules, symbols, and special words used to construct a computer program
Code is . . .the product of translating an algorithm into a programming language instructions for a computer that are written in a programming language
Implementation Phase: TestTesting means executing (running) your program on the computer, to see if it produces correct results If it does not, check the algorithm and/or code to find the error and fix it Finding known errors is called debugging
Maintenance PhaseUse and modify the program to meet changing requirements or correct errors that show up in using itMaintenance begins when your program is put into use and accounts for the majority of effort on most programs
Programming Life Cycle Problem-Solving Analysis and Specification General Solution ( Algorithm ) Verify Implementation Concrete Solution ( Code ) Test Maintenance Use Maintain
Programming Shortcut?THINKINGCODETESTPROBLEM-SOLVING PHASEIMPLEMENTATION PHASEShortcut?AlgorithmCodeProblem
Binary Representation of DataCircuit states correspond to 0 and 1
Bit (short for binary digit) refers to a single 0 or 1 Bit patterns represent data
1 byte = 8 bits
1 KB = 1024 bytes
1 MB = 1024 x 1024 = 1,048,576 bytes
How Many Possible Digits?Binary (Base 2) Numbers use 2 digits: Just 0 and 1
Decimal (Base 10) Numbers use 10 digits: 0 through 9
Machine LanguageIs not portableRuns only on specific type of computer
Is made up of binary-coded instructions (strings of 0s and 1s)
Is the language that can be directly used by the computer
Assembly LanguagesAre machine dependent and run on only one specific type of computerAre translated into machine code by assemblers Are made up of English-like abbreviations such as LOAD, STORE, or ADD
High Level Languages Are portable Are translated into machine code by compilersInstructions are written in language similar to natural languageExamples -- FORTRAN, COBOL, Pascal, C, C++Many are standardized by ISO/ANSI to provide an official description of the language
Three C++ Program Stages
Java Portability
Windows PCrunning JVM
via compiler
via interpreter JVMSOURCEBYTECODEEXECUTABLESPayroll.javaPayroll.class
Unix boxrunning JVM
Macintoshrunning JVM
Java Bytecode
Java programming languageAchieves portability by using both a compiler and an interpreterJava compiler translates a Java program into an intermediate Bytecode--not machine language An interpreter program called the Java Virtual Machine (JVM) translates each successive instruction in the Bytecode program to machine language and immediately runs it
Basic Control StructuresA sequence is a series of statements that executes one after anotherSelection (branch) executes different statements depending on certain conditionsLoop (repetition) repeats statements while certain conditions are metA subprogram breaks the program into smaller units Asynchronous control handles events that originate outside our program, such as button clicks.
SEQUENCE Statement Statement Statement. . .
SELECTION (branch)IF Condition THEN Statement1 ELSE Statement2 Statement1 Statement Statement2Condition. . .TrueFalse
LOOP (repetition). . .FalseTrueWHILE Condition DO Statement1Condition
SUBPROGRAM (function)SUBPROGRAM1. . .SUBPROGRAM1 a meaningful collection of SEQUENCE, SELECTION, LOOP, SUBPROGRAM
ASYNCHRONOUS CONTROLEVENTEVENTHANDLER
a subprogram executed when an event occurs
Object-Oriented ProgrammingA data type is the specification in a programming language of how information is represented as data and the operations that can be preformed on the dataAn object is a collection of data values and associated operationsA class is a description of one or more like objects
More OOP VocabularyInstantiation is the process of creating an object based on the description provided by a classA package is a collection of related classes
An Object of class TimeSetIncrementWrite . . .TimeOPERATIONS DATAPrivate data:
hrs 8
mins 25
secs 42
Basic Computer Components Central Processing Unit ( CPU ) Arithmetic Logic UnitControl UnitAuxiliary StorageDeviceMemory Unit ( RAM & Registers )Peripherals
Memory UnitIs an ordered sequence of storage cells, each capable of holding a piece of data
Each memory cell has a distinct address
The information held can be input data, computed values, or program instructions
Central Processing Unit (CPU)Has two components to execute program instructions --
Arithmetic/Logic Unit performs arithmetic and logical operationsControl Unit controls the order in which instructions in the program are executed
Peripheral DevicesAre input, output, or auxiliary storage devices attached to a computerInput Devices include keyboard and mouseOutput Devices include printers, video display, LCD screensAuxiliary Storage Devices include disk drives, CD-ROM and DVD-ROM drives
Problem Solving TechniquesASK QUESTIONS -- about the data, the process, the output, error conditionsLOOK FOR FAMILIAR THINGS -- certain situations arise again and againSOLVE BY ANALOGY -- it may give you a place to startUSE MEANS-ENDS ANALYSIS -- Determine the I/O and then work out the details
More Problem Solving TechniquesDIVIDE AND CONQUER -- break up large problems into manageable unitsBUILDING-BLOCK APPPROACH -- can you solve small pieces of the problem?MERGE SOLUTIONS -- instead of joining them end to end to avoid duplicate stepsOVERCOME MENTAL BLOCK -- by rewriting the problem in your own words