import java.util.Scanner;
public class Temperature {
// Declare Constant value
private final double MARGIN_OF_ERROR = 0.000001;
// Instance variable
private double degreesKelvin; // degrees in kelvin
double celsius; // degrees in celsius
double fahrenheit; // degrees in fahrenheit
// Constructor method: initialize degreeskelvin to zero
public Temperature() {
degreesKelvin = 0;
}
// Convert and store temperature (in scale) as degreesKelvin
public void set(double temperature, String scale) {
if (scale.equals("C"))
temperature = getCelsius();
if (scale.equals("F"))
degreesKelvin = getFahrenheit();
if (scale.equals("K"))
degreesKelvin = getKelvin();
}
// Convert and save degreesFahrenheit in the kelvin scale
public void setFahrenheit(double degreesFahrenheit){
degreesKelvin = 5*(degreesFahrenheit - 32)/9 + 273.16 ;
}
// Convert and save degreesCelsius in the kelvin scale
public void setCelsius(double degreesCelsius) {
degreesKelvin = degreesCelsius + 273.16;
}
// Convert degreesKelvin to Fahrenheit and return the value
public double getFahrenheit() {
fahrenheit = 9 * (degreesKelvin - 273.16) /5 + 32 ;
return fahrenheit;
}
// Convert degreesKelvin to Celsius and return the value
public double getCelsius() {
celsius = degreesKelvin - 273.16;
return celsius;
}
// Return degreesKelvin
public double getKelvin() {
degreesKelvin = celsius + 273.16;
return degreesKelvin;
}
// Return true if the two degrees are equal, plus or minus the margin of error
public boolean isNearlyEqual(Temperature t2) {
if (getCelsius() == t2.getFahrenheit() + MARGIN_OF_ERROR &&
getCelsius() == t2.getFahrenheit() - MARGIN_OF_ERROR)
return true;
else
return false;
}
// Return true if the different in degrees is greater than the margin of error
public boolean isNotNearlyEqual(Temperature t2) {
double difference = (getCelsius() - t2.getFahrenheit()) ;
if (Math.abs(difference) < MARGIN_OF_ERROR)
return true;
else
return false;
}
// Return true if degrees is greater than t2's degrees and above the margin of error
public boolean isGreaterThan(Temperature t2) {
if (getCelsius() > t2.getFahrenheit())
return true;
else
return false;
}
// Return true if the degrees is less than t2's degrees by more than the margin of error
public boolean isLessThan(Temperature t2) {
if (getCelsius() < t2.getFahrenheit())
return true;
else
return false;
}
// Return true if degress is greater than or nearly equal to t2's degrees, +/- margin of error
public boolean isGreaterOrEqual(Temperature t2) {
if (getCelsius() >= t2.getFahrenheit() + MARGIN_OF_ERROR &&
getCelsius() >= t2.getFahrenheit() - MARGIN_OF_ERROR)
return true;
else
return false;
}
// Return true if degrees is less than or nearly equal to t2's degrees false otherwise
public boolean isLessOrEqual(Temperature t2) {
if (getCelsius() <= t2.getFahrenheit() + MARGIN_OF_ERROR &&
getCelsius() <= t2.getFahrenheit() - MARGIN_OF_ERROR)
return true;
else
return false;
}
}
and the driver method
import java.util.Scanner;
public class Driver {
public static void main(String args[]) {
// Declare variables used for user input
double temp1, temp2;
String scale;
// Create a Scanner object to read from the keyboard
Scanner keyboard = new Scanner(System.in);
// Get a first temperature reading from the user
System.out.print("Enter the first temperature: ");
temp1 = keyboard.nextDouble();
scale = keyboard.nextLine().trim();
// Declare and instantiate an object reference variable
Temperature tempConvert = new Temperature();
// Display the first Temperature in all three scales
System.out.println("First temperature is");
System.out.println(tempConvert.getKelvin() + " degrees Kelvin");
System.out.println(tempConvert.getCelsius() + " degrees Celsius");
System.out.println(tempConvert.getFahrenheit() + " degrees Fahrenheit");
// Get second temperature reading from user
System.out.println();
System.out.print("Enter the second temperature: ");
temp2 = keyboard.nextDouble();
scale = keyboard.nextLine().trim();
// declare and instantiate an object reference variable
Temperature tempConvert2 = new Temperature();
// Display the second temperature in all three scales
System.out.println("Second Temperature is");
System.out.println(tempConvert2.getKelvin() + " degrees Kelvin");
System.out.println(tempConvert2.getCelsius() + " degrees Celsius");
System.out.println(tempConvert2.getFahrenheit() + " degrees Fahrenheit");
// Display the results of the boolean relational methods
// temperature variables
System.out.println();
System.out.println("Nearly Equal is " + tempConvert.isNearlyEqual(tempConvert2));
System.out.println("Not Nearly Equal is " + tempConvert.isNotNearlyEqual(tempConvert2));
System.out.println("Greater than is " + tempConvert.isGreaterThan(tempConvert2));
System.out.println("Less than is " + tempConvert.isLessThan(tempConvert2));
System.out.println("Greater than or equal is " + tempConvert.isGreaterOrEqual(tempConvert2));
System.out.println("Less than or equal is " + tempConvert.isLessOrEqual(tempConvert2));
}
}
where I put (tempConvert.getKelvin()) I tried to put (tempConvert.set()) so that I would call the set method from the driver class but it says error, that "method set in class Temperature could not be applied to give types"
In addition how would I enter the margin of error in lines 86 and 96, how would I set it up?
What am I doing wrong?

New Topic/Question
Reply


MultiQuote



|