Lab05: Formatting output in Java



You have a Canadian friend with whom you talk frequently. Canadians use the metric system for many of their measurements, including temperatures in the Celsius scale. You want to practice your Java skills by writing a program to help you quickly figure out what the equivalent temperature (in Fahrenheit) is here in Harrisonburg. We will use the System.out.printf() method described in Section 3.4 of the book. As a quick review, check out this printf reference by Jack Wilson at Cerritos College.

Collaboration: You are encouraged to work with another student to complete this lab. Each of you should submit your own copy of the programs. It's okay if your files are similar or identical, as long as both of your names are present at the top.

Make sure you add your name in the @author section of the Javadoc and also in the comments above the EXAMPLES section for JGrasp to make sure it prints your name.

 * Lab 5 - Convert.
 * @author YOUR NAME
 * EXAMPLES - Provide 3 examples for testing:
     *      Celsius    Fahrenheit
     * 1.

Part 1: Celsius to Fahrenheit

Write a program that will convert Celsius temperatures to their equivalent Fahrenheit values. The appropriate formula to solve this problem is found on Download to use as a starting point for your solution.

  1. Decide which formula should be used to convert a Celsius temperature to its equivalent in Fahrenheit.
  2. Develop three examples and (fill these out in the comments section)to use in testing your program. Think carefully about the range of data values.
  3. In the main method documentation of, list three test examples rounded to one decimal place.
  4. Write code to solve the problem: declare variables, get the input, do the calculation, and output the results.
  5. Test your code with the three examples you came up with earlier, and make sure that they all work correctly.

Don't forget to prompt the user for input like this:
System.out.print("Enter in a Celsius temperature to convert to Fahrenheit: ");

Part 2: Hello, printf method!

Your program likely displays the temperature as a number with lots of decimal places, depending upon the calculated value. Your Canadian friend will not appreciate you saying, "Wow, that's like 87.199823 Fahrenheit." So you want to produce a number that is more reasonable and has only one decimal point.

System.out.printf is a method like System.out.print or System.out.println. Instead of just printing what is between the parentheses, printf allows for substitutions and formatting of output. The printf method takes two or more parameters, separated by commas. The first parameter is a formatting string. It contains the text that you want to use, as well as positions where it will substitute other values. For example, a double variable is named totalValue and we wanted to print it with three decimal places:

System.out.printf("Your temperature of %.1f degrees "
+ "Celsius is %.1f degrees Fahrenheit\n", c, f);

Substitute your variables for the c and f values above.

Note the format specifier is only %.3f (\n is not part of the format for totalValue; it's just more text to print out at the end).

  1. Try using printf in your program, but display the value with only 1 decimal position.
  2. Copy the following lines to the end of your program, replacing the word fahrenheit with your own variable name.
  3. System.out.printf("Wow, that's like %d degrees!\n", (int) fahrenheit);
  4. System.out.printf("Wow, that's like %8d degrees!\n", (int) fahrenheit);
  5. System.out.printf("If I had $1M for every degree, I'd have $%,.2f!\n", fahrenheit * 1e6);
  6. Add an inline comment (//) before each printf listed above, and explain what the format specifier means.

Note the cast operator (int) in the previous examples. What does it mean to cast? Try removing it and see what happens. Will it compile? Will it run? You'll need to know these answers before the midterm.  This cheatsheet for printf might also be of help.

Part 3: Running Javadoc

Javadoc is a nifty tool that reads your documentation comments and generates web pages based on them. JGrasp makes it easy to run Javadoc, but unfortunately its built-in web browser makes the output difficult to read on Linux.

  1. In JGrasp, go to File > Show Documentation, or click the book icon on the toolbar for this function.
  2. Your documentation should display in the web browser of your choice. Make sure your name and the date are correct.
  3. Experiment with changing the /** comments and running Javadoc again. Make sure you understand the relationship.
  4. To save your html document in the browser window do a Right-Click Save Page As then Web Page html only (for Firefox or Chrome in the labs) or Page Source in Safari on a Mac.

Submission: At the end of class today, or by 11:00 PM tomorrow night if you would like more time, submit to and Celsius.html via Canvas. The latter file will be in the folder you create when running Javadoc for the first time.


Back to Top