public class Temperature9250 { //instance variables private double degrees; priv
ID: 3640250 • Letter: P
Question
public class Temperature9250{
//instance variables
private double degrees;
private char units;//'C' for Celsius, 'F' for Fahrenheit
//methods
//Constructor to specify both temperature and units.
public Temperature9250(double newDegrees, char newUnits)
{
degrees = newDegrees;
units = newUnits;
}
//Constructor to specify just the temperature, defaults to 0 for Celsius.
public Temperature9250(double newDegrees)
{
newDegrees = 0.0;
units = "c";
}
//Constructor to specify just the units, defaults to C degrees.
public Temperature9250(char newUnits)
{
newdegrees = 0.0;
newUnits = "C";
}
//Default constructor (specifies neither temperature or units), defaults to 0 degrees Celsius.
public Temperature9250()
{
degrees = 0;
units = "C";
}
//three mutators: methods to set (reset) the parameter values.
public void set(double newDegrees, char newUnits)
{
degrees = newDegrees;
units = newUnits;
}
//Method to set temperature only.
public void set(double newDegrees)
{
degrees = newDegrees;
}
//Method to set units only.
public void set(char newUnits)
{
if(newUnits.equalsIgnoreCase("c"))
{
units = "C";
}
else if(newUnits.equalsIgnoreCase("f"))
{
units = "F";
}
else
{
System.out.println("Incorrect degree type.");
System.exit(0);
}
}
//two accessors
public double getC()
{
return (5.0/9.0) * (degrees-32);
}
public double getF()
{
return ((9.0/5.0) * degrees) +32;
}
//Three comparison methods: note you have to consider the units
public boolean equals(Temperature another)
{
double difference;
Temperature t1 = another.getC();
Temperature t2 = this.getC();
difference = (t1.degrees - t2.degrees);
return(difference < .0005)
}
public boolean isGreaterThan(Temperature another)
{
}
public boolean isLessThan(Temperature another)
{
}
This is my code so far I have no idea how to do the booleans for this problem.
Explanation / Answer
public boolean isGreaterThan(Temperature another)
{
if(this.degrees > another.degrees)
return true;
return false;
}
public boolean isLessThan(Temperature another)
{
if(this.degrees < another.degrees)
return true;
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.