Section 2f

Step Four


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

    

// initialize program

// initialize variables

// show title
// method: System.out.println
System.out.println( "QUADRATIC ROOTS PROGRAM\n" ); System.out.println( "=======================\n" );

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

// initialize program

// initialize variables
String strCoef_A, strCoef_B, strCoef_C;
double coef_A, coef_B, coef_C;

// 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 );

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


Update the variables


// initialize program

// initialize variables
String strCoef_A, strCoef_B, strCoef_C;
double coef_A, coef_B, coef_C;
double discriminant;

// Process the quadratic roots

// calculate the discriminant
discriminant = coef_B * coef_B - ( 4 * coef_A * coef_C );

// initialize program

// initialize variables
String strCoef_A, strCoef_B, strCoef_C;
double coef_A, coef_B, coef_C;
double discriminant, sqrtOfDisc;

// calculate the square root of the discriminant
// method: Math.sqrt
sqrtOfDisc = Math.sqrt( discriminant );

// initialize program

// initialize variables
String strCoef_A, strCoef_B, strCoef_C;
double coef_A, coef_B, coef_C;
double discriminant, sqrtOfDisc, denominator;

// calculate the denominator
denominator = 2 * coef_A;

// 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;

// calculate root one
rootOne = ( -coef_B + sqrtOfDisc ) / denominator;

// calculate root two
rootTwo = ( -coef_B - sqrtOfDisc ) / denominator;

Display the roots

// display user input
// method: System.out.println

// display root one
// method: System.out.println

// display root two
// method: System.out.println

// 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 );


End the program

// end program
// display program end
// method: System.out.println

// end program
// display program end
// method: System.out.println
System.out.println( "\nEnd Program" );

Some more notes:


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.