2 Replies - 2597 Views - Last Post: 15 February 2011 - 07:51 PM Rate Topic: -----

#1 lairel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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");
		}
      }
   }


please help me

Is This A Good Question/Topic? 0
  • +

Replies To: Margin of error?

#2 smacdav   User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 lairel   User is offline

  • New D.I.C Head

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

Re: Margin of error?

Posted 15 February 2011 - 07:51 PM

Thank you, that was way too easy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1