Java
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Java
-
Section 4: Collections Framework
-
Section 5: Exception Handling
-
Section 6: Generics
-
Section 7: Functional Java
-
Section 8: Concurrency
-
Section 9: I/O and NIO
-
Section 10: JVM Internals
-
Section 11: Modern Java Features
-
Section 12: Build Tools and Project Structure
-
Section 13: Testing
-
Section 14: Databases and Persistence
-
Section 15: Networking
-
Section 16: Design and Best Practices
-
Section 17: Reflection and Annotations
-
Section 18: Logging and Diagnostics
-
Section 19: Date, Time, and Internationalization
-
Section 20: Java Platform Module System
-
Section 21: Security in Java
-
Section 22: Advanced Collections and Data Structures
-
Section 23: More Concurrency Patterns
-
Section 24: Compression, Files, and System Integration
-
Section 25: GUI Programming
-
Section 26: Practical Projects
-
Section 27: Data Structures and Algorithms
-
Section 28: Interview and Algorithm Practice
-
Section 29: JSON and Data Interchange
-
Section 30: More Concurrency Utilities
-
Section 31: More Collections and Streams Practice
-
Section 32: More File and System Programming
-
Section 33: Standard Library Deep Dive
-
Section 34: More Practice and Drills
-
Section 35: More Testing and Quality
-
Section 36: More Design Patterns and Architecture
-
Section 37: Career and Ecosystem
-
Section 38: More OOP and Architecture Practice
-
Section 39: More Enterprise Concepts
-
Section 40: Advanced JavaFX
-
Section 41: More Interview Practice
24: Practice Exercise: Building a Temperature Converter
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.
There are no comments for now.