Systematic and Structured Programming
Step Three - Identifying and Locating the Methods
Once again, step two was not difficult. In fact, between writing steps one and two, you probably still don't have a whole 20 minutes invested in this process. That's just one of the benefits here.
Once again (again), you will also find that step three is just an expansion of step two, although you will be taking some new actions here. Mostly however, you will just be filling in areas that are guided by what you wrote in your previous steps.
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_StepThree. You must then copy the contents of the main method in QuadClass_StepTwo into the main method of your QuadClass_StepThree. At this point, you want to save what was stored as step two for your future review.
Now, you must work your way through the program from top to bottom following the iterative refinement process. This time, you will be identifying the need for, and the use of, methods in your program, but because you are not required to develop your own methods, you will not be creating any new methods in this activity. You will only use those developed by others.
Start here (iteratively)
Initialize the program
// initialize program
// initialize variables
// show title
- There is still very little to do here, but showing the title does require the use of the Java output process.
- Note for all the code components you don't recognize right now, you will be learning about in later topics. For the time being, you only need to see them used, and when the lesson comes along, you will already be familiar, and you will know how to integrate your learning into a program.
- So, you will specify the use of the output operation under the "show title" statement, and your code will look like the following:
// initialize program
// initialize variables
// show title
// method: System.out.println
Get the coefficients from the user
// get coefficients from user
// get coefficient A
// get coefficient B
// get coefficient C
- You have specified that you want to get the three coefficients from the user. This will require a new method from the JOptionPane class called showInputDialog. Since this input operation brings in a string and the input needed is a double value, the Double.parseDouble method will also be required. To be able to use the JOptionPane class, an import statement will be needed near the top of the program; the JOptionPane is part of the javax.swing group of utiltities so this is prepended to the JOptionPane identifier. It will go right under the package statement as shown here
package p1_package;
import javax.swing.JOptionPane;
// get coefficients from user
// get coefficient A
// method: JOptionPane.showInputDialog, Double.parseDouble
// get coefficient B
// method: JOptionPane.showInputDialog, Double.parseDouble
// get coefficient C
// method: JOptionPane.showInputDialog, Double.parseDouble
Process the quadratic roots
// Process the quadratic roots
// calculate the discriminant
// calculate the square root of the discriminant
// calculate the denominator
// calculate roots
// calculate root one
// calculate root two
- With one exception, each of the actions required here is "just" a math operation. Later on, it will be interesting to create methods for these, but since you are not at that point yet, you will plan on using mathematical operations to conduct these specified operations. For that reason, there is no need to create any methods here
- However, there will be a need for calculating the square root which requires another method that can be found in a library. The sqrt method is found in the Math class so the program will be updated in two places. First, you must note that you are using the square root method in the design statements, as follows:
// Process the quadratic roots
// calculate the discriminant
// calculate the square root of the discriminant
// method: Math.sqrt
// calculate the denominator
// calculate roots
// calculate root one
// calculate root two
- Now it turns out that the Math class access is also integrated into your compiler and/or IDE so it is not necessary to put that in the program. However, for the educational exercise, this will be shown here. Again, the reference for finding the Math class is java.lang, however this can be found in any references as needed.
package p1_package;
import javax.swing.JOptionPane;
import java.lang.Math
Display the roots
// display roots
// display user input
// display root one
// display root two
- The actions are also pretty easy for output display, but you will be required to use the System.out.printlnoperations for this.
- Here are your output statements with the methods specified and located.
// display roots
// display user input
// method: System.out.println
// display root one
// method: System.out.println
// display root two
// method: System.out.println
End the program
// end program
// display program end
- With the ending display, System.out.println must be used one more time. The method component is shown below.
// end program
// display program end
// method: System.out.println
Completing Step Three
- You have now finished specifying the requirements and locations of the program methods that you will need. The design part of step three is complete.
- Since you are not required to create any of your own methods, you don't have anything more to do in step three. Later, when you are creating your own methods, there will be a little more to do at this point
Some more notes:
- Without your own method development, this step still does not take very long to write. However, you have now prepared yourself for the fourth and last development step of this program
- 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_StepThree. You must then copy the contents of the main method in QuadClass_StepTwo into the main method of your QuadClass_StepThree. You want to keep your step two 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 your method locations and identify your header files, you must still make sure the program compiles without errors or warnings (i.e., there are no red or yellow flags next to your code). This continues to be trivial because you are not adding any code although you did add some import statements which will actually show warnings that they are not being used. As long as you are aware of the situation, you can move ahead to writing the step four code.
To see the step three process in action, watch this video; then develop your step three code with or without the video as needed in your own IDE.