Finishing the Program with Four Steps
Once again, step three was not difficult. In fact, between writing steps one, two, and three, you probably still don't have a whole 30 minutes invested in this process. That's just one of the benefits here.
During most of an introductory programming course, you will be using methods to develop your programs. However, for the first few weeks before you learn about creating methods, the Six Step Programming Process will essentially end at this step (four); without further method development, steps five and six are not necessary. So, for a program development process that does not involve creating methods, this ends here for the time being.
If you are working through this with the web page, you must create a new class named in your Project_1/p1_package structure. This class will be named QuadClass_StepFour. You must then copy the contents of the main method in QuadClass_StepThree into the main method of your QuadClass_StepFour. At this point, you want to save what was stored as step three for your future review.Now you must again work your way through the program from top to bottom again in the iterative refinement process. This time, and under the circumstances that you will not be using methods, you will simply be following your previous instructions to write the code for your program.
Initialize the program
// initialize program
// initialize variables
// show title
// method: System.out.println
- You will be initializing variables as you write the program, but since you do not know at present which variables you might need, you will not be attending to that issue now
- However, you do need to output the title here, so you must write the code needed to do this, and it is obviously pretty easy
// initialize program
// initialize variables
// show title
// method: System.out.println
System.out.println( "QUADRATIC ROOTS PROGRAM\n" ); System.out.println( "=======================\n" );
- To create the title, use the System.out.println to print tht title, and then to print the underline, and then provide an extra endline character ( '\n' ) to the underline so it will make an extra space between the title and the input which occurs next. Note that println prints a string and then ends the line. In some other circumstances, print can be used which prints the string but does not end the line.
Get the coefficients from the user
// get coefficients from user
// get coefficient A
// method: JOptionPane.showInputDialog, Double.parseDouble
// get coefficient B // method: iostream JOptionPane.showInputDialog, Double.parseDouble
// get coefficient C // method: iostream JOptionPane.showInputDialog, Double.parseDouble
- This operation is simple in the sense that it is one action repeated three times, once for each coefficient. However, you will need three variables to hold the string input data from the user, and then three more variables to hold the actual numerical values acquired from the string. So before the input process is developed, declare the six needed variables, as shown here
// initialize program
// initialize variables
String strCoef_A, strCoef_B, strCoef_C;
double coef_A, coef_B, coef_C;
- Remember to check for errors or warnings after declaring these variables so no problems show up later
- Now you may prompt the user for input and convert the String input to a double value, as shown here. Remember to check for errors or warnings after typing each line. Don't sweat it if some of the code is not clear right now. Again, you have not been exposed to all of these code operations before, but when you do start working with them, you will be using appropriate structure
// get coefficients from user
// get coefficient A
// method: JOptionPane.showInputDialog, Double.parseDouble
strCoef_A = JOptionPane.showInputDialog( "Enter coefficient A: " );
coef_A = Double.parseDouble( strCoef_A );
// get coefficient B
// method: JOptionPane.showInputDialog, Double.parseDouble
strCoef_B = JOptionPane.showInputDialog( "Enter coefficient B: " );
coef_B = Double.parseDouble( strCoef_B );
// get coefficient A
// method: JOptionPane.showInputDialog, Double.parseDouble
strCoef_C = JOptionPane.showInputDialog( "Enter coefficient C: " );
coef_C = Double.parseDouble( strCoef_C );
- This will take care of the input operations
Process the quadratic roots
// Process the quadratic roots
// calculate the discriminant
// calculate the square root of the discriminant // method: Math.sqrt
// calculate the denominator
// calculate root one
// calculate root two
- There are several actions to be taken here, but that just provides an opportunity to practice with the code. The important thing about your present actions is that your design process has been completed. You now have only to follow the instructions you have provided for yourself
- For the first operation ("calculate the discriminant"), you have specified that you want to calculate the discriminant. This means you need to calculate the value under the radical. Since you will be calculating a discriminant, you must create the variable discriminant, as shown here
Update the variables
- You will need the discriminant variable, so it gets declared now.
// initialize program
// initialize variables
String strCoef_A, strCoef_B, strCoef_C;
double coef_A, coef_B, coef_C;
double discriminant;
- Compile after adding to this line. If everything is okay, move on to the next step
- Calculating the discriminant is a simple process, and is shown here
// Process the quadratic roots
// calculate the discriminant
discriminant = coef_B * coef_B - ( 4 * coef_A * coef_C );
- After you type in this code, remember to check for errors or warnings
- For the next operation ("calculate the square root of the discriminant"), the square root of the discriminant must be found. You will need a variable for this, so sqrtOfDisc is declared (above)
- Note that sqrtOfDisc must be a double value because the value returned from the Math.sqrt method you will use is supposed to be a double. Compile after you add this variable to make sure everything is okay
// initialize program
// initialize variables
String strCoef_A, strCoef_B, strCoef_C;
double coef_A, coef_B, coef_C;
double discriminant, sqrtOfDisc;
- Now that you have created a variable for acquiring the square root, go ahead and type the operational code, compiling immediately after
// calculate the square root of the discriminant
// method: Math.sqrt
sqrtOfDisc = Math.sqrt( discriminant );
- For the next operation ("calculate the denominator"), you have specified that you want to calculate the denominator of the quadratic equation. This means you will need to create the denominator variable, as shown
// initialize program
// initialize variables
String strCoef_A, strCoef_B, strCoef_C;
double coef_A, coef_B, coef_C;
double discriminant, sqrtOfDisc, denominator;
- The denominator calculation is also pretty simple, and is shown here. Remember to compile after typing the line
// calculate the denominator
denominator = 2 * coef_A;
- Finally, for the last root processing operations ("calculate root one" and "calculate root two"), you have specified that you want to calculate the two roots of the equation using the solution components you have already generated (i.e., the square root of the discriminant and the denominator). You must create the two root variables as shown here, remembering to compile after adding these variables
// initialize program
// initialize variables
String strCoef_A, strCoef_B, strCoef_C;
double coef_A, coef_B, coef_C;
double discriminant, sqrtOfDisc, denominator;
double rootOne, rootTwo;
- Note again that since the sqrtOfDisc and the other variables are double values, the two root variables need to be doubles since they will be acquiring data calculated using the sqrtOfDisc variable
- The operations for calculating the roots are shown here with the discriminant square root being added to one calculation and subtracted from the other. Compile after typing each of these statements
// calculate root one
rootOne = ( -coef_B + sqrtOfDisc ) / denominator;
// calculate root two
rootTwo = ( -coef_B - sqrtOfDisc ) / denominator;
- This will complete your root processing actions
Display the roots
// display user input
// method: System.out.println
// display root one
// method: System.out.println
// display root two
// method: System.out.println
- The actions are also pretty easy for output display, so you only have to output the descriptive text, the user input, and the root results, as shown
// display user input
// method: System.out.println
System.out.println( "For coefficients of " + coef_A
+ ", " + coef_B + ", and "
+ coef_C + ", " );
// display root one
// method: System.out.print
System.out.print( "root one is: " + rootOne );
// display root two
// method: System.out.println
System.out.println( ", and root two is: " + rootTwo );
- For this action, you did not create any new variables, so no other action needs to be conducted. Again however, make sure you compile after you have written this code
- Notice that the second output statement for rootOne uses .print instead of .println. This will cause the next output statement (i.e., the rootTwo output) to be on the same line.
End the program
// end program
// display program end
// method: System.out.println
- This is easily resolve by placing the output statement, as shown here
// end program
// display program end
// method: System.out.println
System.out.println( "\nEnd Program" );
- Note the end line character ( '\n' ) prior to the "End" text. This is the same as pressing ENTER on your keyboard and makes an extra vertical space before printing "End Program". It mostly keeps the end of the program from being jammed up against the program output
- As previously stated, when you are writing programs without methods (which won't last very long in most introductory programming courses), your work ends at step four
- step one is very short and only asks you to show a brief high-level overview of your program
- step two is also pretty short, but includes your expansion of step one with specific actions that will create your solution; nevertheless, it is still very important that you have created all the steps for solving the problem before you leave this step. When you are done here, you will usually have a comment line for every line of code you will write; this leads to just filling in one line of code at a time when the time comes
- step three is also pretty short, but you must still identify places where you will be using methods, and you will have to add import operations as needed to provide access to external classes and utilities
- in step four, you actually write out all the code required of the program; step four is used to write the main method code whether you create your own methods or not. As you have seen, step four is pretty easy because you already have instructions as to what you need to do; you just have to follow them
- Once you have finished step four, you will have a working program; you only need to run it and verify its correctness. In the future, when you get to step four, you will still implement or write the code for the main method exactly like you did here, but you will still need to design and implement the code for your supporting methods, which is what you will be doing in steps five and six
- It is pretty impressive to just tackle these little parts of the program one at a time, and when you are done, having a working program. However, it is not magic. It is simply good quality, well-organized, and structured programming. This will make your programs better, and it will make your programming process faster and easier
Some more notes:
- This is not difficult. It is structured programming. And every time you practice it, you will get better and faster at it
- Reminder if you haven't already done it: In order to conduct this part of the process, you must create a new class named in your Project_1/p1_package structure. This class will be named QuadClass_StepFour. You must then copy the contents of the main method in QuadClass_StepThree into the main method of your QuadClass_StepFour. You want to keep your step three program exactly as it was so that if there are questions in the future about your overall strategy, you can go back and review the code.
- Remember as you type in any parts of your code, you must still make sure the program compiles without errors or warnings. As previously noted, this is easy with Eclipse since it is constantly checking your code. The only thing that can make this process take too long is if you write several lines of code at once without checking for errors or warnings, and then spend way too long trying to figure out what all the errors mean
To see the step four process in action, watch this video; then develop your step four code with or without the video as needed in your own IDE.