1 Replies - 1458 Views - Last Post: 27 February 2014 - 03:32 AM Rate Topic: -----

#1 swtpineapple   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-February 14

Temperature class and driver not communicating correctly

Posted 26 February 2014 - 07:47 PM

the output of the files are not correct, user's are prompted for a temperature and then it should be converted and display the user's temp in three scales but it isn't doing it. please what did I do incorrectly?

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?

Is This A Good Question/Topic? 0
  • +

Replies To: Temperature class and driver not communicating correctly

#2 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: Temperature class and driver not communicating correctly

Posted 27 February 2014 - 03:32 AM

The temperature values you read in, you never use them anywhere.

As a starting point:
public void set(double temp, String scale) {
   if(scale.equals("C")){
      celsius = temp;
      setFahrenheit(temp);
      setKelvin(temp);
   }

   if(scale.equals("F")){
      celsius = setCelsius(temp);
      fahrenheit = temp;
      setKelvin(temp);
   }
.
.
.                
}


then you can use methods to just return the variables that are required
The tricky part is determining which conversion method to use depending on the scale enetered,but I guess a simple if statement will help there

in your driver class, you need to set the temperature
tempConvert.set(temp1, scale);


and then you can say
temp1.getCelsius();
temp1.getKelvin();

to get whichever one you want.

I hope that helps

This post has been edited by mike73: 27 February 2014 - 03:42 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1