Results 1 to 8 of 8

Thread: Please help

  1. #1
    cod4 is offline Member
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

    Default Please help

    Hi, the teacher made this way more confusing than it needs to be or I am just a complete idiot and I was wondering if someone could help... Here is the teachers assignment, sorry, its kinda long

    This assignment gives you more experience writing classes. The goal is to create a class that can be used to represent temperatures in a very flexible manner. An object of the class will represent a temperature. The class will provide methods for setting the temperature and getting the temperature in units of degrees Kelvin, Celsius, or Fahrenheit.
    In addition, the class will provide some useful library methods. These will be static methods that allow for conversion between temperature scales without creating an object of the class. You will also provide definitions of some useful constants: absolute zero in degrees C and degrees F. Other useful constants might include the triple point of water (which is defined to be the temperature at which water, water vapor and ice are all present in equilibrium), and the ice and steam points of water at standard atmospheric pressure (0C and 100C). The triple point is 273.16 degrees K.

    You'll make a decision about how you want to represent a temperature internally. Options include using Kelvin (zero is absolute zero), Celsius, or Fahrenheit degrees as your internal representation.

    There are at least two different ways you might proceed:
    Use the same temperature scale for all objects
    Represent the temperature in any of the three scales, and use an instance variable to indicate which scale you're using in that object.

    You should consider each approach, and think about which makes the most sense to you. Some things to consider are the complexity of the code needed, how many instance variables are required, and the ease of comparing two temperatures.
    Your class must provide the following methods (with the given signatures):
    public boolean setF(double tInFahrenheit)
    public boolean setC(double tInCelsius)
    public boolean setK(double tInKelvin)
    Each of the set methods must return the value true if the set operation was successful and false otherwise. Your set methods must not allow temperatures that are not physically realizable (below zero Kelvin). You must check the input values and return false if they are temperatures that are below zero Kelvin (absolute zero).
    public double getF()
    public double getC()
    public double getK()
    These get methods will return the temperature represented by the object in the units indicated. You will need to convert from whatever your internal representation is to the appropriate units (Fahrenheit, Celsius, or Kelvin).
    public boolean equals(Temperature t)
    public boolean greaterThan(Temperature t)
    public boolean lessThan(Temperature t)
    These comparison methods will return true if the calling object is related to the argument object by the operator named by the method name. In other words, given two Temperature objects, t1 and t2:
    t1.equals(t2) returns true if (and only if) the two temperature objects represent the same temperature.
    t1.lessThan(t2) returns true iff t1 < t2
    t1.greaterThan(t2) returns true iff t1 > t2

    The following static methods must be defined:
    public static double fahrenheitToCelsius(double degreesF)
    public static double fahrenheitToKelvin(double degreesF)
    public static double celsiusToFahrenheit(double degreesC)
    public static double celsiusToKelvin(double degreesC)
    public static double kelvinToCelsius(double degreesK)
    public static double kelvinToFahrenheit(double degreesK)

    Finally, you must provide constructor methods as follows:
    public Temperature() -- sets to a default temperature value of zero degrees C.
    public Temperature(double t, char scale) -- sets to t degrees in the scale represented by the scale parameter (use 'c' or 'C' for Celsius, 'f' or 'F' for Fahrenheit, and 'k' or 'K' for Kelvin). If the scale parameter is not interpretable or the temperature specified is not a physically possible temperature, your constructor should set the temperature to the default value specified for the default constructor above (you can see how to deal with this situation in a more reasonable fashion by using exception handling (in chapter 8). For now, we don't want to allow the creation of an object that represents an "impossible" temperature.).

    Note that you MUST use the above signatures, and meet all of the specifications. The name of your class MUST be Temperature, saved in a file named "Temperature.java". I will be using a test routine that expects these names and method signatures to test your implementation. If you don't name the methods exactly as written above, your code won't work with my test routine. That will cost you credit. I've posted my test routine with the assignment.

    Include a main method in your class that serves as a test routine for your Temperature class. Your main method should test each of the constructors, and each of the set, get, and comparison operators, and each of the static methods for temperature conversion. It should also verify that regardless of the scale you use to set the temperature, the following relationships hold: 32F = 0C, 212F = 100C, 273.15K = 0C. Your text has conversions from C to F and F to C. Degrees K are the same "size" as degrees C, just offset by 273.15. Therefore, 0K = -273.15C, and 0K = -459.67F (the F temperature is subject to some error: it is more like -459.6699999, the other two scales are related precisely as indicated above: you might want to consider that fact when determining how to do tests of temperature legitimacy).

    and here is the java program that came with it


    public class TestTemperature
    {
    public static void main(String[] args)
    {
    //Test the static conversion routines.
    System.out.println("Converting 0.0 K to F gives: "+ Temperature.kelvinToFahrenheit(0.0));
    System.out.println("Converting 0.0 K to C gives: "+ Temperature.kelvinToCelsius(0.0));
    System.out.println("Converting 0.0 C to K gives: "+ Temperature.celsiusToKelvin(0.0));
    System.out.println("Converting 32.0 F to K gives: "+ Temperature.fahrenheitToKelvin(32.0));
    System.out.println("Converting 32.0 F to C gives: "+ Temperature.fahrenheitToCelsius(32.0));
    System.out.println("Converting 0.0 C to F gives: "+ Temperature.celsiusToFahrenheit(0.0));


    Temperature t = new Temperature();
    System.out.println("Created new Temperature object using default constructor.");
    System.out.println("Farenheit temp is: "+t.getF());
    System.out.println("Celsius temp is: "+t.getC());
    System.out.println("Kelvin temp is "+t.getK());

    System.out.println("Set temperature to 100.0C using setC()");
    t.setC(100.0);
    System.out.println("Farenheit temp is: "+t.getF());
    System.out.println("Celsius temp is: "+t.getC());
    System.out.println("Kelvin temp is "+t.getK());

    System.out.println("Set temperature to 212.0F using setF()");
    t.setF(212.0);
    System.out.println("Farenheit temp is: "+t.getF());
    System.out.println("Celsius temp is: "+t.getC());
    System.out.println("Kelvin temp is "+t.getK());

    System.out.println("Set temperature to 0.0K using setK()");
    t.setK(0.0);
    System.out.println("Farenheit temp is: "+t.getF());
    System.out.println("Celsius temp is: "+t.getC());
    System.out.println("Kelvin temp is "+t.getK());

    Temperature t2 = new Temperature(100.0, 'c');
    System.out.println("Created new Temperature object using new Temperature(100.0,'c').");
    System.out.println("Farenheit temp is: "+t2.getF());
    System.out.println("Celsius temp is: "+t2.getC());
    System.out.println("Kelvin temp is "+t2.getK());

    System.out.println("Comparing temperatures: t in K = "+t.getK()+" t2 in K = "+t2.getK());
    System.out.println("t.equals(t2): "+t.equals(t2));
    System.out.println("Comparing temperatures:");
    System.out.println("t.greaterThan(t2): "+t.greaterThan(t2));
    System.out.println("Comparing temperatures:");
    System.out.println("t.lessThan(t2): "+t.lessThan(t2));

    t.setC(100.0);
    System.out.println("Comparing temperatures: t in K = "+t.getK()+" t2 in K = "+t2.getK());
    System.out.println("t.equals(t2): "+t.equals(t2));
    System.out.println("Comparing temperatures:");
    System.out.println("t.greaterThan(t2): "+t.greaterThan(t2));
    System.out.println("Comparing temperatures:");
    System.out.println("t.lessThan(t2): "+t.lessThan(t2));

    System.out.println("Attempting to set t to -460 F, currently t= "+t.getF()+"F");
    if(t.setF(-460))
    System.out.println("Success, t now is: "+t.getF()+"F");
    else
    System.out.println("Failed, t now is: "+t.getF()+"F");

    System.out.println("Attempting to set t to -274 C, currently t= "+t.getC()+"C");
    if(t.setC(-274))
    System.out.println("Success, t now is: "+t.getC()+"C");
    else
    System.out.println("Failed, t now is: "+t.getC()+"C");

    System.out.println("Attempting to set t to -0.1 K, currently t= "+t.getK()+"K");
    if(t.setK(-0.1))
    System.out.println("Success, t now is: "+t.getK()+"K");
    else
    System.out.println("Failed, t now is: "+t.getK()+"K");
    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    This assignment gives you more experience writing classes. The goal is to create a class that can be used to represent temperatures in a very flexible manner.
    Fun.
    An object of the class will represent a temperature.
    Okay.
    The class will provide methods for setting the temperature and getting the temperature in units of degrees Kelvin, Celsius, or Fahrenheit.
    Okay.
    In addition, the class will provide some useful library methods. These will be static methods that allow for conversion between temperature scales without creating an object of the class.
    Let's do these after the constants below.
    You will also provide definitions of some useful constants: absolute zero in degrees C and degrees F. Other useful constants might include the triple point of water (which is defined to be the temperature at which water, water vapor and ice are all present in equilibrium), and the ice and steam points of water at standard atmospheric pressure (0C and 100C). The triple point is 273.16 degrees K.
    This looks like a good place to start.
    Java Code:
    public class TestTemperature
    {
        public static void main(String[] args)
        {
            //Test the static conversion routines.
            System.out.println("Converting 0.0 K to F gives: "+
                                Temperature.kelvinToFahrenheit(0.0));
            System.out.println("Converting 0.0 K to C gives: "+
                                Temperature.kelvinToCelsius(0.0));
            System.out.println("Converting 0.0 C to K gives: "+
                                Temperature.celsiusToKelvin(0.0));
            System.out.println("Converting 32.0 F to K gives: "+
                                Temperature.fahrenheitToKelvin(32.0));
            System.out.println("Converting 32.0 F to C gives: "+                      
    
                                Temperature.fahrenheitToCelsius(32.0));
            System.out.println("Converting 0.0 C to F gives: "+
                                Temperature.celsiusToFahrenheit(0.0));
    /*
            Temperature t = new Temperature();
            System.out.println("Created new Temperature object using " +
                               "default constructor.");
            System.out.println("Farenheit temp is: "+t.getF());
            System.out.println("Celsius temp is: "+t.getC());
            System.out.println("Kelvin temp is "+t.getK());
    
            System.out.println("Set temperature to 100.0C using setC()");
            t.setC(100.0);
            System.out.println("Farenheit temp is: "+t.getF());
            System.out.println("Celsius temp is: "+t.getC());
            System.out.println("Kelvin temp is "+t.getK());
    
            System.out.println("Set temperature to 212.0F using setF()");
            t.setF(212.0);
            System.out.println("Farenheit temp is: "+t.getF());
            System.out.println("Celsius temp is: "+t.getC());
            System.out.println("Kelvin temp is "+t.getK());
    
            System.out.println("Set temperature to 0.0K using setK()");
            t.setK(0.0);
            System.out.println("Farenheit temp is: "+t.getF());
            System.out.println("Celsius temp is: "+t.getC());
            System.out.println("Kelvin temp is "+t.getK());
    
            Temperature t2 = new Temperature(100.0, 'c');
            System.out.println("Created new Temperature object using " +
                               "new Temperature(100.0,'c').");
            System.out.println("Farenheit temp is: "+t2.getF());
            System.out.println("Celsius temp is: "+t2.getC());
            System.out.println("Kelvin temp is "+t2.getK());
    
            System.out.println("Comparing temperatures: t in K = "+
                                t.getK()+" t2 in K = "+t2.getK());
            System.out.println("t.equals(t2): "+t.equals(t2));
            System.out.println("Comparing temperatures:");
            System.out.println("t.greaterThan(t2): "+t.greaterThan(t2));
            System.out.println("Comparing temperatures:");
            System.out.println("t.lessThan(t2): "+t.lessThan(t2));
    
            t.setC(100.0);
            System.out.println("Comparing temperatures: t in K = "+
                                t.getK()+" t2 in K = "+t2.getK());
            System.out.println("t.equals(t2): "+t.equals(t2));
            System.out.println("Comparing temperatures:");
            System.out.println("t.greaterThan(t2): "+t.greaterThan(t2));
            System.out.println("Comparing temperatures:");
            System.out.println("t.lessThan(t2): "+t.lessThan(t2));
    
            System.out.println("Attempting to set t to -460 F, " +
                               "currently t= "+t.getF()+"F");
            if(t.setF(-460))
                System.out.println("Success, t now is: "+t.getF()+"F");
            else
                System.out.println("Failed, t now is: "+t.getF()+"F");
    
            System.out.println("Attempting to set t to -274 C, " +
                               "currently t= "+t.getC()+"C");
            if(t.setC(-274))
                System.out.println("Success, t now is: "+t.getC()+"C");
            else
                System.out.println("Failed, t now is: "+t.getC()+"C");
    
            System.out.println("Attempting to set t to -0.1 K, " +
                               "currently t= "+t.getK()+"K");
            if(t.setK(-0.1))
                System.out.println("Success, t now is: "+t.getK()+"K");
            else
                System.out.println("Failed, t now is: "+t.getK()+"K");
    */
        }
    }
    
    class Temperature {
        // make a decision about how you want to represent
        // a temperature internally. Options include using
        // Kelvin (zero is absolute zero), Celsius, or
        // Fahrenheit degrees as your internal representation.
        // Okay, I'll make an arbitrary decision:
        //     use Kelvin for the internal representation.
        // Therefore, all values inside this class will be
        // in degrees Kelvin.
        // provide definitions of some useful constants:
        // absolute zero in degrees C and degrees F.
        // Here's where you get to make up some names:
        final double ABS_ZERO_CELSIUS    = -273.15;
        final double ABS_ZERO_FAHRENHEIT = -459.67;
        final double ABS_ZERO_KELVIN     =    0;
    
        // Other useful constants might include
        // the triple point of water (which is defined to be the
        // temperature at which water, water vapor and ice are all
        // present in equilibrium),
        // The triple point is 273.16 degrees K.
        final double TRIPLE_POINT = 273.16;
    
        // and the ice and steam points of water at standard
        // atmospheric pressure (0C and 100C).
        final double ICE_POINT   = 273.15;
        final double STEAM_POINT = 373.13;
    
    
    
        // provide some ... static methods that allow for conversion
        // between temperature scales without creating an object of
        // the class
        // The names of these are given in the TestTemperature class:
    
        public static double  kelvinToFahrenheit(double kelvin) {
            return 0;
        }
    
        public static double kelvinToCelsius(double kelvin) {
            return 0;
        }
    
        public static double celsiusToKelvin(double celsius) {
            return 0;
        }
    
        public static double fahrenheitToKelvin(double fahrenheit) {
            return 0;
        }
    
        public static double fahrenheitToCelsius(double fahrenheit) {
            return (fahrenheit - 32.0)*5/9;
        }
    
        public static double celsiusToFahrenheit(double celsius) {
            return 0;
        }
    
        // move on to the [i]get[/i]/[i]set[/i] method implementations...
    
    
    }
    Just do a little at a time.

  3. #3
    cod4 is offline Member
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

    Default

    Thank you very much, that helped a lot. I have soo much trouble in this, I spend so many hours to get nowhere. Thank you

  4. #4
    cod4 is offline Member
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

    Default

    K... I got this far

    import java.util.*;
    public class Temperature
    {

    private double defaultTemp;

    final double Abs0Celsius = -273.15;
    final double Abs0Fahrenheit = -459.67;
    final double Abs0Kelven = 0;

    final double TriplePoint = 273.16;
    final double IcePoint = 273.15;
    final double SteamPoint = 373.13;

    public boolean setF(double tInFahrenheit)
    {
    return true;
    }
    public boolean setC(double tInCelsius)
    {
    return true;
    }
    public boolean setK(double tInKelvin)
    {
    return true;
    }

    public double getF()
    {

    return 0.0;
    }
    public double getC()
    {

    return 0.0;
    }
    public double getK()
    {

    return 0.0;
    }

    public boolean equals(Temperature t)
    {
    return true;
    }
    public boolean greaterThan(Temperature t)
    {
    return true;
    }
    public boolean lessThan(Temperature t)
    {
    return true;
    }

    public static double fahrenheitToCelsius(double degreesF)
    {

    return 0.0;//(degreesF - 32) * 5 / 9;
    }

    public static double fahrenheitToKelvin(double degreesF)
    {
    return 0.0;//(degreesF + 459.67) * 5/9;
    }

    public static double celsiusToFahrenheit(double degreesC)
    {
    return 9.0/5.0*degreesC + 32.0;
    }

    public static double celsiusToKelvin(double degreesC)
    {
    return 0.0;//degreesC + 273.15;
    }

    public static double kelvinToCelsius(double degreesK)
    {
    return 0.0;//degreesK - 273.15;
    }

    public static double kelvinToFahrenheit(double degreesK)
    {
    return 0.0;//(degreesK - 273.15) * 9/5 + 32 ;
    }

    public Temperature()
    {
    }

    public Temperature(double t, char scale)
    {
    }

    }

    and now I am stuck.

  5. #5
    cod4 is offline Member
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

    Default

    teacher sent this earlier today, still dont know what to do
    Java Code:
    import java.util.*;
    public class Temperature
    {
    	//you will need to declare an instance variable to store the temperature here.
    	//it must be given private access control.
    
    	public boolean setF(double tInFahrenheit)
    	{
    		return true;
    	}
    	public boolean setC(double tInCelsius)
    	{
    		return true;
    	}
    	public boolean setK(double tInKelvin)
    	{
    		return true;
    	}
    
    
    
    
    	//Each of the set methods must return the value true if the set operation was successful
    	//and false otherwise. Your set methods must not allow temperatures that are not
    	//physically realizable (below zero Kelvin).  You must check the input values and return
    	//false if they are temperatures that are below zero Kelvin (absolute zero).
    	public double getF()
    	{
    
    		return 0.0;
    	}
    	public double getC()
    	{
    
    		return 0.0;
    	}
    	public double getK()
    	{
    
    		return 0.0;
    	}
    	//These get methods will return the temperature represented by the object in the units indicated.
    	//You will need to convert from whatever your internal representation is to the appropriate units
    	//(Fahrenheit, Celsius, or Kelvin).
    	public boolean equals(Temperature t)
    	{
    		return true;
    	}
    	public boolean greaterThan(Temperature t)
    	{
    		return true;
    	}
    	public boolean lessThan(Temperature t)
    	{
    		return true;
    	}
    	//These comparison methods will return true if the calling object is related to the argument
    	//object by the operator named by the method name. In other words, given two Temperature objects,
    	//t1 and t2:
    	//the method invocation t1.equals(t2) returns true if (and only if) the two temperature objects
    	//represent the same temperature.
    	//method call t1.lessThan(t2) returns true iff t1 < t2
    	//method call t1.greaterThan(t2) returns true iff t1 > t2
    
    	//The following static methods must be defined (you will find them useful for implementing
    	//the get and set methods, and they are also useful as library routines for other programmers):
    	public static double  fahrenheitToCelsius(double degreesF)
    	{
    
    		return 0.0;
    	}
    
    	public static double fahrenheitToKelvin(double degreesF)
    	{
    		return 0.0;
    	}
    
    	public static double celsiusToFahrenheit(double degreesC)
    	{
    		return 9.0/5.0*degreesC + 32.0;
    	}
    
    	public static double celsiusToKelvin(double degreesC)
    	{
    		return 0.0;
    	}
    
    	public static double kelvinToCelsius(double degreesK)
    	{
    		return 0.0;
    	}
    
    	public static double kelvinToFahrenheit(double degreesK)
    	{
    		return 0.0;
    	}
    
    
    	//Finally, you must provide constructor methods as follows:
    	//sets to a default temperature value of zero degrees C.
    	public Temperature()
    	{
    	}
    
    	//sets to t degrees in the scale represented by the scale parameter
    	//(use 'c' or 'C' for Celsius, 'f' or 'F' for Fahrenheit, and 'k' or 'K' for Kelvin).
    	//If the scale parameter is not interpretable or the temperature specified is not a
    	//physically possible temperature, your constructor should set the temperature to the default
    	//value specified for the default constructor above (you can see how to deal with this situation
    	//in a more reasonable fashion by using exception handling (in chapter 8). For now, we don't want
    	//to allow the creation of an object that represents an "impossible" temperature.).
    	public Temperature(double t, char scale)
    	{
    	}
    
    }
    Last edited by cod4; 04-01-2008 at 10:10 AM.

  6. #6
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    Cod, please enclose your code in the [code] [/ code] tags (minus the space after /) It makes your post a lot easier to read.
    Did this post help you? Please me! :cool:

  7. #7
    cod4 is offline Member
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

    Default

    k, I was trying to figure out how to do that but couldn't
    Java Code:
    public class Temperature2
    {
    //you will need to declare an instance variable to store the temperature here.
    //it must be given private access control.
    
    public boolean setF(double tInFahrenheit)
    {
    return true;
    }
    public boolean setC(double tInCelsius)
    {
    return true;
    }
    public boolean setK(double tInKelvin)
    {
    return true;
    }
    
    
    
    
    //Each of the set methods must return the value true if the set operation was successful
    //and false otherwise. Your set methods must not allow temperatures that are not
    //physically realizable (below zero Kelvin). You must check the input values and return
    //false if they are temperatures that are below zero Kelvin (absolute zero).
    public double getF()
    {
    
    return 0.0;
    }
    public double getC()
    {
    
    return 0.0;
    }
    public double getK()
    {
    
    return 0.0;
    }
    //These get methods will return the temperature represented by the object in the units indicated.
    //You will need to convert from whatever your internal representation is to the appropriate units
    //(Fahrenheit, Celsius, or Kelvin).
    public boolean equals(Temperature t)
    {
    return true;
    }
    public boolean greaterThan(Temperature t)
    {
    return true;
    }
    public boolean lessThan(Temperature t)
    {
    return true;
    }
    //These comparison methods will return true if the calling object is related to the argument
    //object by the operator named by the method name. In other words, given two Temperature objects,
    //t1 and t2:
    //the method invocation t1.equals(t2) returns true if (and only if) the two temperature objects
    //represent the same temperature.
    //method call t1.lessThan(t2) returns true iff t1 < t2
    //method call t1.greaterThan(t2) returns true iff t1 > t2
    
    //The following static methods must be defined (you will find them useful for implementing
    //the get and set methods, and they are also useful as library routines for other programmers):
    public static double fahrenheitToCelsius(double degreesF)
    {
    
    return 0.0;
    }
    
    public static double fahrenheitToKelvin(double degreesF)
    {
    return 0.0;
    }
    
    public static double celsiusToFahrenheit(double degreesC)
    {
    return 9.0/5.0*degreesC + 32.0;
    }
    
    public static double celsiusToKelvin(double degreesC)
    {
    return 0.0;
    }
    
    public static double kelvinToCelsius(double degreesK)
    {
    return 0.0;
    }
    
    public static double kelvinToFahrenheit(double degreesK)
    {
    return 0.0;
    }
    
    
    //Finally, you must provide constructor methods as follows:
    //sets to a default temperature value of zero degrees C.
    public Temperature()
    {
    }
    
    //sets to t degrees in the scale represented by the scale parameter
    //(use 'c' or 'C' for Celsius, 'f' or 'F' for Fahrenheit, and 'k' or 'K' for Kelvin).
    //If the scale parameter is not interpretable or the temperature specified is not a
    //physically possible temperature, your constructor should set the temperature to the default
    //value specified for the default constructor above (you can see how to deal with this situation
    //in a more reasonable fashion by using exception handling (in chapter 8). For now, we don't want
    //to allow the creation of an object that represents an "impossible" temperature.).
    public Temperature(double t, char scale)
    {
    }
    
    }
    Last edited by cod4; 04-01-2008 at 10:10 AM.

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default One thing at a time

    Suggestions:
    Do one thing at a time. Try:
    Get all of your static conversion methods working first.
    Implement the constructors, one at a time.
    test them.
    Get your set methods working properly.
    Get your get methods working properly.
    Test each (set of) method(s) as you go.
    Then start on the other methods (equals, lessThan, etc) one at a time.
    Complexity builds as you advance along this list. So therw's no point in thinking about the more complex parts until you have done the more simple parts, eg, defining constants that can be used throughout the class.
    If you get overwhelmed then remove everything that you haven't completed from the class. Add one thing at a time and get it working. Or start over.
    Java Code:
    public class Temperature2
    {
        //you will need to declare an instance variable to store
        // the temperature here.
        //it must be given private access control.
        private double defaultTemp;  // in kelvin for now, your choice
        int celsiusScale    = 'C';
        int fahrenheitScale = 'F';
        int kelvinScale     = 'K';
    
        // If you make these constants [i]static[/i] you can
        // use them in your static conversion methods.
        final static double Abs0Celsius = -273.15;
        final static double Abs0Fahrenheit = -459.67;
        final static double Abs0Kelven = 0;
    
        final static double TriplePoint = 273.16;
        final static double IcePoint = 273.15;
        final static double SteamPoint = 373.13;
    
        // sets to a default temperature value of zero degrees C
        public Temperature2()
        {
            defaultTemp = celsiusToKelvin(0);
        }
    
        // sets to t degrees in the scale represented by the scale
        // parameter (use 'c' or 'C' for Celsius, 'f' or 'F' for
        // Fahrenheit, and 'k' or 'K' for Kelvin). If the scale
        // parameter is not interpretable or the temperature specified
        // is not a physically possible temperature, your constructor
        // should set the temperature to the default value specified
        // for the default constructor above (you can see how to deal
        // with this situation in a more reasonable fashion by using
        // exception handling (in chapter 8). For now, we don't want
        // to allow the creation of an object that represents an
        // "impossible" temperature.). 
        public Temperature2(double t, char scale)
        {
            switch(scale)
            {
                case 'C':
                    defaultTemp = celsiusToKelvin(t);
                    break;
                case 'F':
                    // convert "t" from fahrenheit to kelvin
                    // and assign the value to "defaultTemp"
                    break;
                case 'K':
                    defaultTemp = t;
                    break;
                default:
                    // scale value not interpretable
                    defaultTemp = celsiusToKelvin(0);
            }
            if(defaultTemp < 0)  // out of bounds
                defaultTemp = celsiusToKelvin(0);
        }
    
    
        // Each of the set methods must return the value true if the
        // set operation was successful and false otherwise. Your set
        // methods must not allow temperatures that are not physically
        // realizable (below zero Kelvin).  You must check the input
        // values and return false if they are temperatures that are
        // below zero Kelvin (absolute zero).
        public boolean setF(double tInFahrenheit)
        {
            double k = fahrenheitToKelvin(tInFahrenheit);
            if(k >= 0)
                defaultTemp = k;
            return k >= 0;
        }
    
        public boolean setC(double tInCelsius)
        {
            // Replace this default value with your implementation.
            return true;
        }
    
        public boolean setK(double tInKelvin)
        {
            return true;
        }
    
    
        // These get methods will return the temperature represented
        // by the object in the units indicated. You will need to
        // convert from whatever your internal representation is to
        // the appropriate units (Fahrenheit, Celsius, or Kelvin). 
        public double getF()
        {
            // This value is placed here so you can compile this class.
            // Replace it with your implementation of this method.
            return 0.0;
        }
    
        public double getC()
        {
            return kelvinToCelsius(defaultTemp);
        }
    
        public double getK()
        {
            return 0.0;
        }
    
        public boolean equals(Temperature t)
        {
            return true;
        }
    
        public boolean greaterThan(Temperature t)
        {
            return true;
        }
    
        public boolean lessThan(Temperature t)
        {
            return true;
        }
    
        public static double fahrenheitToCelsius(double degreesF)
        {
            return (degreesF - 32.0) * 5 / 9;
        }
    
        public static double fahrenheitToKelvin(double degreesF)
        {
            return (degreesF + 459.67) * 5/9;
        }
    
        public static double celsiusToFahrenheit(double degreesC)
        {
            return 9.0*degreesC/5.0 + 32.0;
        }
    
        public static double celsiusToKelvin(double degreesC)
        {
            return degreesC + 273.15;
        }
    
        public static double kelvinToCelsius(double degreesK)
        {
            return degreesK - 273.15;
        }
    
        public static double kelvinToFahrenheit(double degreesK)
        {
            return //(degreesK - 273.15) * 9/5 + 32 ;
                   9*degreesK/5 + Abs0Fahrenheit;
        }
    
        public static void main(String[] args) {
            // Test your methods like this. Make sure each one works
            // the way you want. After you get some of these ready
            // you can switch to using the TestTemperature class.
            double t = Temperature.celsiusToFahrenheit(18.0);
            System.out.printf("t = %.1f%n", t);
            // Test default constructor.
            Temperature2 t2 = new Temperature2();
            System.out.printf("t2.getF = %.2f%n", t2.getC());
            boolean okay = t2.setF(75);
            System.out.println("okay = " + okay);
            System.out.printf("t2.getC = %.2f%n", t2.getC());
            // Test the constructor with different values...
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •