Example 1: Single Decision Algorithm ( if )
This example weaves a simple if decision statement
into our 5-Step Solution Algorithm.
Problem Statement
Design a program that will prompt the end-user for three numbers, accept those numbers as input, sort them into ascending sequence and output them to the console.
1. Nouns and Verbs
Identify the nouns and verbs in the problem statement.
PROBLEM STATEMENT: Design a program that will prompt the end-user for three numbers, accept those numbers as input, sort them into ascending sequence and output them to the console. Nouns: first number, second number, third number Verbs: prompt, accept, sort, output
2. Defining Diagram
Organizing nouns and verbs into inputs, processes and outputs in order to create a defining diagram.
INPUTS: first number second number third number PROCESSING: prompt user for numbers accept three numbers sort three numbers output three numbers OUTPUTS: display first number, second number and third number
3. Solution Algorithm
Create a solution algorithm based on the defining diagram.
public Constructor(){
call readAndSortNumbers();
}
private method readAndSortNumbers(){
declare and initialize int numOne that will hold end-user input
declare and initialize int numTwo that will hold end-user input
declare and initialize int numThree that will hold end-user input
instantiate Scanner object using input as its variable
print to the console "Please enter the first number"
if(valid input){
save the input.nextInt() value into the numOne variable
}
print to the console "Please enter the second number"
if(valid input){
save the input.nextInt() value into the numTwo variable
}
print to the console "Please enter the third number"
if(valid input){
save the input.nextInt() value into the numThree variable
}
display a message using the variables numOne, numTwo, numThree in their original order
if (numOne > numTwo){
swap = numOne
numOne = numTwo
numTwo = swap
}
if (numTwo > numThree){
swap = numTwo
numTwo = numThree
numThree = swap
}
if (numOne > numTwo) {
swap = numOne
numOne = numTwo
numTwo = swap
}
display a message using the variables numOne, numTwo, numThree in ascending sort order
}//end of readAndSortNumbers()
4. Test Plan
Create a test plan for the algorithm.
VARIABLES: numOne numTwo numThree TEST CASE 1: INPUT VALUES: 3, e, 7 EXPECTED RESULT: The program uses zero for sorting numTwo and numThree: 0 0 3 ACTUAL RESULT: The program never gives me the chance to enter a value for numThree. It uses zero for sorting numTwo and numThree: 0 0 3 TEST CASE 2: INPUT VALUES: 33, 11, y EXPECTED RESULT: The program uses zero for sorting numThree: 0 11 33 ACTUAL RESULT: The program uses zero for sorting numThree: 0 11 33 TEST CASE 3: INPUT VALUES: -3, 0, 3 EXPECTED RESULT: -3, 0, 3 ACTUAL RESULT: -3, 0, 3
5. Java Code
Write the Java source code based on the solution algorithm. Test your code using the test plan.
Below is a Java code snippet. A working Java solution must also include a class, constructor, as well as the main method and necessary import statements. Please study the code. Better yet, create a working Java project in NetBeans.
REALLY IMPORTANT! The hasNextInt() methods are to get you used to end-user validation. As is, the results are not what you may expect if the end-user does not enter an integer. Also, this code does not give the end-user the opportunity to try again. These details were left out in the interest of clarity for the topic at hand...Simple if statements.
private void readAndSortNumbers() {
//declare and initialize variables
int numOne = 0;
int numTwo = 0;
int numThree = 0;
int swap = 0;
//instantiate Scanner object using input as its variable
Scanner input = new Scanner(System.in);
//print to the console "Please enter the first number"
System.out.print("Please enter your first number: ");
//if(valid input)
if(input.hasNextInt()){
numOne = input.nextInt();
}
//print to the console "Please enter the second number"
System.out.print("Please enter your second number: ");
//if(valid input)
if(input.hasNextInt()){
numTwo = input.nextInt();
}
//print to the console "Please enter the third number"
System.out.print("Please enter your third number: ");
//if(valid input)
if(input.hasNextInt()){
numThree = input.nextInt();
}
//display a message using the variables numOne, numTwo, numThree in the original order
System.out.println("You entered the numbers in this order: " + numOne + " " +
numTwo + " " + numThree);
// Sort the numbers
if (numOne > numTwo) {
swap = numOne;
numOne = numTwo;
numTwo = swap;
}
if (numTwo > numThree) {
swap = numTwo;
numTwo = numThree;
numThree = swap;
}
if (numOne > numTwo) {
swap = numOne;
numOne = numTwo;
numTwo = swap;
}
//display a message using the variables numOne, numTwo, numThree in ascending sort order
System.out.println("Here are your numbers in ascending sort order: " + numOne + " " +
numTwo + " " + numThree);
}//end of readAndSortNumbers