Class TemperatureCelsius provides constants and functions to work with temperatures in Celsius.

// the class provides various static function related 
// to temperatures in Celsius
public class TemperatureCelsius {
    public final static double boiling = 100.0;
    public final static double freezing = 0.0;

    public static double fromFahrenheit(double fahr) {
	return ((fahr - 32)/9.0 * 5.0 );
    }

    public static double toFahrenheit(double cels) {
	return cels * 9.0 / 5.0 + 32;
    }

    
}
Here is the test program for this class. Note that no objects are created, methods are used similarly to the Math methods.

public class TemperatureTest {
    public static void main (String [] args) {
	double temp = 44;
	if (TemperatureCelsius.fromFahrenheit(temp) 
	    < TemperatureCelsius.freezing) {
	    System.out.println("Brrr...");
	} else {
	    System.out.println("Nice and warm!");
	}
    }
}

This is an example from CSci 1211 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.