-1

Temperature is measured mainly in three units: in degrees Celsius, degrees Fahrenheit and in kelvins. It’s easy to convert any of them to the two others:

Kelvin to Celsius:     C = K - 273.15
Celsius to Kelvin:     K = C + 273.15
Kelvin to Fahrenheit:  F = 9./5*(K - 273.15) + 32
Fahrenheit to Kelvin:  K = 5./9*(F - 32) + 273.15
Celsius to Fahrenheit: F = 9./5*C + 32
Fahrenheit to Celsius: C = 5./9*(F - 32)

Write class Temperature with one (and only one!) private field of type double; objects of the class describe temperature. The class has one constructor and three methods:

  • Temperature(double tm, char unit) — constructor taking temperature (as a double) and symbol of the unit used: ’C’ for Celsius, ’F’ for Fahrenheit and ’K’ for kelvins;

  • three methods („getters”) returning the temperature represented by an object,but in different units:

    public double getInC()
    public double getInF()
    public double getInK()
    

I don't really understand how to do this if we don't have an field of type char and we can't get any parameters into functions, how to solve it?

Below is what I have so far. It obviously does not fulfil the requirements yet.

public class Temperature {
    private final double tm;
    public Temperature(double tm, char unit) {
        this.tm = tm;
    }
    public double getInC(){
    }
    public double getInF(){
    }
    public double getInK(){
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • What's the specific issue? Keep the temperature in a known unit *(doesn't matter which)*. Based on the unit it's stored in each getter can return the appropriate conversion (or none). E.g., when the temperature comes in store it in *(say)* K, and convert in the ctor if it's passed in in *(say)* F. The getters convert to the appropriate unit, but in this example, `getInK` would just return the stored value. – Dave Newton Dec 07 '22 at 13:44
  • Just decide on one unit that you are using internally in your class (for example Kelvin, but which you like the best). In your constructor depending on the unit passed convert to your internal scale (or not if the unit is already the same). Only after conversion assign to your temperature field. To avoid confusion, include the unit in the field name, for example `temperatureKelvin`. Anyone creating an instance of your class and using the methods needs not know or care which unit you use internally. – Ole V.V. Dec 07 '22 at 15:10
  • It’s a fine exercise in data hiding or encapsulation. I really hope you will be able to solve it. – Ole V.V. Dec 07 '22 at 15:18

2 Answers2

0

Just create a field for the unit as well, then you have all the necessary information to do the conversion:

public class Temperature {
    private final double tm;
    private final char unit;

    public Temperature(double tm, char unit) {
        this.tm = tm;
        this.unit = unit;
    }

    public double getInC() {
        // TODO: conversion
    }

    public double getInF() {
        // TODO: conversion
    }

    public double getInK() {
        // TODO: conversion
    }

    @Override
    public String toString() {
        return tm + "" + unit;
    }
}

Btw, what you have here is called a 'Value Object', and the recommendation is to add a toString() method so you can print temparatures if you want to (and later also add equals and hashcode methods to compare instances by value).

Alternative solution if you don't want to add a field: convert the temparature given in the constructor into an internal unit (proposal: in K), and then convert to the requested temperatures from Kelvin.

Peter Walser
  • 15,208
  • 4
  • 51
  • 78
0

Here is an example of the 'alternative solution':

public class Temperature {

    private double value = 0d;

    Temperature(double value, char unit) {
        this.value = value;
        switch (unit) {
        case 'C':
            this.value = value;
            break;
        case 'K':
            this.value = value - 273.15;
            break;
        case 'F':
            this.value = 5.0 / 9 * (value - 32);
            break;
        }
        System.out.println("Temperature is " + value + "°" + unit);
    }

    double getInC() {
        return value;
    }

    double getInF() {
        return 9.0 / 5 * value + 32;
    }

    double getInK() {
        return value + 273.15;
    }

    public static void main(String[] args) {
        Temperature test = new Temperature(42, 'C');
        System.out.println("\t" + test.getInC() + "°C");
        System.out.println("\t" + test.getInK() + "°K");
        System.out.println("\t" + test.getInF() + "°F");

        test = new Temperature(42, 'K');
        System.out.println("\t" + test.getInC() + "°C");
        System.out.println("\t" + test.getInK() + "°K");
        System.out.println("\t" + test.getInF() + "°F");

        test = new Temperature(42, 'F');
        System.out.println("\t" + test.getInC() + "°C");
        System.out.println("\t" + test.getInK() + "°K");
        System.out.println("\t" + test.getInF() + "°F");
    }

Hope it helps