boolean method
I'm trying to create a boolean method that checks the temperature that is stored in temperature field and checks to see if the temperature is at or below the freezing point of ethyl alcohol. This is what I have done but I was told by my instructor that it was incorrect.
publicstaticboolean isEthylFreezing(double temperature)
{
boolean status;
if (temperature <= -173)
status =true;
else
status =false;
return status;
}
Can you give me a clue on where I should go?
It is correct, but it could be shorter. Look at this:
public static boolean isEthylFreezing(double temperature)
{
return (temperature <= -173);
}
Thanks, so when i call the method in the main method, is the following correct?
public static void main(String[] args)
{
String input; //holds the user's input
double temp; //holds the temperature
//asks for the temperature
input = JOptionPane.showInputDialog("Enter the temperature to be checked: ");
temp = Double.parseDouble(input);
//create an object from the CheckTemp Class
CheckTemp temp1 = new CheckTemp(temp);
//check the temperature and displays what substance boils and freezes at the temperature
if (temp1.isEthylFreezing(temp))
{
JOptionPane.showMessageDialog(null,"Ethyl Alcohol freezes at " + temp + " degrees");
Looks good to me, except youre missing a closing brace.
thanks for the help. if my instructor still tells me its incorrect, i'll just show here these posts!!
Only a slight logical problem. If a user entered -200 your program would say "Ethyl alcohol freezes at -200 degrees"
> Only a slight logical problem. If a user entered -200
> your program would say "Ethyl alcohol freezes at -200
> degrees"
I think he meant it to be like that. It looks like he'll add in more methods later to see if different materials freeze at that temperature. And Ethyl alcohol freezes at -200 degrees too. ;-)
The isEthylFreezing method is static, so you should change the following:
if (temp1.isEthylFreezing(temp))
to
if (CheckTemp.isEthylFreezing(temp))
Not sure what you are planning to do with the constructed CheckTemp object. If you only want to call static methods, you don't need the constructed object.
Having a 'double' named 'temp' and a 'CheckTemp' object named 'temp1' is confusing. temp and temp1 sound like they should be the same type.
Your other option, depending on your requirements, would be to make the CheckTemp methods non-static. You could then construct a CheckTemp object with a temperature, and not pass a parameter to the methods--just check the temperature you constructed the object with.
if (temp1.isEthylFreezing()) { }
if (temp1.isWaterFreezing()) { }
if (temp1.isCO2Freezing()) { }
etc.
My point is Ethyl alcohol doesn't freeze AT -200 degrees. It will be frozen yes but -200 degrees is not its freezing point. So the ouput is somewhat misleading. A more appropriate output would be
"At " + temp + " degrees Ethyl alcohol will be frozen."