Celsius to Fahrenheit Converter
by CodeChum Admin
Write a program that takes a temperature in Celsius as input (string) and converts it into double. Take the
double value in Celsius and convert it into Fahrenheit. The formula for Celsius to Fahrenheit is: (C * 9 / 5) +
32. Finally, print the Fahrenheit value in the format "Temperature in Fahrenheit: {result)".
Sample Output 1
Enter temperature in Celsius: 0
Temperature in Fahrenheit: 32.00
Sample Output 2
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.00
Sample Output 3|
Enter temperature in Celsius: -10
Temperature in Fahrenheit: 14.00
SEPERATE IT INTO MAIN, CELCIUS, TEMPERATURE FILES
Assistant
To create a Celsius to Fahrenheit converter program in a structured way, we can divide the code into three separate files: Main.java, Celsius.java, and Temperature.java. Each file will handle a specific part of the program:
User
Temperature must be an interface
Assistant
To incorporate the concept of interfaces into the design, we can define an interface for the temperature conversion logic. Let's call this interface TemperatureConverter. This interface will specify a method that any class implementing it must define, which is the conversion from Celsius to Fahrenheit.
Here's how you can modify the files to include an interface:
TemperatureConverter.java
java
public interface TemperatureConverter {
double convertToFahrenheit(Celsius celsius);
}
Temperature.java
java
Main.java: This file will contain the main method where the user input is taken and the conversion logic is called.
Celsius.java: This file will define a class to handle the Celsius temperature.
Temperature.java: This file will define a class to handle the conversion logic from Celsius to Fahrenheit.
Here's how you can structure these files:
Main.java
java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter temperature in Celsius: ");
String input = scanner.nextLine();
double celsius = Double.parseDouble(input);
Celsius celsiusTemp = new Celsius(celsius);
Temperature tempConverter = new Temperature();
double fahrenheit = tempConverter.convertToFahrenheit(celsiusTemp);
System.out.printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
}
}
Celsius.java
java
public class Celsius {
private double value;
public Celsius(double value) {
this.value = value;
}
public double getValue() {
return value;
}
}
Temperature.java
java
public class Temperature {
public double convertToFahrenheit(Celsius celsius) {
return (celsius.getValue() * 9 / 5) + 32;
}
}
Explanation:
Main.java: This is the entry point of the application. It uses Scanner to read user input, converts the input string to a double, creates an instance of Celsius, and then uses Temperature to convert the Celsius value to Fahrenheit. Finally, it prints the result formatted to two decimal places.
Celsius.java: This class encapsulates the Celsius temperature. It has a constructor to initialize the temperature and a method to retrieve it.
Temperature.java: This class contains the logic for converting Celsius to Fahrenheit. It has a method convertToFahrenheit that takes a Celsius object and returns the Fahrenheit equivalent.
This structure helps in maintaining a clean and modular code, making it easier to manage and extend in the future.
public class Temperature implements TemperatureConverter {
@Override
public double convertToFahrenheit(Celsius celsius) {
return (celsius.getValue() * 9 / 5) + 32;
}
}
Main.java
No changes are needed in Main.java as it already uses the Temperature class for conversion.
Celsius.java
No changes are needed in Celsius.java as it remains a simple class encapsulating the Celsius temperature value.
Explanation:
TemperatureConverter.java: This interface defines a contract that any class implementing it must provide a method to convert Celsius to Fahrenheit. This promotes flexibility and allows for different implementations of temperature conversion if needed in the future.
Temperature.java: This class now implements the TemperatureConverter interface and provides the actual implementation of the conversion logic.
By introducing an interface, the design adheres to the principle of abstraction, where the details of the conversion are hidden from the user of the interface. This makes the system more modular and easier to extend or modify without affecting other parts of the code.
“Celsius to Fahrenheit Converter by CodeChum Admin Write a program that takes a….” Conversation with deepseek-v2-api-0628, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m851cqznbrm09c09h6p0z0y8