Download presentation
Presentation is loading. Please wait.
2
Moving To Code 3
3
More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program 3
4
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
5
IPO Chart for the Floating Point Adder 3 InputProcessingOutput first number second number Processing items: none 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 sum
6
Assigning Names, Data Types, and Initial Values to the IPO Items §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 3
7
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 3 4
8
Assigning Names, Data Types, and Initial Values to the IPO Items 3 IPO Chart InformationJava instructions Input first number second number Processing Output 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 int firstNumber = 0; int secondNumber = 0; int sum = 0;
9
Assigning Names, Data Types, and Initial Values to the IPO Items 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 3
10
Translating the Algorithm Steps into Java Code §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 3
11
Translating algorithm steps into Java Code 3 IPO Chart InformationJava instructions Input first number second number Processing Output sum Algorithm 1. enter the first number and the second number 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();
12
Translating algorithm steps into Java Code 3 IPO Chart InformationJava instructions 2. calculate the sum by adding the first number to the second number 3. display the sum sum = firstNumber + secondNumber; System.out.println(“The sum total is ” + sum);
13
Desk-Checking the Program §Desk-check every program to make sure that each step in the algorithm was translated correctly 3
14
Evaluating and Modifying the Program §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 3
15
Evaluating and Modifying the Program §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 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.