# Margin of error?

Page 1 of 1

## 2 Replies - 2597 Views - Last Post: 15 February 2011 - 07:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=216417&amp;s=f8b02a9d7edf677487689a50c409920e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lairel

Reputation: 0
• Posts: 43
• Joined: 03-February 11

# Margin of error?

Posted 15 February 2011 - 07:21 PM

I need to make an if-else statement for margin of error = 0.001. How do i write the margin of error if-else this is what i have ATM

```public class Temperature
{
//instance variable
private double degreesInKelvin;

//Declare constant value for margin of error
final double MARGIN_OF_ERROR = 0.001;

//Constructor method: initialize degrees in Kelvin to zero
public Temperature()
{
degreesInKelvin = 0;
}

//Convert and save degrees in Celsius in the Kelvin scale
public void setDegreesFromCelsius(double celsius)
{
degreesInKelvin = celsius + 273.15;
}

//Convert and save degrees in Fahrenheit in the Kelvin scale
public void setDegreesFromFahrenheit(double fahrenheit)
{
degreesInKelvin = (( 5.0 / 9.0 ) * ( fahrenheit - 32 ) ) + 273.15;
}

//Return the degrees in Kelvin
public double getDegreesInKelvin()
{
return degreesInKelvin;
}
}

```

```	import java.util.Scanner;

public class TemperatureDemo
{
public static void main(String args[])
{
// Declare constant for margin of error
final double MARGIN_OF_ERROR = 0.001;

//Declare variables
double degrees;
Temperature temp1 = new Temperature();
Temperature temp2 = new Temperature();

//create a scanner object to read from the keyboard
Scanner keyboard = new Scanner(System.in);

//Get the temperature in Celsius from the user
System.out.print("Enter a temperature in Celsius ");
degrees = keyboard.nextDouble();
temp1.setDegreesFromCelsius(degrees);

//Get the temperature in Fahreinheit from the user
System.out.print("Enter a temperature in Fahrenheit ");
degrees = keyboard.nextDouble();
temp2.setDegreesFromFahrenheit(degrees);

double difference = temp1.getDegreesInKelvin() - temp2.getDegreesInKelvin();
if (Math.abs(difference) <= MARGIN_OF_ERROR)

//Display the two temperatures
System.out.println("Temperaure 1 in Kelvin is " + temp1.getDegreesInKelvin());
System.out.println("Temperature 2 in Kelvin is " + temp2.getDegreesInKelvin());

if (temp1 == temp2)
{
System.out.println("The two temperatures are equivalent");
}
else
{
System.out.println("The two temperatures are not equivalent");
}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Margin of error?

### #2 smacdav

Reputation: 56
• Posts: 177
• Joined: 06-June 09

## Re: Margin of error?

Posted 15 February 2011 - 07:31 PM

If I understand your goal correctly, you want to print "The two temperatures are equivalent" whenever the temperatures are within the margin of error of one another. In that case, simply replace the if statement you have on line 37 with the one you have on line 30.

### #3 lairel

Reputation: 0
• Posts: 43
• Joined: 03-February 11

## Re: Margin of error?

Posted 15 February 2011 - 07:51 PM

Thank you, that was way too easy.