CS 149: Introduction to Programming
James Madison University, Spring 2018 Semester

Lab06: Formatting output in Java

Background

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.6 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.

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

Part 1: Celsius to Fahrenheit

Write a program that will convert Celsius temperatures to their equivalent Fahrenheit values. The appropriate method to solve this problem is found on mathsisfun.com. Download Celsius.java 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 to use in testing your program. Think carefully about the range of data values.

  3. In the main method documentation of Celsius.java, 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 Celsius.java 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 Celsius.java 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);
    System.out.printf("Wow, that's like %8d degrees!\n", (int) fahrenheit);
    System.out.printf("If I had $1M for every degree, I'd have $%,.2f!\n", fahrenheit * 1e6);
    
  4. 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.

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 Project > Generate Documentation.

  2. When it asks you to create a new folder, make sure you choose a directory and/or pay attention to where it is going. You may have to expand the dialog box. Then choose generate. Note that to view the documentation you can choose Show Documentation, but to make changes you must use Generate Documentation..

  3. Your documentation should display in the web browser of your choice. Make sure YOUR comments are what is displayed. Right click to view the page source and make sure the date is correct.

  4. Experiment with changing the /** comments and running Javadoc again. Make sure you understand the relationship.