4. Method Development¶
4.1. Objectives¶
- Describe what is meant by incremental development
4.2. Time Goal¶
- 15 minutes on this section
4.3. Key Terms¶
- debugging
- searching for errors and correcting in code
- incremental development
- Start with a small working program and make small, incremental changes to the program.
- stub
- A method declaration and a valid return statement without any logic processing code
4.4. Common Mistakes¶
- Write too much code before trying to compile!
- Not compiling frequently, even after small changes to code
4.5. Exercises¶
Note
People often make the mistake of writing a lot of code before they try to compile and run it. Then they spend way too much time debugging or searching for errors and correcting in the code.
A better approach is what we call incremental development. The key aspects of incremental development are:
- Start with a working program and make small, incremental changes. At any point, if there is an error, you will know where to look.
- Use variables to hold intermediate values so you can check them, either with print statements or by using a debugger.
- Once the program is working, you can consolidate multiple statements into compound expressions (but only if it does not make the program more difficult to read).
As an example, suppose you want to find the distance between two points, given by the coordinates (x1, y1) and (x2, y2). By the usual definition:
\(distance = \sqrt{(x_2 − x_1)^2 +(y_2 − y_1)^2}\)
The first step is to consider what a distance method should look like in Java. In other words, what are the inputs (parameters) and what is the output (return value)? For this method, the parameters are the two points, and it is natural to represent them using four double values. The return value is the distance, which should also have type double.
Already we can write an outline for the method, which is sometimes called a stub. The stub includes the method declaration and a return statement:
public static double distance(double x1, double y1, double x2, double y2) {
return 0.0; // stub
}
The return statement is a placeholder that is only necessary for the program to compile. At this stage the program doesn’t do anything useful, but it is good to compile it so we can find any syntax errors before we add more code.
It’s usually a good idea to think about testing before you develop new methods; doing so can help you figure out how to implement them. To test the method, we can invoke it from main using the sample values:
double dist = distance(1.0, 2.0, 4.0, 6.0);
With these values, the horizontal distance is 3.0 and the vertical distance is 4.0. So the result should be 5.0, the hypotenuse of a 3-4-5 triangle. When you are testing a method, it is necessary to know the right answer.
Note
Once we have compiled the stub, we can start adding code one line at a time. After each incremental change, we recompile and run the program. If there is an error, we have a good idea where to look: the lines we just added.
The next step is to find the differences x2 − x1 and y2 − y1. We store those values in temporary variables named dx and dy, so that we can examine them with print statements before proceeding. They should be 3.0 and 4.0.
public static double distance(double x1, double y1, double x2, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
System.out.println("dx is " + dx);
System.out.println("dy is " + dy);
return 0.0; // stub
}
We will remove the print statements when the method is finished. Code like that is called scaffolding, because it is helpful for building the program, but it is not part of the final product.
The next step is to square dx and dy. We could use the Math.pow method, but it is simpler (and more efficient) to multiply each term by itself.
public static double distance(double x1, double y1, double x2, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
double dsquared = dx * dx + dy * dy;
System.out.println("dsquared is " + dsquared);
return 0.0; // stub
}
Again, you should compile and run the program at this stage and check the intermediate value, which should be 25.0. Finally, we can use Math.sqrt to compute and return the result.
public static double distance(double x1, double y1, double x2, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
double dsquared = dx * dx + dy * dy;
double result = Math.sqrt(dsquared);
return result;
}
As you gain more experience programming, you might write and debug more than one line at a time. But by using incremental development, scaffolding, and testing, your code is more likely to be correct the first time
-
Q-98: Which of the following are some ways to help make you better at writing code?
- Use methods to break down the program into manageable pieces
- Correct
- Compile only at the end of writing your program
- Incorrect
- Utilize stubs
- Correct
- Start with the smallest working program possible, compile it, gradually make changes to it, and continuously recompile
- Correct
- Avoid scaffolding and print statements that won't be in the final version of your program
- Correct
-
Q-99: Which of the following is the best example of a method stub that returns a boolean value if a weight exceeds a certain limit?
public static boolean overMaximumLoad(double tons){ if (tons > 100) return true; else return false; }
- Incorrect
public static boolean overMaximumLoad(double tons){}
- Incorrect
public static void overMaximumLoad(double tons){}
- Incorrect
public static double overMaximumLoad(boolean tons){ return false; }
- Incorrect
public static boolean overMaximumLoad(double tons){ return true; }
- Correct
What is an approach where you create intermediate print statements to help show if your code is performing correctly?