Skip to main content
Engineering LibreTexts

11.9: Putting It All Together

  • Page ID
    17727
  • At this point, you have seen enough Java to write useful programs that solve everyday problems. You can (1) import Java library classes, (2) create a Scanner, (3) get input from the keyboard, (4) format output with printf, and (5) divide and mod integers. Now we will put everything together in a complete program:

    import java.util.Scanner;
    
    /**
     * Converts centimeters to feet and inches.
     */
    public class Convert {
    
        public static void main(String[] args) {
            double cm;
            int feet, inches, remainder;
            final double CM_PER_INCH = 2.54;
            final int IN_PER_FOOT = 12;
            Scanner in = new Scanner(System.in);
            
            // prompt the user and get the value
            System.out.print("Exactly how many cm? ");
            cm = in.nextDouble();
            
            // convert and output the result
            inches = (int) (cm / CM_PER_INCH);
            feet = inches / IN_PER_FOOT;
            remainder = inches % IN_PER_FOOT;
            System.out.printf("%.2f cm = %d ft, %d in\n",
                                cm, feet, remainder);
        }
    }
    

    Although not required, all variables and constants are declared at the top of main. This practice makes it easier to find their types later on, and it helps the reader know what data is involved in the algorithm.

    For readability, each major step of the algorithm is separated by a blank line and begins with a comment. It also includes a documentation comment (/**), which we’ll learn more about in the next chapter.

    Many algorithms, including the Convert program, perform division and modulus together. In both steps, you divide by the same number (IN_PER_FOOT).

    When statements get long (generally wider than 80 characters), a common style convention is to break them across multiple lines. The reader should never have to scroll horizontally.

    • Was this article helpful?