Section 8e

Step Three - Implementing the main Method


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Systematic and Structured Programming, Part 2

Step Four - (Nearly) Finishing the Program With Methods

Going through step three took a little longer because you had to analyze what the program needs were, and specify what would need to happen to resolve those needs. Still, you were writing specifications and locating actions, and other than creating the stub methods, there was no coding, so the process should have been pretty smooth.

In this chapter, you will take the method specifications and locations and turn them into a program. The methods will not be implemented, but your main program will be completed as a result of this step. This, by the way, is exactly what you also did in the Four Step Programming Process. In essence, you will be writing the real program, but in the Six Step Programming Process, you are assuming the methods you have specified but not yet implemented will do what you said they would do.

By putting off the method-related details, you can focus on one problem, which at this point is the main program. Later, you can continue to focus on single problems as you flesh out the individual methods. The best part of this whole system is that you will always have developed instructions for yourself to follow in each succeeding step. This demonstrates the power of abstracting the process.

If you are working through this with the web page, you should copy the code from your QuadClass__StepThree.java to your new QuadClass_StepFour.java file so that you don't overwrite your third step. You will be reminded again to make this change at the end of this web page.

Now, you must work your way through the program from top to bottom again in the iterative refinement process. This time, you will use the methods in your program with the assumption that they will work (when you get them written)

Start here

Initialize the program

// initialize program

// initialize variables

// show title
// method: displayTitle

This is easy. You still won't know what your variables are yet, so you can put off working with them for a little bit. You do know what it takes to print the title, so you can place that here

// initialize program

// initialize variables

// show title
// method: displayTitle
displayTitle();

Now compile or check for the red icon in your Eclipse to make sure the code is correct


Get the coefficients from the user

// get coefficients from user

// get coefficient A
// method: promptForCoefficient

// get coefficient B
// method: promptForCoefficient

// get coefficient C
// method: promptForCoefficient

While the displayTitle method didn't do any processing you have now come to a place where your own processing methods will be used, but you will go ahead and write your program as if the methods were fully operational

Before you can actually use the promptForCoefficient method in the code, you will need to declare the variables necessary for this operation. The variable declaration process is shown here

// initialize program

// initialize variables
int coefA, coefB, coefC;

Compile after declaring the variables

Then you simply place the method in the locations where you previously specified they would go, as shown. If you forget what the parameter should be, you can go down to the specification area and check. That's handy.

// get coefficients from user

// get coefficient A
// method: promptForCoefficient
coefA = promptForCoefficient ( 'A' );

// get coefficient B
// method: promptForCoefficient
coefB = promptForCoefficient ( 'B' );

// get coefficient C
// method: promptForCoefficient
coefC = promptForCoefficient ( 'C' );

After you type each one of these lines, make sure you compile or check for the red icon in Eclipse.


Process the quadratic roots

// Process quadratic roots

// calculate the discriminant
// method: calcDiscriminant

// calculate the square root of the discriminant
// method: Math.sqrt

// calculate the denominator
// method: calcDenominator

// calculate roots

// calculate root one
// method: calcRoot

// calculate root two
// method: calcRoot

For the calcRoot method, you must again declare the variable needed to accept the discriminant, as shown

// initialize program

// initialize variables
int coefA, coefB, coefC, discriminant;

Compile after adding the discriminant variable

Now type the code in the main program, as shown here

// calculate the discriminant
// method: calcDiscriminant
discriminant = calcDiscriminant( coefA, coefB, coefC );

Notice that the actual parameters are coefA, coefB, and coefC while the formal parameters are cofA, cofB, and cofC. These are purposely different in this example -- although they don't have to be -- to show that the variable names are unrelated. What matters is that the data will be copied from the actual parameter (e.g., coefA) and passed to the formal parameter (e.g., cofA) where cofA will be used in the method as needed. When the called method has completed its operations, cofA will be out of scope and coefA will be the appropriate variable to be used in the calling method. Make sure you understand this difference

Remember to compile or check the for the red icons in Eclipse after typing this line

The next method you have specified is the square root method, but since you are not creating this method, you only need to use it

First, create the variable needed to acquire the data, as shown, and then compile. Note that this variable is a double value since the sqrt method returns double quantities

// initialize program

// initialize variables
int coefA, coefB, coefC, discriminant;
double discSquareRoot;

Now use the method in your program, and then compile or check for the red Eclipse icons

// calculate the discriminant square root
// method: sqrt
discSquareRoot = sqrt( (double)discriminant );

The next method you specified is the calcDenominator method

As before, start by first declaring any needed variables, and then compiling

// initialize program

// initialize variables
int coefA, coefB, coefC, discriminant;
int denominator;
double discSquareRoot;

Then you can write the method in the appropriate place in the program

// calculate the denominator
// method: calcDenominator
denominator = calcDenominator( coefA );

The next method you need is calcRoot

Create the necessary variables, rootOne and rootTwo

// initialize program

// initialize variables
int coefA, coefB, coefC, discriminant;
int denominator;
double discSquareRoot, rootOne, rootTwo;

Then write the method in the program. In this case, you are using the same method for two operations, a process called code reusability.

// calculate roots

// calculate root one
// method: calcRoot
rootOne = calcRoot( coefB, denominator, discSquareRoot );

// calculate root two
// method: calcRoot
rootTwo = calcRoot( coefB, denominator, -discSquareRoot );

Note that the positive and negative square roots of the discriminant are sent to the method. The method will do the same thing for both, but you will have accomplished getting the "plus or minus" effect out of the process

Display the roots

// display roots

// display user input
// method: printString

// display root one
// method: displayRoot

// display root two
// method: displayRoot

Displaying the user input values with printString would look like the following:

// display roots

// display user input
// method: printString
conIO.printString( "For the coefficients " + coefA + ", and " + coefB
", and " + coefC + ", the roots will be:" );
conIO.printEndline();

The display user input values part will end the line in preparation for displaying the roots. Finally, the method displayRoot is needed.

Create any variables that are needed. In this case, you will need a variable for the root number, so this is added here

// initialize program

// initialize variables
int coefA, coefB, coefC, discriminant;
int denominator, rootNumber;
double discSquareRoot, rootOne, rootTwo;

Place the displayRoot methods in the main method as shown

// display roots

// display user input
// method: printString
conIO.printString( "For the coefficients " + coefA + ", and " + coefB
", and " + coefC + ", the roots will be:" );
conIO.printEndline();

// display root one
// method: displayRoot
rootNumber = 1;
displayRoot( rootNumber, rootOne );

// display root two
// method: displayRoot
rootNumber = 2;
displayRoot( rootNumber, rootTwo );

End the program

// end program

// display program end
// method: printString, printEndline

This is another function that is already written for you, so you only need to place it in the main method, as shown

// end program

// display program end
// method: printString, printEndLine
conIO.printString( "Program End" );
conIO.printEndline();

Quick Review

Wrapping Up Step Four

This step puts your main method code into play. The program does not run yet, but you can be confident that it will when you have completed the last two steps. Programming is about science, technology, and structure; those folks that try a little of this and a little of that until they succeed (or think they have succeeded) can not make it in this discipline.


To see the step four process in action, watch this video; then develop your step four code with or without the video as needed.