Assigning Names, Data Types, and Initial Values to the IPO Items 3

advertisement
3
Moving To Code
More on the Problem-Solving
Process
3
 The final step in the problem-solving process is
to evaluate and modify (if necessary) the program
Coding the Algorithm into
a Program
Problem Specification:
Create a program that prompts the
user for two integers, adds the two
numbers together, then displays the
result.
3
IPO Chart for the Floating
Point Adder
Input
Processing
Output
first number
second number
Processing items:
none
sum
Algorithm:
1. enter the first number
and the second number
2. calculate the sum by
adding the first number to the
second number
3. display the sum
3
Assigning Names, Data Types,
and Initial Values to the IPO Items
3
 Programmers use the information in the IPO chart
to code the algorithm
 First, the programmer assigns a descriptive name
to each unique input, processing, and output item
listed in the IPO chart
 In most programming languages, these names
can contain only letters, numbers, and the
underscore; they cannot contain punctuation
characters or spaces
 Most Java programmers use lowercase letters for
the names, capitalizing the first letter of
subsequent words if necessary
Assigning Names, Data Types,
and Initial Values to the IPO Items
 The programmer also assigns a data type to each
input, processing, and output item
 The data type specifies the type of data each item
represents
 In addition to assigning both a name and data type to
each input, processing, and output item, the
programmer also assigns an initial value
 This is referred to as initializing the item
 Variables are simply computer memory locations that
the program will use while running
4
3
Assigning Names, Data Types,
and Initial Values to the IPO Items
IPO Chart Information
Input
first number
second number
Processing
Output
Java instructions
int firstNumber = 0;
int secondNumber = 0;
sum
int sum = 0;
Algorithm
1. enter the first number and the
second number
2. calculate the sum by adding the
first number to the second number
3. display the sum
3
Assigning Names, Data Types,
and Initial Values to the IPO Items
3
 The word double, which must be typed using
lowercase letters, is a keyword in Java
 A keyword is a word that has a special meaning in a
programming language
 Notice that each of the variable declaration
instructions ends with a semicolon (;)
 The instruction to declare a variable is considered a
statement, which is simply a Java instruction that
causes the computer to perform some action after it is
executed, or processed, by the computer
 All Java statements must end with a semicolon
Translating the Algorithm Steps
into Java Code
3
 After assigning a name, data type, and initial
value to each input, processing, and output item,
the programmer then translates each step in the
algorithm into one or more Java instructions
 In Java, you use streams, which are just
sequences of characters, to perform standard
input and output operations
 The standard output stream is called System.out
which refers to the computer screen
Translating algorithm steps
into Java Code
IPO Chart Information
Input
first number
second number
Processing
Output
sum
Algorithm
1. enter the first number and the
second number
3
Java instructions
int firstNumber = 0;
int secondNumber = 0;
int sum = 0;
Scanner in = new
Scanner(System.in);
System.out.print(“Enter
first integer: ”);
firstNumber = in.nextInt();
System.out.print(“Enter
second integer: ”);
secondNumber = in.nextInt();
Translating algorithm steps
into Java Code
IPO Chart Information
3
Java instructions
2. calculate the sum by adding the sum = firstNumber +
secondNumber;
first number to the second number
3. display the sum
System.out.println(“The sum
total is ” + sum);
3
Desk-Checking the Program
 Desk-check every program to make sure that
each step in the algorithm was translated
correctly
Evaluating and Modifying
the Program
3
 The final step in the problem-solving process is
to evaluate and modify (if necessary) the
program
 Programmers often refer to this as the “testing
and debugging” step
 Testing refers to running (executing) the
program, along with sample data, on the
computer
 Debugging refers to the process of locating and
removing any errors, called bugs, in a program
Evaluating and Modifying
the Program
3
 Program errors can be either syntax errors
or logic errors
 You create a syntax error when you enter
an instruction that violates the
programming language’s syntax
 Logic errors, on the other hand, are much
more difficult to find, because they can
occur for a variety of reasons
Download