1

I need to use a normalized temperature value, to check if someone has an illness.

I have written a function that converts from Celsius to Fahrenheit, but need to write the same function using normalized temperatures.

public void hasFever(int temperature1, int temperature2, int temperature3, int temperature4, String bodylocation) throws ArithmeticException, InputMismatchException {
    final int noOfTimesTempTaken = 4;
    if(bodylocation == "ear") { //Determine if someone has fever if temperature was taken by the ear
        final double feverTemperature = 98.6;   
        double temperature1InFahrenheit = (temperature1 * 9/5) + 32;
        double temperature2InFahrenheit = (temperature2 * 9/5) + 32;
        double temperature3InFahrenheit = (temperature3 * 9/5) + 32;
        double temperature4InFahrenheit = (temperature4 * 9/5) + 32;
        double avgTemp = (temperature1InFahrenheit + temperature2InFahrenheit + temperature3InFahrenheit + temperature4InFahrenheit) / noOfTimesTempTaken;
        if(avgTemp >= feverTemperature) {
            System.out.println("This person has a fever because their temperature of " + avgTemp + " is greater than normal which is 98.6");
        } else {
            System.out.println("This person does not have a fever because their temperature of " + avgTemp + " is less than normal which is 98.6");
        }
    }
}
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
y26tween
  • 11
  • 2
  • 1
    I would be nice, if you could tell us what exactly your problem is. What is the output you get? And what is the expected output? – Minding Aug 04 '19 at 01:41
  • You don't have any code, which may throw either an `ArithmeticException` nor an `InputMismatchException`, so you can remove `throws ArithmeticException, InputMismatchException`. Also `ArithmeticException` is an [unchecked exception](https://stackoverflow.com/questions/6115896/understanding-checked-vs-unchecked-exceptions-in-java/6116051#6116051) and therefore does not have to be in the method signature. – Minding Aug 04 '19 at 01:51
  • 1
    My problem lies in how to "convert' my temperature variables to normalized temperature that could be evaluated against the feverTemperature to determine if the person is ill. – y26tween Aug 04 '19 at 02:11
  • I will remove the specific exception and just throw general Exception – y26tween Aug 04 '19 at 02:11
  • You don't have to throw any Exception. Also you should use [String.equals()](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) instead of `==` to compare Strings. – Minding Aug 04 '19 at 02:20
  • 3
    It's probably just me, but I don't know what you mean by "normalized". Could you explain to me what that means in this context? – Minding Aug 04 '19 at 02:21
  • 1
    since temperature could be measured in Celsius or Fahrenheit ( the reason I'm converting from Celsius to Fahrenheit) before evaluating the value against Fever temperature. I want to refactor the function to be able to compare Temperature values (without converting from one unit to the other )that are either in Calcium or Fahrenheit in determining if the person is sick. – y26tween Aug 04 '19 at 02:35
  • You have to do everything in a common temperature scale to then compare values. It seems to me that doing that is "normalization". It doesn't matter which scale you use technically, although in your case it would be easier to convert your `feverTemperature` value to Celsius instead of what you're doing. But you ARE doing normalization the way I see it. – CryptoFool Aug 04 '19 at 02:56

2 Answers2

1

First of all, it should be noted that normalized temperature is not a term with a recognized / standard meaning that is applicable here. The term normalize is sometimes used to mean "adjusted to deal with measurement inaccuracies", but that's not what you seem to be trying to do here.

(It must be said that you have not successfully clarified what you mean by normalize, so we have to guess. This is my best guess, based on what you have said.)

I think you are looking for something like this:

double VALID_FAHRENHEIT_TEMP_THRESHOLD = 60.0;  // say

public double normalizeToFahrenheit(double temp) {
    if (temp >= VALID_FAHRENHEIT_TEMP_THRESHOLD) {
        return temp;
    } else {
        return temp * (9.0 / 5.0) + 32.0;
    }
}

But the problem is that this kind of "normalization" is scientifically and mathematically unsound. We are assuming that if a number is less than a particular value it must in degrees Centigrade rather than degrees Fahrenheit. But that isn't necessarily true. Both the Centigrade and Fahrenheit scales go down to very low values.

More practically, if someone has drowned in freezing water, it is possible for their body temperature in Fahrenheit may be way down ... possible close to freezing point. It may well be lower that our chosen threshold. If we assume that it "must be" Centigrade and convert it, we report an incorrect temperature.

A related problem is that a strange temperature value could be the result of the user miss-reading a thermometer, miss-hearing a measurement, or miss-typing a value; e.g. 11.3 instead of 101.3. Applying a normalization to mistaken input would lead to even more misleading results.


So given that this "normalization" methodology is not valid, what should you do instead?

My suggestion is for your app to report an anomaly to the user. For example, show them a dialog like this:

  The temperature xy degrees is outside of the expected range for human 
  temperatures in Fahrenheit.  

  Options: 

     1) Accept as a Fahrenheit value.  
     2) Apply Centigrade to Fahrenheit conversion. 
     3) Switch device to Centigrade mode.  
     4) Reenter value.
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

