Skip to main content
Engineering LibreTexts

14.2: Writing Methods

  • Page ID
    15243
  • Beginners 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. 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} \nonumber \]

    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)? In this case, the two points are the parameters, 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 signature and a return statement:

    public static double distance (double x1, double y1, double x2, double y2) {
        return 0.0;
    }
    

    The return statement is a placeholder that is 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 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 helpful to know the right answer.

    Once we have compiled the stub, we can start adding lines of code one at a time. After each incremental change, we recompile and run the program. If there is an error at any point, we have a good idea where to look: the last line we added.

    The next step is to find the differences x2x1 and y2y1. We store those values in temporary variables named dx and dy.

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

    The print statements allows us to check the intermediate values before proceeding. They should be 3.0 and 4.0. 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 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;
    }
    

    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. Nevertheless, incremental development can save you a lot of time.

    • Was this article helpful?