Tuesday, September 26, 2017

Programming Challenge 13.16 - Freezing and Boiling Points

Example Files: FreezingAndBoilingPoints.h
                          FreezingAndBoilingPoints.cpp
                          FreezingAndBoilingPointsDm.cpp

FreezingAndBoilingPoints.h


#ifndef FREEZING_AND_BOILING_POINTS_H
#define FREEZING_AND_BOILING_POINTS_H

class Chemistry
{
    private:
        int temperature;        // Holds a single temperature

    public:
        Chemistry()
        {
            temperature = 0;
        }

        Chemistry(int temp)
        {
            temperature = temp;
        }

        bool isEthylFreezing();
        bool isEthylBoiling();
        bool isOxygenFreezing();
        bool isOxygenBoiling();
        bool isWaterFreezing();
        bool isWaterBoiling();

        int getTemperature() const
        { return temperature; }
};
#endif

FreezingAndBoilingPoints.cpp


/* FreezingAndBoilingPoints.cpp Implementation file for the Chemistry class. */

#include "FreezingAndBoilingPoints.h"

enum Freezing { ETHYL_FR = -173, OXYGEN_FR = -362, WATER_FR = 32 };
enum Boiling { ETHYL_BO = 172, OXYGEN_BO = -306, WATER_BO = 212 };

/* **********************************************************
            Chemistry::isEthylFreezing()
   If Ethyl is freezing at a certain temperature, true is
    returned from this function, else false is returned.
   ********************************************************** */

bool Chemistry::isEthylFreezing()
{
    if (getTemperature() <= ETHYL_FR)
    { return true; }

    return false;
}

/* **********************************************************
            Chemistry::isOxygenFreezing()
   If Oxygen is freezing at a certain temperature, true is
    returned from this function, else false is returned.
   ********************************************************** */

bool Chemistry::isOxygenFreezing()
{
    if (getTemperature() <= OXYGEN_FR)
    { return true; }

    return false;
}

/* **********************************************************
            Chemistry::isWaterFreezing()
   If Water is freezing at a certain temperature, true is
    returned from this function, else false is returned.
   ********************************************************** */

bool Chemistry::isWaterFreezing()
{
    if (getTemperature() <= WATER_FR)
    { return true; }

    return false;
}

/* **********************************************************
            Chemistry::isEthylFreezing()
   If Ethyl is freezing at a certain temperature, true is
    returned from this function, else false is returned.
   ********************************************************** */

bool Chemistry::isEthylBoiling()
{
    if (getTemperature() >= ETHYL_BO)
    { return true; }

    return false;
}

/* **********************************************************
            Chemistry::isOxygenBoiling()
   If Oxygen is boiling at a certain temperature, true is
    returned from this function, else false is returned.
   ********************************************************** */

bool Chemistry::isOxygenBoiling()
{
    if (getTemperature() >= OXYGEN_BO)
    { return true; }

    return false;
}

/* **********************************************************
            Chemistry::isWaterBoiling()
   If Water is boiling at a certain temperature, true is
    returned from this function, else false is returned.
   ********************************************************** */

bool Chemistry::isWaterBoiling()
{
    if (getTemperature() >= WATER_BO)
    { return true; }

    return false;
}


FreezingAndBoilingPointsDm.cpp


/* FreezingAndBoilingPointsDm.cpp - Demo file for the Chemistry class. */

#include <string>
#include <iostream>
#include "FreezingAndBoilingPoints.h"

using std::string;
using std::cin;
using std::cout;

void isFreezing(Chemistry);
void isBoiling(Chemistry);

int main()
{
    char again = ' ';
    int temperature = 0;

    cout << "FREEZING AND BOILING POINTS\n\n"
         << "Enter a temperature in Fahrenheit, and I will tell you which substances\n"
          << "are freezing and which are boiling at that temperature.\n\n";

    do
    {
        cout << "Enter a temperature: ";
        cin >> temperature;

        Chemistry temp(temperature);

        isFreezing(temp);
        isBoiling(temp);

        cout << "\nDo you wish to try this again (Y/N)? ";
        cin >> again;
        cout << "\n";

        while (toupper(again) != 'Y' && toupper(again) != 'N')
        {
            cout << "\nDo you wish to try this again (Y/N)? ";
            cin >> again;
            cout << "\n";
        }

        if (toupper(again) == 'N')
        {
            cout << "Have a nice day!";
        }
    } while (toupper(again) != 'N');

    cin.get();
    cin.ignore();
    return 0;
}

/* **********************************************************
    isFreezing() accepts a Chemistry object as its argument
   This function outputs each substance that is freezing at a
    certain temperature to screen.
   ********************************************************** */

void isFreezing(Chemistry temp)
{
    cout << "\nThese substances freeze at " << temp.getTemperature() << " degrees F:\n";

    if (temp.isEthylFreezing())
    {
        cout << "\nEthyl";
    }

    if (temp.isOxygenFreezing())
    {
        cout << "\nOxygen";
    }

    if (temp.isWaterFreezing())
    {
        cout << "\nWater";
    }
}

/* **********************************************************
    isBoiling() accepts a Chemistry object as its argument
   This function outputs each substance that is boiling at a
    certain temperature to screen.
   ********************************************************** */

void isBoiling(Chemistry temp)
{
    cout << "\nThese substances boil at " << temp.getTemperature() << " degrees F:\n\n";

    if (temp.isEthylBoiling())
    {
        cout << "Ethyl\n";
    }

    if (temp.isOxygenBoiling())
    {
        cout << "Oxygen\n";
    }

    if (temp.isWaterBoiling())
    {
        cout << "Water\n";
    }
}

Example Output:




No comments:

Post a Comment