I am new to java and I have encountered my first wall so any and all help would be greatly appreciated. I am writing a temperature class that handles both (and converts among) Celsius and Fahrenheit. My constructor should have two explicit params, first a character (unitChar) then a double (value). Also, two double instance variables (cVal & fVal or tempC & tempF). Next, two accessor methods (getCelsius and getFahrenheit) that just return a double value. Finally, I'd like to add a comment method that provides a description of the temperature. For example, below 32 = "Freezing", 32-50 = "Brisk", 50-80 = "Nice" and above 80 = "Hot". My problem is I do not know how to properly incorporate the explicit params so I haven't made much progress since I took up this take task:/. Please help.
import java.util.*;
public class Temperature
{
public static void main(String[] args)
{
double cVal;
double fVal;
double value;
System.out.println("Please enter a temperature.")
Scanner keyboard = new Scanner(System.in);
value = keyboard.nextInt();
char unitChar;
System.out.println("Please enter the unit of measure F or C")
tempUnit = keyboard.next().charAt(0);
if((unitChar=='f')||(unitChar =='F'))
{
cVal = (5.0/9.0)* value - 32;
}
else if((unitChar=='C')||(unitChar =='c'))
{
fVal = (9.0/5.0) * value + 32;
}
}
public double getCelsius()
{
double cVal = (5.0/9.0) * value - 32;
return cVal;
}
public double getFahrenheit()
{
double fVal = (9.0/5.0) * value + 32;
return fVal;
}
public String getDesription()
{
String retVal = "";
if (value <= 32)
{
System.out.println("It is freezing.");
}
if ((value >= 33) && (value <= 50))
{
System.out.println("It is brisk.");
}
if ((value >= 51) && (value <= 80))
{
System.out.println("It is nice.");
}
else if (value > 81)
{
System.out.println("It is hot.");
}
System.out.println("Done");
}
}

New Topic/Question
Reply



MultiQuote





|