-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperatureConverter.java
More file actions
63 lines (51 loc) · 1.82 KB
/
TemperatureConverter.java
File metadata and controls
63 lines (51 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.Scanner;
// Abstract class Temperature
abstract class Temperature {
protected double temp;
// Method to set temperature data
public void setTempData(double temp) {
this.temp = temp;
}
// Abstract method to be implemented by subclasses
abstract void changeTemp();
}
// Fahrenheit class to convert to Celsius
class Fahrenheit extends Temperature {
private double ctemp; // Celsius temperature
@Override
void changeTemp() {
// Convert Fahrenheit to Celsius: C = 5/9 * (F - 32)
ctemp = 5.0 / 9.0 * (temp - 32);
System.out.printf("%.2f°F = %.2f°C\n", temp, ctemp);
}
}
// Celsius class to convert to Fahrenheit
class Celsius extends Temperature {
private double ftemp; // Fahrenheit temperature
@Override
void changeTemp() {
// Convert Celsius to Fahrenheit: F = 9/5 * C + 32
ftemp = 9.0 / 5.0 * temp + 32;
System.out.printf("%.2f°C = %.2f°F\n", temp, ftemp);
}
}
public class TemperatureConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Temperature Converter");
System.out.println("--------------------");
// Create Fahrenheit to Celsius converter
Fahrenheit fahrenheit = new Fahrenheit();
System.out.print("Enter temperature in Fahrenheit: ");
double fTemp = scanner.nextDouble();
fahrenheit.setTempData(fTemp);
fahrenheit.changeTemp();
// Create Celsius to Fahrenheit converter
Celsius celsius = new Celsius();
System.out.print("Enter temperature in Celsius: ");
double cTemp = scanner.nextDouble();
celsius.setTempData(cTemp);
celsius.changeTemp();
scanner.close();
}
}