Skip to main content
Engineering LibreTexts

11.9: Putting It All Together

  • Page ID
    17727
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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.


    This page titled 11.9: Putting It All Together is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .

    • Was this article helpful?