Skip to Content
Course content

24: Practice Exercise: Building a Temperature Converter

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Alright, it's time to stop reading theory and actually build something. We've covered the basics of variables, conditionals, and user input, so let's mash them all together. We're going to build a Temperature Converter. It sounds simple, but it's a perfect way to practice handling user choices and dealing with Java's picky nature when it comes to numbers.

Getting the user's intent

First, I need a way to ask the user what they actually want to do. Do they have Celsius and want Fahrenheit, or vice versa? I'll use a Scanner for this. I like to keep my input logic clean, so I'll use a simple integer choice (1 or 2). It's not the most sophisticated UI in the world, but for a CLI tool, it gets the job done without forcing the user to type out long strings.

import java.util.Scanner;

public class TempConverter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Temperature Converter");
        System.out.println("1. Celsius to Fahrenheit");
        System.out.println("2. Fahrenheit to Celsius");
        System.out.print("Choose an option: ");
        
        int choice = scanner.nextInt();
        // Conversion logic goes here...
    }
}

The classic integer division trap

Now for the math. The formula for Celsius to Fahrenheit is (C * 9/5) + 32. I'll start writing the logic for the first option. I'm going to be honest—even after years of doing this, I still occasionally trip over integer division in Java. Watch what happens if I write it like this:

if (choice == 1) {
    System.out.print("Enter temperature in Celsius: ");
    double celsius = scanner.nextDouble();
    double fahrenheit = (celsius * 9 / 5) + 32; 
    System.out.println("Result: " + fahrenheit);
}

At first glance, this looks fine. But if I use a number where the result should have a decimal, I might notice something weird. In Java, 9 / 5 is integer division, which equals 1, not 1.8. By doing this, I've just stripped the precision out of my formula. My temperatures are going to be wrong.

To fix this, I just need to make sure at least one of the numbers is a double. I'll change 5 to 5.0. This tells Java, "Hey, I want a floating-point calculation here."

// The corrected line
double fahrenheit = (celsius * 9 / 5.0) + 32;

Handling the reverse conversion and formatting

Now I'll add the logic for Fahrenheit to Celsius. The formula is (F - 32) * 5/9. Again, I'll use 5.0 and 9.0 to avoid that integer trap. I also don't want the output to look like 23.333333333333332, which is what Java loves to do with doubles. I'll use System.out.printf to round it to two decimal places.

else if (choice == 2) {
    System.out.print("Enter temperature in Fahrenheit: ");
    double fahrenheit = scanner.nextDouble();
    double celsius = (fahrenheit - 32) * 5.0 / 9.0;
    System.out.printf("Result: %.2f Celsius%n", celsius);
} else {
    System.out.println("Invalid choice. Please run the program again.");
}

And that's it. We've got a working tool that handles input, prevents math errors, and formats the output for a human to actually read. It's not a complex piece of architecture, but these small habits—like double-checking your division—are what separate a junior dev from someone who doesn't spend three hours debugging a "rounding error."




📋 Practical Task

Exercise: Add a Kelvin Conversion Feature

Your task is to extend the TempConverter program we just built. Currently, it only handles Celsius and Fahrenheit. I want you to modify the program to include a third option: Kelvin.

  • Update the menu to include "3. Celsius to Kelvin".
  • Implement the conversion logic: Kelvin = Celsius + 273.15.
  • Ensure the Kelvin result is also formatted to two decimal places using printf.
  • Add a check to ensure the user cannot enter a temperature below absolute zero (-273.15°C); if they do, print an error message instead of the result.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.