Freezing and Boiling Points

The problem

The following table lists the freezing and boiling points of several substances.

SubstanceFreezing PointBoiling Point
Ethyl Alcohol-173172
Oxygen-362-306
Water32212

Design a class that stores a temperature in a temperature field and has the appropriate accessor andmutator methods for the field. In addition to appropriate constructors, the class should have the following methods:

  • isEthylFreezing. This method should return the boolean value true if the temperature stored in the temperature field is at or below the freezing point of ethyl alcohol. Otherwise, the method should return false.
  • isEthylBoiling. This methodshould return the boolean value true if the temperature stored in the temperature field is at or above the boiling point of ethyl alcohol. Otherwise, the method should return false.
  • isOxygenFreezing. This method should return the boolean value true if the temperature stored in the temperature field is at or below the freezing point of oxygen. Otherwise, the method should return false.
  • isOxygenBoiling. This method should return the boolean value true if the temperature stored in temperature field is at or above the boiling point of oxygen. Otherwise,the method should return false.
  • isWaterFreezing. This method should return the boolean value true if the temperature stored in the temperature field is at or below the freezing point of water. Otherwise, the method should return false.
  • isWaterBoiling. This method should return the boolean value true if the temperature stored in the temperature field is at or above the boiling point of water. Otherwise, the method should return false.

Write a program that demonstrate the class. The program should ask the user to enter a temperature, and then display a list of the substance that will freeze at that temperature and those that will boil at that temperature. For example, if the temperature is -20 the class should report that water will freeze and oxygen will boil at that temperature.

Breaking it down

FreezeAndBoiling class

public class FreezeAndBoiling {

    private double temperature;

    public FreezeAndBoiling(double t) {
        temperature = t;
    }

    public double getTemperature() {
        return temperature;
    }

    /**
     * Method should check if the temperature is freezing
     *
     * @return true if Ethyl is freezing
     */
    public boolean isEthylAlchoolFreezing() {

        if (temperature <= -173.0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Method should check if the temperature is boiling
     *
     * @return true if Ethyl is boiling
     */
    public boolean isEthylAlchoolBoiling() {

        if (temperature >= 172.0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Method should check if the temperature is freezing
     *
     * @return true if Oxygen is freezing
     */
    public boolean isOxygenFreezing() {

        if (temperature <= -362.0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Method should check if the temperature is boiling
     *
     * @return true if Oxygen is boiling
     */
    public boolean isOxygenBoiling() {

        if (temperature >= -306.0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Method should check if the temperature is freezing
     *
     * @return true if Water is freezing
     */
    public boolean isWaterFreezing() {

        if (temperature <= 32.0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Method should check if the temperature is boiling
     *
     * @return true if Water is boiling
     */
    public boolean isWaterBoiling() {

        if (temperature >= 212.0) {
            return true;
        } else {
            return false;
        }
    }
}

Program demonstration

public static void main(String[] args) {

    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter a temperature: ");
    double temperature = keyboard.nextDouble();

    // close keyboard
    keyboard.close();

    FreezingAndBoilingPoints freezeAndBoilingPoints = new FreezingAndBoilingPoints();
    FreezeAndBoiling freezeAndBoiling = freezeAndBoilingPoints.new FreezeAndBoiling(
            temperature);

    // Display elements will freeze.
    if (freezeAndBoiling.isEthylAlchoolBoiling()) {
        System.out.println("Ethyl alcohol will freeze.");
    }

    if (freezeAndBoiling.isOxygenFreezing()) {
        System.out.println("Oxygen will freeze.");
    }

    if (freezeAndBoiling.isWaterFreezing())
        System.out.println("Water will freeze.");

    // Display if elements will boil.
    if (freezeAndBoiling.isEthylAlchoolBoiling()) {
        System.out.println("Ethyl alcohol will boil.");
    }

    if (freezeAndBoiling.isOxygenBoiling()) {
        System.out.println("Oxygen will boil.");
    }

    if (freezeAndBoiling.isWaterBoiling()) {
        System.out.println("Water will boil.");
    }
}

Output

Enter a temperature: 48
Oxygen will boil.

Unit tests

@Test
public void test_isEthylAlchoolFreezing() {

    FreezingAndBoilingPoints freezeAndBoilingPoints = new FreezingAndBoilingPoints();
    FreezeAndBoiling freezeAndBoiling = freezeAndBoilingPoints.new FreezeAndBoiling(
            -174);

    assertTrue(freezeAndBoiling.isEthylAlchoolFreezing());
}

@Test
public void test_isEthylAlchoolBoiling() {

    FreezingAndBoilingPoints freezeAndBoilingPoints = new FreezingAndBoilingPoints();
    FreezeAndBoiling freezeAndBoiling = freezeAndBoilingPoints.new FreezeAndBoiling(
            172);

    assertTrue(freezeAndBoiling.isEthylAlchoolBoiling());
}

@Test
public void test_isOxygenFreezing() {

    FreezingAndBoilingPoints freezeAndBoilingPoints = new FreezingAndBoilingPoints();
    FreezeAndBoiling freezeAndBoiling = freezeAndBoilingPoints.new FreezeAndBoiling(
            -400);

    assertTrue(freezeAndBoiling.isOxygenFreezing());
}

@Test
public void test_isOxygenBoiling() {

    FreezingAndBoilingPoints freezeAndBoilingPoints = new FreezingAndBoilingPoints();
    FreezeAndBoiling freezeAndBoiling = freezeAndBoilingPoints.new FreezeAndBoiling(
            -362);

    assertTrue(freezeAndBoiling.isOxygenFreezing());
}

@Test
public void test_isWaterFreezing() {

    FreezingAndBoilingPoints freezeAndBoilingPoints = new FreezingAndBoilingPoints();
    FreezeAndBoiling freezeAndBoiling = freezeAndBoilingPoints.new FreezeAndBoiling(
            10);

    assertTrue(freezeAndBoiling.isWaterFreezing());
}

@Test
public void test_isWaterBoiling() {

    FreezingAndBoilingPoints freezeAndBoilingPoints = new FreezingAndBoilingPoints();
    FreezeAndBoiling freezeAndBoiling = freezeAndBoilingPoints.new FreezeAndBoiling(
            213);

    assertTrue(freezeAndBoiling.isWaterBoiling());
}

Level Up

  • In each of the freezing or boiling methods, check that the method parameters are valid. Write a unit test to validate the behavior you implemented.
  • There is a consitent pattern of elements on if it will freeze and boil. While the FreezeAndBoiling performs a specific behavior, it isn't set up to adapt to new elements. Refactor existing code to use a Enum.
  • Each of the freezing and boiling methods we wrote a positive test to validate if it met a condition. Write the negative test for each method validating the false behavior.