0 ratings0% found this document useful (0 votes) 68 views57 pagesChapt 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, 
claim it here.
Available Formats
Download as PDF or read online on Scribd
JAVA
SECOND EDITION
Chapter 1
Introduction to Object-
Oriented Programming and
Problem Solving
(© 2008 Jones snd Barlet PublishersKnowledge Goals
Understand what a computer program is
Know the three phases of the software life
cycle
Understand what an algorithm is
Learn what a high-level programming
language is
Understand the difference between machine
code and Bytecode
(© 2008 fones snd Barlet PublishersKnowledge Goals
Understand the compilation, execution, and
interpretation processes
Learn what the major components of a
computer are and how they work together
Understand the concept of an object in the
context of computer problem solving
(© 2008 fones snd Barlet PublishersSkills aS
List the basic stages involved in writing a
computer application
Distinguish between hardware and software
List the ways of structuring code in a Java
application
Name several problem-solving techniques
Choose an applicable problem-solving
technique
Identify the objects in a problem statement
How can you distinguish the knowledge goals from the
skills goals?
ot Date PublishersWhat is Programming?
Computer
A programmable device that can store, retrieve, and
process data
Computer programming
The process of specifying objects and the ways in
which those objects interact to solve problems
Programming
Writing out instructions for solving a problem or
performing a task
Computer program
Instructions defining a set of objects and orchestrating
their interactions to solve a problem
1d Ware PublishersHow do we write a Program ;
PROBLEM-SOLVING PHASE IMPLEMENTATION PHASE
  
   
  
  
   
  
 
Analysis
and specification
General solution Concrete solution
(algorithm)
|
Verity
 
 
 
  
 
Revise to address
new requirements
and correct errors
discovered in use
MAINTENANCE PHASEHow do we write a program ...
Problem-Solving Phase
« Understand (define) the problem and
identify what the solution must do
* Specify the objects and their interactions to
solve the problem
+ Follow the steps exactly to see if the
solution really does solve the problemHow do we write a program ...
Implementation Phase
« Translate the object specifications (the
general solution) into a programming
language
+ Have the computer carry out the solution
and check the results
What if the solution isn't correct?How do we write a program ...
Maintenance Phase
« Use the program
+ Modify the program to meet changing
requirements, to enhance its functionality,
or to correct any errors that show up in
using itHow can a computer work with objects...
Hat Size Magnification
  
Travel Time
(© 2008 fones snd Barlet PublishersNea do we ae a program ...
 
(© 2008 fones snd Barlet PublishersHow can a computer work with objects?
Information
Any knowledge that can be communicated
Data
Information in a form that a computer can use
Object
Acollection of data values and associated
operationsHow can a computer work with objects...
Object Object Output of
Contents Operation Operation
Ms. Jane T. Leegle Esq.
 
  
(© 2008 fones snd Barlet PublishersHow can a computer work with objects...
Objects contain data and operations
Consider a Name object
What sort of data would this object contain?
What sort of operations might we need to
manipulate the data?What is an algorithm?
Once we have the algorithm, then what?
We translate the algorithm into statements
in a programming language
Say again?
(© 2008 fones snd Barlet PublishersClasses of Objects
Date class
data: month, day, year
operations: set and return month, day, year
Date object (an instance of a class)
June
23
2004
(© 2008 Jones an ware PublishersWhat is an algorithm?
How are the operations specified?
Algorithm
Instructions for solving a problem ina
finite amount of time using a finite amount
of dataaORON>
6.
 
What is an algorithm?
Objects: Key, Transmission, Gas Pedal, Engine,
Phone
Insert the key.
Make sure the transmission is in Park (or Neutral).
Depress the gas pedal.
Turn the key to the start position.
If the engine starts within six seconds, release the key.
If the engine doesn’t start within six seconds, release
the key and gas pedal, wait ten seconds, and repeat
Steps 3 through 6, but not more than five times.
7. If the car doesn’t start, phone your mechanic.
(© 2008 fones snd Barlet PublishersWhat is a ve nae 9
   
Chip's Java Code
 
b. Algorithm translated by different people
2 © 2008 jones and arte PaihersWhat kind of instructions ...
Computer
A programmable device that can store,
retrieve, and process data
[ers
accept data, send data, perform
arithmetic operations, perform logical
operationsClasses of Objects
What about multiple objects with the same
properties?
Class
A specification of the representation of a
particular kind of object, in terms of data
and behavior (operations)What kind of instructions ...
Control structures
Statements that organize the instructions
that specify the behaviors of objects
- sequence
- selection
- repetition
- subprograms
- asynchronous
(© 2008 fones snd Barlet PublishersWhat kind of instructions ...
‘SELECTION (also called branch or decison) fn
IF ooditon THEN stalomentt ELSE statment2
 
 
(© 2008 Jones an ware PublishersWhat is a programming language?
Programming language
Aset of rules, symbols and special words used to
construct a computer program
Statements
Specific combinations of symbols and special words that
are defined by a programming language to be complete
units within a program; analogous to sentences in a
human language
Code
Instructions for a computer that are written
programming language
20What kind of instructions ...
LOOP (also called repetition or iteration)
WHILE condition DO statamentt
 
 
 
|
'SUBPROGRAM (also called method, procedure, function, or subroutine)
‘SUBPROGRAMT
JBPROGRAM
meaningful collection z
 
any of the above
ASYNCHRONOUS
  
25 (© 2008 fones snd Barlet PublishersHow It All Works
 
 
 
(© 2008 Jones an ware PublishersSoftware Maintenance
Maintaining software accounts for the
majority of its live cycle (i.e., old programs
never die, they just get modified)
Where were you on January 1, 2000?Assembly Languages
+ Are machine dependent and run on only one
specific type of computer
+ Are translated into machine code by
assemblers
- Are made up of English-like abbreviations
such as LOAD, STORE, or ADDHow It All Works
Assembly Language Machine Language Meaning
1001 001 014 101
 
Operation Result Operand1 Operand2 Operation Result Operand 1 Operand 2
High-level language:
R1=R3+R5
R2=R6-R1
29 (© 2008 Jones an ware PublishersHigh-Level Languages
Source code
Instructions written in a high-level
language
Object code
A machine language version derived from
source code
(© 2008 Jones andHow It All Works
Machine language
The language, made up of binary-coded
instructions, that is used directly by the
computer
Assembly language
A low-level programming language in
which a mnemonic represents each
machine language instruction for a
particular computer
28Machine Language
- Is not portable
- Runs only on specific type of computer
- Is made up of binary-coded instructions
(strings of Os and 1s)
- Is the language that can be directly used
by the computerSoftware Maintenance
Software maintenance tips
- Check existing code
- Make changes to a copy
- Change related aspects to leave clean,
consistent code for next change
- Keep backup copiesHigh-Level Languages
» Are portable
- Are translated into machine code by
compilers
- Instructions are written in language similar to
natural language
- Examples -- FORTRAN, COBOL, Pascal,
C, C++
» Many are standardized by ISO/ANSI to
provide an official description of the
languageHigh-Level Languages
Direct execution
The process by which a computer performs the actions
specified in a machine-language program
Interpretation
The translation, while a program is running, of non-
machine language instructions (such as Bytecode) into
executable operations
Virtual machine
Aprogram that makes one computer act like another
37
(© 2008 fones snd Barlet PublishersWhat's inside the computer?
    
 
 
Control unit The Manager
Arithmetic/logic unit Does operationsWhat's inside the computer?
 
 
~<— Your program
 
 
Memory unit
Internal data storage inside a computer, made up of
an ordered sequence of addressable cells
 
 
40 © 2008 fone a are PublishersJava's Road to Portability
Bytecode
A standardized machine language into
which Java source code is compiled
Windows PC:
running JVMPortability
opuect
‘COMPUTER ‘CODE
EXECUTES: (MACHINE
‘TRANSLATOR LANGUAGE ‘compuTen
SouRCE CODE ‘cope VERSION OF EXECUTES:
(ces) (compuen) ‘SOURCE CODE) OBJECT CODE
Windows PC
Ces compiler
 
z
a
(C++ compior
 
 
35 (© 2008 Jones an ware PublishersHigh-Level Languages
      
 
 
Input Direct Execution Output
Interpretation Phase
 
es and arte PublishersWhat's inside the meWhat's inside the computer?
Hardware
The physical components of a computer
Software
Computer programs; the set of all programs
available on a computer
Clock
An electrical circuit that sends out a train of pulses
to coordinate the actions of the computer's
hardware components; its speed is measured in
hertz (cycles per second)
42Problem Solving
How to Solve It: A New Aspect of
Mathematical Method by George Polya
"How to solve it list" written within the
context of mathematical problems
But the list is quite generalProblem Solving
How ao you solve problems?
Understand the problem
Devise a plan
Carry out the plan
Look back
(© 2008 Jones an ware PublishersWhat is system software?
User/computer interface
Aconnecting link that translates between the computer's
internal representation of data and representations that
humans are able to work with
Interactive system
A system that supports direct communication between the
user and the computer
System software
The set of programs that simplifies the user/
communication interface
Operating system
Set of programs that manages all the computer's resources
43What is system Se
 
   
 
See
how
it
all
fits
together
?
 
 
 
 
oureur
 
(© 2008 Jones an ware PublishersProblem Solving
Problem solving
The act of finding a solution to a
perplexing, distressing, vexing, or
unsettled question
How do you define problem solving?Problem-Solving Techniques
 
(© 2008 Jones an ware PublishersProblem-Solving Techniques
Ask questions!
- What do | know about the problem?
- What is the information that | have to
process in order the find the solution?
- What does the solution look like?
- What sort of special cases exist?
- How will | recognize that | have found
the solution?Problem-Solving Techniques
Never reinvent the wheel!
Similar problems come up again and again
in different guises
A good programmer recognizes a task or
subtask that has been solved before and
plugs in the solution
Can you think of two similar problems?Problem-Solving Techniques
Hard problem |Problem-Solving Techniques
Means: Fy, walk, hitchhike, bike,
rive, sail, bus
Revised Means: Fly to Chicago and then to Austin;
fly to Newark and then to Austin: fly to Atlanta
land then to Austin
Start: Boston Means to Intermediate Goal: Commuter:
Intermediate Goal: Newark walk, hitchhike, bike, drive, sail, bus
Goal: Austin
Solution: Take commuter flight to Newark and then catch cheap flight to Austin
 
52
(© 2008 fones snd Barlet PublishersProblem-Solving Techniques
Existing
Software
New Program
 
 
 
 
Inventory System
 
 
 
Existing
Software
 
 
 
 
 
(© 2008 fones snd Harlet PublishersProblem-Solving Techniques
List of Temperatures
Use the same
method to
find these
values in
both cases.
Highest = 95
Lowest = 18
50
List of Test Scores
27
14
55
98
72
66
45
12
39
70
68
Highest = 98
Lowest = 12
 
(© 2008 fones snd Barlet PublishersProblem-Solving Techniques
‘Summing Values on a File
   
Summing and Counting
Values on a File
   
While data on file
Read value
Add value to sum.
Add one to count
Close file
     
 
   
   
 
 
55 (© 2008 Jones and ware PublishersProblem-Solving Techniques
Object-Oriented Problem Solving Stages
- Brainstorm List all objects that might
contribute to solution
- Filter Review the classes to find duplicates
or remove unnecessary objects
- Responsibilities Determine the operations
associated with a class of objects
- Collaborations Determine the interactions
between classesProblem-Solving Techniques
- Attributes Determine the values defined by
a class that are used to represent its
objects
- Driver Write a program for creating the
objects and coordinating their
collaborations to solve the problem