0

How can I convert to different measurement systems if I can't create another UNIT field to understand which system I need to convert to?

class Temperature{
    private final double tm;
    Temperature(double tm, char unit) {
        this.tm = tm;

    }
    public double getInC(){

    }
    public double getInF(){

    }
    public double getInK(){

    }
}
public class Temperatures {
    public static void main(String[] args) {
        Temperature t1 = new Temperature(25, 'C');
        System.out.printf("C: %6.2f%n", t1.getInC());
        System.out.printf("F: %6.2f%n", t1.getInF());
        System.out.printf("K: %6.2f%n", t1.getInK());
        Temperature t2 = new Temperature(77, 'F');
        System.out.printf("C: %6.2f%n", t2.getInC());
        System.out.printf("F: %6.2f%n", t2.getInF());
        System.out.printf("K: %6.2f%n", t2.getInK());
        Temperature t3 = new Temperature(298.15, 'K');
        System.out.printf("C: %6.2f%n", t3.getInC());
        System.out.printf("F: %6.2f%n", t3.getInF());
        System.out.printf("K: %6.2f%n", t3.getInK());
    }
}

Idk what to do about the fact that I can't create another field

BlakieSl
  • 11
  • 3
  • 2
    Maybe you could always store the temperature in Celsius, and have logic in the constructor that converts the given temperature into Celsius if the unit is something different. – Dawood ibn Kareem Dec 18 '22 at 19:44
  • Can you please tell me how to apply this? – BlakieSl Dec 18 '22 at 20:08
  • If you're familiar with `switch` expressions, you could put one in the constructor. Otherwise, you could use some `if` and `else if` statements to achieve the same result. – Dawood ibn Kareem Dec 18 '22 at 20:15
  • Under the linked original question read the last paragraph of [the answer](https://stackoverflow.com/a/74717724/5772882), **Alternative solution**. – Ole V.V. Dec 19 '22 at 18:30

0 Answers0