Can't get this method to work
I'm having trouble with the method in my program "toCelsius" the toFahrenheit method is practically identical except for the reverse equation, and that works fine. When i run my program however, the output (in the toString method) always says 0 degrees celsius no matter what temperature it is, but if input as celsius it works fine.
example output:
Please enter the temperature outside.
85
Is this temperature in Celsius or Fahrenheit? Please enter "C" or "F".
c
Based on the temperature outside: 85.0 degrees Celsius (117.0 Fahrenheit) I suggest: swimming.
Please enter a temperature for another day or time.
85
Is this temperature in Celsius or Fahrenheit? Please enter "C" or "F".
f
Based on the temperature outside: 85.0 degrees Fahrenheit (0.0 Celsius) I suggest: swimming.
The two temperatures are not equal
class code:
publicclass TempActivity
{
String activity, scale, output;
double temp, tempC, tempF;
double convTemp;
public TempActivity()
{
temp = 0;
}
public TempActivity(int startTemp)
{
setTemp(startTemp);
}
publicdouble getTemp()
{
return temp;
}
publicvoid setTemp(double newTemp)
{
temp = newTemp;
}
public String getActivity()
{
if (getTemp()>=85)
activity ="swimming";
if (getTemp()>=70 && getTemp()<=84)
activity ="tennis";
if (getTemp()>=60 && getTemp()<=69)
activity ="hiking";
if (getTemp()<60)
activity ="board games";
return activity;
}
publicboolean equals(TempActivity activeA)
{
if (getTemp()==activeA.getTemp() && getScale().equals(activeA.getScale()))
returntrue;
else
returnfalse;
}
publicvoid toCelsius(double newC)
{
tempC = (newC-32)*(5/9);
convTemp = tempC;
}
publicvoid toFahrenheit(double newF)
{
tempF = (9/5)*(newF+32);
convTemp = tempF;
}
publicvoid setScale(String newScale)
{
scale = newScale;
}
public String getScale()
{
return scale;
}
public String toString()
{
if (getScale().equalsIgnoreCase("c"))
output ="Based on the temperature outside: " +temp +" degrees Celsius (" +convTemp +" Fahrenheit) I suggest: " +activity +".";
elseif (getScale().equalsIgnoreCase("f"))
output ="Based on the temperature outside: " +temp +" degrees Fahrenheit (" +convTemp +" Celsius) I suggest: " +activity +".";
return output;
}
}
client code:
import java.util.*;
publicclass TempActivityClient
{
publicstaticvoid main(String[] args)
{
double temp, tempA;
String activity, activityA, scale, scaleA;
TempActivity active =new TempActivity();
Scanner keyboard =new Scanner(System.in);
System.out.println("Please enter the temperature outside.");
temp = keyboard.nextInt();
System.out.println("Is this temperature in Celsius or Fahrenheit? Please enter \"C\" or \"F\".");
scale = keyboard.next();
if (scale.equalsIgnoreCase("c"))
{active.toFahrenheit(temp);
active.setScale("c");}
elseif (scale.equalsIgnoreCase("f"))
{active.toCelsius(temp);
active.setScale("f");}
else
System.out.println("Error: answer must be \"c\" or \"f\"");
active.setTemp(temp);
activity = active.getActivity();
System.out.println("");
System.out.println(active.toString());
TempActivity activeA =new TempActivity();
System.out.println("");
System.out.println("Please enter a temperature for another day or time.");
tempA = keyboard.nextInt();
System.out.println("Is this temperature in Celsius or Fahrenheit? Please enter \"C\" or \"F\".");
scaleA = keyboard.next();
if (scaleA.equalsIgnoreCase("c"))
{activeA.toFahrenheit(tempA);
activeA.setScale("c");}
elseif (scaleA.equalsIgnoreCase("f"))
{activeA.toCelsius(tempA);
activeA.setScale("f");}
else
System.out.println("Error: answer must be \"c\" or \"f\"");
activeA.setTemp(tempA);
activityA = activeA.getActivity();
System.out.println("");
System.out.println(activeA.toString());
System.out.println("");
if (active.equals(activeA))
System.out.println("The two temperatures are equal");
else
System.out.println("The two temperatures are not equal");
}
}
Message was edited by:
thecynicle1

