Write a Java program to demonstrate the use of an interface for custom temperature conversion
interface TemperatureConverter { double celsiusToFahrenheit(double celsius); double fahrenheitToCelsius(double fahrenheit); } public class Main { public static void main(String[] args) { TemperatureConverter temperatureConverter = new TemperatureConverter() { @Override public double celsiusToFahrenheit(double celsius) { return (celsius * 9/5) + 32; // Implement Celsius to Fahrenheit conversion } @Override public double fahrenheitToCelsius(double fahrenheit) { return (fahrenheit - 32) * 5/9; // Implement Fahrenheit to Celsius conversion } }; double celsiusValue = 25.0; double fahrenheitValue = 77.0; // Convert Celsius to Fahrenheit double fahrenheitResult = temperatureConverter.celsiusToFahrenheit(celsiusValue); System.out.println(celsiusValue + "°C is " + fahrenheitResult + "°F"); // Convert Fahrenheit to Celsius double celsiusResult = temperatureConverter.fahrenheitToCelsius(fahrenheitValue); System.out.println(fahrenheitValue + "°F is " + celsiusResult + "°C"); } }
25.0°C is 77.0°F 77.0°F is 25.0°C
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions