Hi everyone, Just wondering, if you can help me with this program it deals with
ID: 3626222 • Letter: H
Question
Hi everyone,
Just wondering, if you can help me with this program it deals with classes...Help please!! If you can, please be as detailed as possible through comments in the code so I can understand -- thanks!
Define a class Temperature that will be used to convert Fahrenheit to Celsius and vice-versa. The class should have member variables to hold a temperature value (double) and a character for the scale (either 'F' or 'C'). Include for methods
• A constructor with no parameters that sets the temperature to zero and scale to 'C'
• A constructor that takes one parameter, a double, and sets the temperature to that value, and scale to 'C'
• A constructor that takes two parameters, a double and a char, sets the temperature and the scale
• An accessor, getFahrenheit, returns the temperature in Fahrenheit (degreeF = 9*(degreesC)/5 + 32)
• An accessor, getCelsius, returns the temperature in Celsius (degreeC = 5*(degreesF-32)/9)
• A mutator method, setTemperature, takes one parameter, double, to set the temperature
• A mutator method, setTemperature, takes two parameters, double and char, to set temperature and scale
• A suitable toString method (return type of String so a Temperature object can be displayed)
Temperature temp1 = new Temperature();
Temperature temp2 = new Temperature(20);
Temperature temp3 = new Temperature(65, 'F');
double cels = temp2.getCelsius();
double fahr = temp2.getFahrenheit();
System.out.println(temp2 + " is " + cels + " in Celsius");
System.out.println(temp2 + " is " + fahr + " in Fahrenheit");
temp2.setTemperature(30);
cels = temp2.getCelsius();
fahr = temp2.getFahrenheit();
System.out.println(temp2 + " is " + cels + " in Celsius");
System.out.println(temp2 + " is " + fahr + " in Fahrenheit");
cels = temp3.getCelsius();
fahr = temp3.getFahrenheit();
System.out.println(temp3 + " is " + cels + " in Celsius");
System.out.println(temp3 + " is " + fahr + " in Fahrenheit");
temp3.setTemperature(50);
cels = temp3.getCelsius();
fahr = temp3.getFahrenheit();
System.out.println(temp3 + " is " + cels + " in Celsius");
System.out.println(temp3 + " is " + fahr + " in Fahrenheit");
temp1.setTemperature(80, 'F');
cels = temp1.getCelsius();
fahr = temp1.getFahrenheit();
System.out.println(temp1 + " is " + cels + " in Celsius");
System.out.println(temp1 + " is " + fahr + " in Fahrenheit");
Here is the tester program...
/*
* This program demonstrates the use of Temperature objects.
* Three objects are instantiated. Their use is shown
* and their methods are tested.
*
* This is a client program of the Temperature class.
*/
public class TemperatureDriver {
public static void main(String[] args) {
Temperature temp1 = new Temperature();
Temperature temp2 = new Temperature(20);
Temperature temp3 = new Temperature(65, 'F');
double cels = temp2.getCelsius();
double fahr = temp2.getFahrenheit();
System.out.println(temp2 + " is " + cels + " in Celsius");
System.out.println(temp2 + " is " + fahr + " in Fahrenheit");
System.out.println();
temp2.setTemperature(30);
cels = temp2.getCelsius();
fahr = temp2.getFahrenheit();
System.out.println(temp2 + " is " + cels + " in Celsius");
System.out.println(temp2 + " is " + fahr + " in Fahrenheit");
System.out.println();
cels = temp3.getCelsius();
fahr = temp3.getFahrenheit();
System.out.println(temp3 + " is " + cels + " in Celsius");
System.out.println(temp3 + " is " + fahr + " in Fahrenheit");
System.out.println();
temp3.setTemperature(50);
cels = temp3.getCelsius();
fahr = temp3.getFahrenheit();
System.out.println(temp3 + " is " + cels + " in Celsius");
System.out.println(temp3 + " is " + fahr + " in Fahrenheit");
System.out.println();
temp1.setTemperature(80, 'F');
cels = temp1.getCelsius();
fahr = temp1.getFahrenheit();
System.out.println(temp1 + " is " + cels + " in Celsius");
System.out.println(temp1 + " is " + fahr + " in Fahrenheit");
System.out.println();
}
}
Explanation / Answer
// Define a class Temperature that will be used to convert Fahrenheit to Celsius and vice-versa.
public class Temperature
{
// The class should have member variables to hold a temperature value (double) and a character for the scale (either 'F' or 'C'). Include for methods
private double value;
private char scale;
// A constructor with no parameters that sets the temperature to zero and scale to 'C'
public Temperature()
{
this(0.0, 'C');
}
// A constructor that takes one parameter, a double, and sets the temperature to that value, and scale to 'C'
public Temperature(double value)
{
this(value, 'C');
}
// A constructor that takes two parameters, a double and a char, sets the temperature and the scale
public Temperature(double v, char s)
{
value = v;
scale = s;
}
// An accessor, getFahrenheit, returns the temperature in Fahrenheit (degreeF = 9*(degreesC)/5 + 32)
public double getFarenheit()
{
if(scale == 'F')
return value;
else
return 9*value/5+32;
}
// An accessor, getCelsius, returns the temperature in Celsius (degreeC = 5*(degreesF-32)/9)
public double getCelcius()
{
if(scale == 'C')
return value;
else
return 5*(value-32)/9;
}
// A mutator method, setTemperature, takes one parameter, double, to set the temperature
public void setTemperature(double v)
{
value = v;
}
// A mutator method, setTemperature, takes two parameters, double and char, to set temperature and scale
public void setTemperature(double v, char s)
{
value = v;
scale = s;
}
// A suitable toString method (return type of String so a Temperature object can be displayed)
public String toString()
{
return ""+value+""+scale;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.