Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperatureConverter.java
More file actions
Latest commit
63 lines (51 loc) · 1.82 KB
/
Copy pathTemperatureConverter.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
importjava.util.Scanner;
// Abstract class Temperature
abstractclassTemperature {
protecteddoubletemp;
// Method to set temperature data
publicvoidsetTempData(doubletemp) {
this.temp = temp;
}
// Abstract method to be implemented by subclasses
abstractvoidchangeTemp();
}
// Fahrenheit class to convert to Celsius
classFahrenheitextendsTemperature {
privatedoublectemp; // Celsius temperature
@Override
voidchangeTemp() {
// 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
classCelsiusextendsTemperature {
privatedoubleftemp; // Fahrenheit temperature
@Override
voidchangeTemp() {
// 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);
}
}
publicclassTemperatureConverter {
publicstaticvoidmain(String[] args) {
Scannerscanner = newScanner(System.in);
System.out.println("Temperature Converter");
System.out.println("--------------------");
// Create Fahrenheit to Celsius converter
Fahrenheitfahrenheit = newFahrenheit();
System.out.print("Enter temperature in Fahrenheit: ");
doublefTemp = scanner.nextDouble();
fahrenheit.setTempData(fTemp);
fahrenheit.changeTemp();
// Create Celsius to Fahrenheit converter
Celsiuscelsius = newCelsius();
System.out.print("Enter temperature in Celsius: ");
doublecTemp = scanner.nextDouble();
celsius.setTempData(cTemp);
celsius.changeTemp();
scanner.close();
}
}