Title: Chapter 2 Problem Solving
1Chapter 2Problem Solving
2The Program Development Life Cycle
- Define the problem
- Plan the problem solution
- Code the program
- Test and debug the program
- Document the program
- Maintain the program
3- Define the ProblemThe problem must defined in
terms of input, processing, and output. - Look for nouns in the problem statement that
suggest input and output. - Look for verbs to suggest processing steps.
4- Plan the SolutionWhen developing computer
software, the planning stage is implemented using
a collection of algorithms and class diagrams.
5An algorithm is a series of step-by-step
instructions that produces a solution to a
problem.Algorithms are usually coded as methods
within a Java class.
6Pseudocode is English-like statements that
require less precision than a formal programming
language. A good pseudocode algorithm should be
independent of, but easily translated into, any
formal programming language.
7A class diagram is a diagram that shows the data
and methods which belong to a given class.
8- Code the ProgramCoding involves the actual
writing of the program in a formal programming
language. - The program is written, or coded, by translating
your algorithm steps into the formal language
code.
9- Debug the ProgramRealize that you have an
error.Locate and determine the cause of the
error.Fix the error.
10A syntax error is any violation of the rules of
the programming language.
A logic error occurs when the compiler does what
you tell it to do but is not doing what you meant
it to do.
A run-time error occurs when the program attempts
to perform an illegal operation as defined by the
laws of mathematics or the particular compiler in
use.
11- Ways to Debug a Program
- Desk-check
- Compile
- Run
- Debugger
12- Document the ProgramThe final program
documentation is simply the recorded result of - problem definition
- solution planning
- Coding
- testing results
- debugging results
- user instructions
13- Maintain the ProgramFuture changes and
enhancements to the system are made. - Corrective maintenance
- Enhancement maintenance
- Forced maintenance
14Problem Abstraction
- Abstraction provides for generalization in
problem solving by allowing you to view a problem
in general terms, without worrying about the
details of the problem solution.
15Stepwise Refinement
- Stepwise refinement is the process of gradually
adding detail to a general problem solution until
it can be easily coded in a computer language.
16Classification
- Classification is a process humans use to
arrange information into categories. - Use clear-cut rules to place an object into the
appropriate category, or class. - Then step down using additional rules to
differentiate the items.
17Software Development Abstraction
- In software development, we must decide what
details are relevant and which are not. These
decisions must be made within the context of the
overall purpose and domain of the system.
18Abstraction Challenges
- Unlimited possibilities in abstraction
- Usually more than one correct model
- There are incorrect models misrepresents the
real-world situation - No easy test to determine if a model is valid,
need to communicate our model to - The future users of the system
- Fellow software programmers
19Successful Abstraction
- Successful abstraction requires
- Insight into the problem domain
- Creativity
- Good listening skills
- Good observation skills
- An organized process of abstraction
- Effective communication of the proposed model
20Algorithms
- An algorithm is a sequence of step by step
instructions that solves a problem. - Produce a solution to the problem in a finite
amount of time - Employ operations that are well defined
understood by people in the computer industry - Employ instructions that can be carried out
effectively
21Psuedocode Control Structures
- Sequence control structures operations that
produce a single action or result - Decision control structures operations that
allow the computer to make a decision - Iteration control structures operations that
are used for repeating operations within the
algorithm
22Problem  Develop a set of algorithms and class
diagram to calculate the amount of sales tax and
the total cost of a sales item, including tax.
Assume the sales tax rate is 7 percent and the
user will enter the cost of the sales item. Â
23Defining the Problem
Nouns sales tax, total cost, item cost
Input The item cost. (Sales tax is given)
Output The total cost for an item.
Verbs calculate
Processing tax .07 cost totalCostcost
tax
24Initial Algorithm
main() BEGIN Read the item cost from the
user. Calculate the sales tax and total cost
of the sales item. Write the total cost of
the sales item, including sales tax, on the
system monitor. END.
25First Level of Refinement
 setCost() BEGIN Write a user prompt to enter
