Java Project: Temperature Conversion

Tips :: JavaJava code for the conversion of degrees from Celsius to Fahrenheit and vise versa. This was a project for Java programming 101 in the fall semester of 2012. The project and tutorial can help Java newbies get a sense of some of the basic conditions and loops.



Copying this project in full or in part is plagiarism. I share this code as a sample, to help you understand and learn. If you use any of this code, you should maintain the attribution to the original author (me!) along with a link to this page.

The Exercise

The original project exercise stated:

Write a program that allows the user to convert a temperature given in degrees from either Celsius to Fahrenheit or Fahrenheit to Celsius. Use the following formulas:
Degrees_C = 5(Degrees_F-32)/9
Degrees_F = (9(Degrees_C)/5) + 32)
Prompt the user to enter a temperature and either a C or c for Celsius or an F or f for Fahrenheit. Convert the temperature to Fahrenheit if Celsius is entered or to Celsius if Fahrenheit is entered. Display the result in a readable format. If anything other than C, c, F, or f is entered, print an error message and stop.

My code goes a bit further than that, but the principle remains the same.

Algorithm Description

We start by setting up the variables and strings that would be needed in the code:

These variables and strings will be used in the rest of the class to define and manipulate the output.

Next, we ask the user for input. For convenience, since we will repeat this request for input later, the request is served through the function speakIntroduction(). If we wish to change the request, we will only have to change the function rather than changing it over and over in the repetitive code:

This function merely prints out the instruction on the screen. It will be repeated every 5 attempts by the user as a usage reminder.

The program will request input from the user, on a continuous loop. The user will be prompted to provide input until they enter either “stop” or “quit”.

Trim Whitespaces

The lowercase action is meant to make sure the entire code validates on the same condition without worrying about case sensitivity. This will be useful for the temperature type (‘c’ or ‘C’) and to the stop commands (‘stop’ or ‘STOP’ or ‘Stop’, etc).

(Notice that this can also be done by requesting a comparison regardless of case by using the method equalsIgnoreCase)

Verify String is not Empty

Manipulate the String

Split the string to numerical temperature and temperature type (symbol):

As it splits the input, the program verifies that the first part is a number (float). If there are no numbers in the supplied input, an error message will be printed and the loop will restart, asking the user to re-enter the input.

Since the user can also type “stop” or “quit” to stop the program, either of those will match the exception caught in the NumberFormatException. However, these should not produce the same error message, but instead continue to the output processing phase, and so these will be skipped, and the loop will stop.

Calculate Output

After the previous step, the program produces two variables that were split from the original input.

These will be calculated for a proper output using the boolean function calculateOutput():

The function checks the character value of the type of temperature and uses it to calculate output. If the temperature given is in Fahrenheit, the program will convert to Celsius and return the outputted result:

If the temperature is given in Celsius, the program will convert to Fahrenheit and return the outputted result:

Otherwise, the program will return an error:

Following this, the loop returns and the user is asked to provide the input again, or insert ‘quit’ or ‘stop’ to quit the program.

Repeating Instructions

In order to remind the user about the purpose of the program, the initial instructions will be printed on the screen every 5 input attempts. The counter will be reset and count 5 input loops again:


Finally, when the user inputs “quit” or “stop”, the loop will end, and so will the program:


Click to Enlarge

Click to Enlarge

The Full Code

Please note: You are free to use this class in your code, as long as you keep the attribution and copyright notice. If you improve it, send it back to me and I’ll make sure to credit your improvements in the live version!


Tags: ,

Trackback from your site.