forjavastuff
forjavastuff

Reputation: 3

thermometer class logic error

I'm trying to make a class that has a single constructor that accepts a temperature (in Celsius) as a double, and if the temperature is less than -273.15, it sets it to -273.15. It also calculates other temperatures for different units of measurement, but that's not important. For some reason, I'm getting a logic error that doesn't correct inputs less than -273.15 to -273.15.

public class TemperatureC
{


private double temperature;


public TemperatureC(double c)
{
    if (temperature < -273.15)
    {
        temperature = -273.15;
    }
    else
    {
        temperature = c;
    }
}

public TemperatureC()
{
    temperature = -273.15;
}

public double getC()
{
    return temperature;
}

public double getF()
{
    return ((temperature * 1.8) + 32);
}

public double getK()
{
    return (temperature + 273.15);
}

public void setC(double c)
{
    if (temperature >= -273.15)
    {
        temperature = c;
    }

}

}

And this is what's using the class.

import java.util.Scanner;

public class TemperatureTester
{
public static void main(String[] args)
{
Scanner thermometer = new Scanner(System.in);
TemperatureC temp = new TemperatureC();

System.out.printf("Please enter the initial temperature:");
double intialTemp = thermometer.nextDouble();
temp.setC(intialTemp);

System.out.println("The current temperature in Celsius is:" + temp.getC());
System.out.println("The current temperature in Fahrenheit is:" + temp.getF());
System.out.println("The current temperature in Kelvin is:" + temp.getK());

System.out.printf("Please enter a new temperature:");
double secondTemp = thermometer.nextDouble();
temp.setC(secondTemp);

System.out.println("The current temperature in Celsius is:"+ temp.getC());
System.out.println("The current temperature in Fahrenheit is:"+ temp.getF());
System.out.println("The current temperature in Kelvin is:"+ temp.getK());

}
}

And here is my faulty output:

Please enter the initial temperature:-900
The current temperature in Celsius is:-900.0
The current temperature in Fahrenheit is:-1588.0
The current temperature in Kelvin is:-626.85
Please enter a new temperature:-900
The current temperature in Celsius is:-900.0
The current temperature in Fahrenheit is:-1588.0
The current temperature in Kelvin is:-626.85

It should correct inputs less than -273.15 to -273.15.

Upvotes: 0

Views: 173

Answers (2)

Dave McGuire
Dave McGuire

Reputation: 26

You're only checking for temperate < -273.15 in the constructor, so any time you call setC then you won't correct it. Additionally, in the setC method you don't set the temperature at all unless it's at or above -273.15

You could remove the constructor altogether since you aren't calling it anyway and change the logic in setC to check for temperates < -273.15

Upvotes: 0

Sean Powell
Sean Powell

Reputation: 563

Your issue is you are checking the default value of the constructor. Either set temperature to c first or check against c.

public TemperatureC(double c)
{

temperature = c;

if (temperature < -273.15)
{
    temperature = -273.15;
}

that should work, as a side effect the else is no longer needed

Upvotes: 1

Related Questions