Section 8g

Step Six - Implementing the Methods


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Systematic and Structured Programming, Part 2

Step Six - Finishing the Job

Like step two for the main program where you solved the primary problem, step five is a very important part of the process since you solved all the smaller supporting problems that support the overall solution. Like all of the steps, it should not have taken a long time, and like all the previous steps, if you did a good job in step five, this next (and final) step will be made that much easier. This last part of the programming process simply involves following the instructions you developed in step five and completing and testing the code that you generate.

If you are working through this with the web page, you should change your program file name from QuadClass_StepFive.java to QuadClass_StepSix.java now so that you don't overwrite your fifth step. You will be reminded again to make this change at the end of this web page. Note that the version numbers are not added to the file names in this text since you will be in charge of knowing when you update your files.

As in step five, your final iterative pass will be through the stub methods, developing and testing the program code as you progress. Once again, there is little or no need to look back at previous operations. Once you have implemented the code for all the methods, your program should work.

In most cases, there is no particular order to follow with implementing the methods, however if you have methods that conduct input and/or output operations, it is sometimes a good idea to write the I/O methods first so that as your other methods are developed, you can use your I/O methods to conduct interim tests on your program. For this very simple program, it will work fine to implement the methods in the order they are found.

Start here

printTitle

From the step five process, the printTitle looks like the following:

void displayTitle()
{
// initialize method/variables

// print title
// method: printString, printEndline

// print underline
// method: printString, printEndline

// print extra endline
// method: printEndline
}

You will simply fill in the code as specified by your step five specifications

void displayTitle()
{
// initialize method/variables

// print title
// method: printString, printEndline
conIO.printString( "QUADRATIC ROOT CALCULATOR" );
conIO.printEndline();

// print underline
// method: printString, printEndline
conIO.printString( "=========================" );
conIO.printEndline();

// print extra endline
// method: printEndline
conIO.printEndline();
}

Compile or check for red icons after every line or two but definitely compile before going on to the next method

promptForCoefficient

Like the printTitle method, From the step five actions, the method in step three form looks like the following:

int promptForCoefficient( char coefLetter )
{
// initialize method/variables

// prompt user for coefficient
// method: promptForInt

// return user input
return 0; // temporary stub return
}

You have only to fill in the code necessary to respond to your program instructions:

int promptForCoefficient( char coefLetter )
{
// initialize method/variables
int returnedCoef;

// prompt user for coefficient
// method: promptForInt
returnedCoef = conIO.promptForInt( "Enter coefficient " + coefLetter
+ ": " );

// return user input
return returnedCoef;
}

Remember to compile or check those red icons after every line or two of code

calcDiscriminant

Like many well-modularized methods, the instructions and the code for this method are simple. Here are the step three comments for this method:

int calcDiscriminant( int cof_A, int cof_B, int cof_C )
{
// initialize method/variables

// calculate the discriminant

// return the discriminant
return 0; // temporary stub return
}

And here is the code for this method:

int calcDiscriminant( int cof_A, int cof_B, int cof_C )
{
// initialize method/variables
int disc;

// calculate the discriminant
disc = cof_B * cof_B - ( 4 * cof_A * cof_C );

// return the discriminant
return disc;
}

While this is a pretty easy program to write, development of very complicated programs can be this easy or nearly this easy if you have created an effective, modular solution before you started coding

Remember to compile and/or check for those red icons.

calcDenominator

Here are the design statements for this method:

int calcDenominator( int cof_A )
{
// initialize method/variables

// calculate the denominator

// return the denominator
return 0; // temporary stub return
}

And here is the code:

int calcDenominator( int cof_A )
{
// initialize method/variables
int denominator;

// calculate the denominator
denominator = 2 * cof_A;

// return the denominator
return denominator;
}

Remember that you must compile or check for those red icons after every line or two of written code to keep from piling up problems that will slow you down later

calcRoot

Here are design steps for this method:

double calcRoot( int cof_B, int denom, double discSqrRoot )
{
// initialize method/variables

// calculate the numerator

// calculate the root

// return the root
return 0.0; // temporary stub return
}

And here is the code:

double calcRoot( int cof_B, int denom, double discSqrRoot )
{
// initialize method/variables
double numerator, rootSolution;

// calculate the numerator numerator = -cof_B + discSqrRoot;

// calculate the root
rootSolution = numerator / denom;

// return the root
return rootSolution;
}

displayRoot

Here are the design statements for this method:

void displayRoot( int rootNum, double rootValue )
{
// initialize method/variables

// show root display

// display root number
// method: printString, printEndline

// display root value
// method: printString, printEndline

// end line
// method: printEndline
}

And here is the code. Note that we found that we did not need the printEndline method for the display root number operation:

void displayRoot( int rootNum, double rootValue )
{
// initialize method/variables

// show root display

// display root number
// method: printString
conIO.printString( "Root number " + rootNum );

// display root value
// method: printString, printEndline
conIO.printString( " is: " + rootValue );
conIO.printEndline();

// end line
// method: printEndline
conIO.printEndline
}

And once again, keep compiling


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


Wrapping Up Step Six (and the Program)

While it may seem miraculous, it is very likely that your program will compile and run the first time you try it, even if it is much more complicated and longer than the one used here as an example. Even if it doesn't run correctly, or seems to have some problems, the debugging process has been made incredibly simple since you should be able to identify any module that isn't working correctly

Okay, so there is one more thing to do. Try this again - practice writing this program two or three times. It will never be particularly difficult, but you will get faster and better at it if you practice a few times. And then go ahead: Bask in the Glory

Run the Program

One of the most powerful outcomes of using this six step programming process is that when you are done writing your program, you rarely have to go back

The program just completed should run the first time, for the following reasons:

  1. you solved the problem before you wrote the program; you even solved the small problems related to the modules before you wrote that code

  2. you wrote the overview of the program in a few simple statements before you went deeply into it (step one); you made sure the problem was solved before you moved on

  3. you took your original overview and, following your own simple instructions, created all of the solution as pseudocode statements (step two); again, you looked over your work and made sure that your statements would solve the problem

  4. you identified modules (i.e., methods) that would contribute toward parts of the program that needed to be conducted (step three); you made sure that the modules and components of the program would solve the problem before you went on, even if you didn't know exactly what would happen in each module

  5. You wrote the code for the main method knowing it would be fully correct because you knew that your design solved the problem (step four)

  6. you reused the first three steps to design the operations of each of your methods (step five)

  7. you finished the process by filling in some fairly simple code solutions to some fairly simple problems (step six), and because the modules were narrowly focused, they never really got too complicated

This is pretty much all there is to it. The key, as stated before, is to never solve big problems. Break the program into smaller quantities, and solve each item one at a time. No program will ever be too big to handle if you follow this process

Wrapping Up the Final Design Component

In order to conduct this part of the process, you must remember to save your QuadClass_StepFive.java file to QuadClass_StepSix.java before you make any changes. You want to keep your step five 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.