You are on page 1of 44

Chapter 2

MISY 2312:
Introductory Programming
for Information Systems

Dr. Washah

1
Chapter 2

Chapter 2
Problem Solving
Chapter 2
Computer Language Levels

• High-level language: A language that people can read,


write, and understand
– A program written in a high-level language must be translated
into a language that can be understood by a computer before it
can be run
• Machine language: A language that a computer can
understand
• Low-level language: Machine language or any language
similar to machine language
• Compiler: A program that translates a high-level
language program into an equivalent low-level language
program
– This translation process is called compiling
Chapter 2

The 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

4
Chapter 2

• Define the Problem


The 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.

5
Chapter 2

• Plan the Solution


When developing computer software,
the planning stage is implemented
using a collection of algorithms and
class diagrams.

6
Chapter 2

An algorithm is a series of step-by-step


instructions that produces a solution to a
problem using Englishlike statements,
called pseudocode, 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. Algorithms are usually coded
as methods within a Java class.
7
Chapter 2

A class diagram is a diagram that shows


the data and methods which belong to a
given class.

8
Chapter 2

• Code the Program


Coding involves the actual writing of
the program in a formal programming
language. Once a language is
chosen, the program is written, or
coded, by translating your algorithm
steps into the formal language code.

9
Chapter 2

• Debug the Program


Realize that you have an error.
Locate and determine the cause of
the error.
Fix the error.
Desk-check the program,
Compile the program.
Run the program.
Debug the program using a debugger.
10
Chapter 2

A 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
Chapter 2

• Document the Program


The final program documentation is
simply the recorded result of the
problem definition, solution planning,
coding, testing results, debugging
results, and user instructions.

12
Chapter 2

• Maintain the Program

1. corrective maintenance

2. enhancement maintenance

3. forced maintenance.

13
Chapter 2

Problem 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.

14
Chapter 2

Stepwise 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.

15
Chapter 2
Software development cycle

• Specifications – What do you want to do?


• Design – How will you do what you want?
• Implement – Code it.
• Test – Check if it works.
• Maintain – School projects don’t usually
make it this far.

Bugs are cheaper earlier in the cycle!


Chapter 2

Problem
 
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.
 

17
Chapter 2

Defining 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

18
Chapter 2

Initial 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.

19
Chapter 2
First 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.

20
Chapter 2
irst 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.
21
Chapter 2

main()

setData() calculatePay() getData()

A problem solution diagram for the sales tax problem shows the abstract analysis
and stepwise refinement required for solving complex problems.

22
Chapter 2

StudentPayroll

name
date
rate
hours
grosspay

setData()

calculatePay()
getData()

A class diagram for the SalesTax class shows the data and method members
that make up the class.

23
Chapter 2

A 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.

24
Chapter 2
Fundamental Building Blocks of
Programs
• There are two basic aspects of
programming: data and instructions. To
work with data, you need to understand
variables and types; to work with
instructions, you need to understand
control structures and subroutines. You'll
spend a large part of the course becoming
familiar with these concepts.

25
Chapter 2
• A variable is just a memory location (or
several locations treated as a unit) that has
been given a name so that it can be easily
referred to and used in a program. The
programmer only has to worry about the
name; it is the compiler's responsibility to
keep track of the memory location. The
programmer does need to keep in mind that
the name refers to a kind of "box" in
memory that can hold data, even if the
programmer doesn't have to know where in
memory that box is located.
26
Chapter 2

In Java and most other languages, a


variable has a type that indicates
what sort of data it can hold.

27
Chapter 2
Programming languages always have
commands for getting data into and out of
variables and for doing computations with
data.
• interest = principal * 0.07;

28
Chapter 2

• There are also "input commands" for


getting data from the user or from files on
the computer's disks and "output
commands" for sending data in the other
direction.

29
Chapter 2

• A program is a sequence of instructions. In


the ordinary "flow of control," the computer
executes the instructions in the sequence
in which they appear, one after the other.

30
Chapter 2
Objects and Object-oriented
Programming
• During the 1970s and into the 80s, the
primary software engineering methodology
was structured programming.

31
Chapter 2

• In bottom-up design, the approach is to


start "at the bottom," with problems that
you already know how to solve (and for
which you might already have a reusable
software component at hand). From there,
you can work upwards towards a solution
to the overall problem.

32
Chapter 2

• The reusable components should be as


"modular" as possible. A module is a
component of a larger system that
interacts with the rest of the system in a
simple, well-defined, straightforward
manner.

33
Chapter 2

This latest approach is called object-


oriented programming, often abbreviated as
OOP.

34
Chapter 2
Objects and Methods

• Java is an object-oriented programming


(OOP) language
– Programming methodology that views a
program as consisting of objects that interact
with one another by means of actions (called
methods)
– Objects of the same kind are said to have the
same type or be in the same class
Chapter 2
Java Application Programs

• There are two types of Java programs:


applications and applets
• A Java application program or "regular" Java
program is a class with a method named main
– When a Java application program is run, the run-time
system automatically invokes the method named
main
– All Java application programs start with the main
method
Chapter 2
Applets

• A Java applet (little Java application) is a Java


program that is meant to be run from a Web
browser
– Can be run from a location on the Internet
– Can also be run with an applet viewer program for
debugging
– Applets always use a windowing interface
• In contrast, application programs may use a
windowing interface or console (i.e., text) I/O
Chapter 2
A Sample Java Application Program

1-38
Chapter 2
System.out.println

• Java programs work by having things


called objects perform actions
– System.out: an object used for sending
output to the screen
• The actions performed by an object are
called methods
– println: the method or action that the
System.out object performs
Chapter 2
Objects that contain the same type of data
and that respond to the same messages in
the same way belong to the same class

40
Chapter 2
/*
* TestStudentPayroll.java
*
* Created on April 21, 2002, 8:51 AM
*/
 
/**
import StaugIO Class for GUI I/O
*
* @author Andrew C. Staugaard, Jr.
* @version 1.0
*/ StudentPayroll Class
 
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 Class Data
private double hours = 0.0; //WEEKLY HOURS
private double grossPay = 0.0; //WEEKLY GROSS PAY
private StaugIO io = new StaugIO(); //INPUT/OUTPUT OBJECT
 

Information Systems Programming with Java, 2nd edition 41


Andrew C. Staugaard, Jr.
Chapter 2
//StudentPayroll() CONSTRUCTOR METHOD
public StudentPayroll()
{
Constructor Method
}//END StudentPayroll() (special method required for
//setData() METHOD all classes)
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() Class Methods
//getData() METHOD
public void getData()
{
io.writeInfo("Student: " + name
+ "\nDate: " + date
+ "\nRate: $" + rate
+ "\nHours: " + hours
+ "\nGross Pay: $" + grossPay);
}//END getData()
}//END StudentPayroll CLASS

Information Systems Programming with Java, 2nd edition 42


Andrew C. Staugaard, Jr.
Chapter 2
Application Class
Method main()

public class TestStudentPayroll


{ Object Definition
public static void main (String args[])
{

StudentPayroll student = new StudentPayroll();


student.setData();
student.calculatePay(); Method Calls
student.getData();
 
//EXIT PROGRAM
System.exit(0);
}//END main()
}//END TestStudentPayroll CLASS

Information Systems Programming with Java, 2nd edition 43


Andrew C. Staugaard, Jr.
Chapter 2

44

You might also like