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 Javadoca 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. Your program should closely resemble what you did in PA1.

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("The total value is %.3f\n", totalValue);

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.

Submission: At the end of class today, or by 11:00 PM if you would like more time, submit and Celsius.html via Canvas. The latter file will be in the folder you create when running Javadoc for the first time. Don't forget to complete Part 4 as well.

Part 4: Canvas Profile

As a final part of today's lab, let's take a few minutes and double check your Canvas account setup.

  1. Log into Canvas and click the "Settings" link on the upper right.
  2. If you haven't done so already, please set up your profile picture.
  3. Click the "Edit Settings" button and update your display name if you go by some other name.
  4. If you'd like to receive text messages from Canvas, click "Add Contact Method" on the right.
  5. Click "Notifications" on the left sidebar and confirm what emails/texts you want to receive.

Just to be clear, these steps are required for the lab. You'll lose points if your profile picture is still a ghost.

Back to Top