James Madison University, Fall 2016 Semester
Lab05: 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.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.
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
convertcelciustofarenheit.com.
Download Celsius.java to
use as a starting point for your solution.
Decide which formula should be used to convert a Celsius temperature to its equivalent in Fahrenheit.
Develop three examples to use in testing your program. Think carefully about the range of data values.
In the main method documentation of Celsius.java, list three test examples rounded to one decimal place.
Write code to solve the problem: declare variables, get the input, do the calculation, and output the results.
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).
Try using
printf
in your Celsius.java program, but display the value with only 1 decimal position.Copy the following lines to the end of your program, replacing the word
fahrenheit
with your own variable name.Add an inline comment (
//
) before eachprintf
listed above, and explain what the format specifier means.
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);
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. DrJava makes it easy to run Javadoc, but unfortunately its built-in web browser makes the output difficult to read on Linux.
In DrJava, go to Edit > Preferences. Change the Web Browser to
/usr/bin/google-chrome
or/usr/bin/firefox
.Now press the Javadoc button on the main toolbar. When it asks you to create a new folder, press OK and then Yes.
Your documentation should display in the web browser of your choice. Make sure your name and the date are correct.
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 Celsius.java 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.
Log into Canvas and click the "Settings" link on the upper right.
If you haven't done so already, please set up your profile picture.
Click the "Edit Settings" button and update your display name if you go by some other name.
If you'd like to receive text messages from Canvas, click "Add Contact Method" on the right.
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.