the cost of the item (cost). Read cost. END.
26First Level of Refinement (cont.)
calculateTotalCost() BEGIN Set tax to (.07
cost). Set totalCost to (cost tax ) END.
getTotalCost() BEGIN Write totalCost. END.
27main()
getTotalCost()
setCost()
calculateTotalCost()
A problem solution diagram for the SalesTax
problem.
28SalesTax
cost
tax
totalCost
setCost()
calculateTotalCost()
getTotalCost()
A class diagram for the SalesTax class shows the
data and method members that make up the class.
29Problem  Develop a set of algorithms and class
diagram to calculate the gross pay for a for a
student working part time (less than 40 hours a
week). Assume that the student will enter his/her
hourly rate of pay and number of hours worked in
a given week. Make sure that the report shows the
student name and date. Â
30Defining the Problem
Nouns gross pay, rate, hours, name, date
Input The rate of pay, the number of hours
worked, the student name and the date.
Output The gross pay for the week, the hourly
rate of pay, the number of hours worked during
the week, the student name, and the date.
Verbs calculate
Processing grossPay rate hours
31Initial Algorithm
main() BEGIN Read the rate, hours, name, and
date from the user. Calculate the gross pay.
Write the payroll report to the user. END.
32First Level of Refinement
 setData() BEGIN Write a user prompt to enter
the student name. Read name. Write a user
prompt to enter the date. Read date. Write
a user prompt to enter the hourly rate of pay.
Read rate. Write a prompt to enter the number
of hours worked. Read hours. END.
33First Level of Refinement (cont.)
calculatePay() BEGIN Set grossPay rate
hours. END.
getData() BEGIN Write name. Write date.
Write rate of pay. Write hours worked.
Write gross pay. END.
34A problem solution diagram for the student
payroll problem shows the abstract analysis and
stepwise refinement required for solving complex
problems.
35A class diagram for the StudentPayroll class
shows the data and method members that make up
the class.
36A method in Java is a procedure designed to
perform specific tasks, such as those performed
by an algorithm. In other programming languages,
methods might be referred to as subprograms,
functions, or subroutines.
37/ TestStudentPayroll.java Created on
April 21, 2002, 851 AM / Â / _at_author
Andrew C. Staugaard, Jr. _at_version 1.0
/ Â import staugIO.StaugIO
//FOR IO class StudentPayroll String name
"" //STUDENT NAME
String date "mm/dd/yyyy" //PAYROLL
DATE private double rate 0.0
//HOURLY RATE private double hours 0.0
//WEEKLY HOURS private double
grossPay 0.0 //WEEKLY GROSS PAY
private StaugIO io new StaugIO()
//INPUT/OUTPUT OBJECT Â
import StaugIO Class for GUI I/O
StudentPayroll Class
Class Data
38 //StudentPayroll() CONSTRUCTOR METHOD
public StudentPayroll() //END
StudentPayroll() //setData() METHOD
public void setData() name
io.readString("Enter the student name")
date io.readString("Enter the date in
mm/dd/yyyy format") rate
io.readDouble("Enter the hourly rate of pay")
hours io.readDouble("Enter hours that "
name " worked") //END setData()
//calculatePay() METHOD public void
calculatePay() grossPay rate
hours //END calculatePay()
//getData() METHOD public void getData()
io.writeInfo("Student " name
"\nDate " date
"\nRate " rate
"\nHours " hours
"\nGross Pay " grossPay) //END
getData() //END StudentPayroll CLASS
Constructor Method (special method required for
all classes)
Class Methods
39Application Class
Method main()
public class TestStudentPayroll public
static void main (String args)
StudentPayroll student new
StudentPayroll() student.setData()
student.calculatePay()
student.getData() Â //EXIT PROGRAM
System.exit(0) //END main() //END
TestStudentPayroll CLASS
Object Definition
Method Calls
40(No Transcript)