I hope I understand your problem correctly. So you want to pass a universal temperature unit in to your function.

class Temperature {
    private final static double CELSIUS_OFFSET = 273,15;
    private final static double FAHRENHEIT_OFFSET = 32;
    private final static double FAHRENHEIT_FACTOR = 9.0 / 5.0;
    private final double tempInKelvin;

    private Temperature() {
        //Private constructor, so only this class can access it
    }

    public static Temperature fromKelvin(double tempInKelvin) {
        final Temperature temperature = new Temperature();
        temperature.tempInKelvin = tempInKelvin;
        return temperature;
    }

    public static Temperature fromCelsius(double tempInCelsius) {
        return fromKelvin(tempInCelsius - CELSIUS_OFFSET);
    }

    public static Temperature fromFahrenheit(double tempInFahrenheit) {
        return fromCelsius((tempInFahrenheit - FAHRENHEIT_OFFSET) / FAHRENHEIT_FACTOR);
    }

    public double getInKelvin() {
        return tempInKelvin;
    }

    public double getInCelsius() {
        return tempInKelvin + CELSIUS_OFFSET;
    }

    public double getInFahrenheit() {
        return getInCelsius()*FAHRENHEIT_FACTOR + FAHRENHEIT_OFFSET;
    }
}

Using this class you can do this:

//Example use (has to be in a method, of course):
final Temperature temp1 = Temperature.fromCelsius(39.2);
final Temperature temp2 = Temperature.fromFahrenheit(95.3);
final Temperature temp3 = Temperature.fromCelsius(39.1);
final Temperature temp4 = Temperature.fromCelsius(39.3);
hasFever(temp1, temp2, temp3, temp4, "ear");

//New hasFever() method:
public void hasFever(Temperature temp1, Temperature temp2, Temperature temp3, Temperature temp4, String bodylocation) {
    final int noOfTimesTempTaken = 4;
    //USE String.equals()!!!
    if(bodylocation.equals("ear")) { //Determine if someone has fever if temperature was taken by the ear
        final double feverTemperature = 98.6;
        double avgTemp = (temp1.getInFahrenheit() + temp2.getInFahrenheit() + temp3.getInFahrenheit() + temp4.getInFahrenheit()) / noOfTimesTempTaken;
        if(avgTemp >= feverTemperature) {
            System.out.println("This person has a fever because their temperature of " + avgTemp + " is greater than normal which is 98.6");
        } else {
            System.out.println("This person does not have a fever because their temperature of " + avgTemp + " is less than normal which is 98.6");
        }
    }
}

Now you have a universal Temperature class which basically has no accessable unit, until you specify which one you want. This also converts all units for you.

Minding
  • 1,383
  • 1
  • 17
  • 29