From d3621dd5f414e522b90ac809709a9ac867181570 Mon Sep 17 00:00:00 2001 From: Justin Musgrove Date: Sun, 1 Jun 2014 14:02:00 -0500 Subject: [PATCH] added boiling and freezing point exercises --- .../beginner/FreezingAndBoilingPoints.java | 151 ++++++++++++++++++ .../FreezingAndBoilingPointsTest.java | 77 +++++++++ 2 files changed, 228 insertions(+) create mode 100644 src/main/java/com/levelup/java/exercises/beginner/FreezingAndBoilingPoints.java create mode 100644 src/test/java/com/levelup/java/exercises/beginner/FreezingAndBoilingPointsTest.java diff --git a/src/main/java/com/levelup/java/exercises/beginner/FreezingAndBoilingPoints.java b/src/main/java/com/levelup/java/exercises/beginner/FreezingAndBoilingPoints.java new file mode 100644 index 0000000..a7b6257 --- /dev/null +++ b/src/main/java/com/levelup/java/exercises/beginner/FreezingAndBoilingPoints.java @@ -0,0 +1,151 @@ +package com.levelup.java.exercises.beginner; + +import java.util.Scanner; + +/** + * This java example will demonstrate a solution to the Freezing and boiling exercise. + * + * @author Justin Musgrove + * @see Freezing and Boiling Points + */ +public class FreezingAndBoilingPoints { + + 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; + } + } + } + + 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."); + } + } + +} diff --git a/src/test/java/com/levelup/java/exercises/beginner/FreezingAndBoilingPointsTest.java b/src/test/java/com/levelup/java/exercises/beginner/FreezingAndBoilingPointsTest.java new file mode 100644 index 0000000..fc671d7 --- /dev/null +++ b/src/test/java/com/levelup/java/exercises/beginner/FreezingAndBoilingPointsTest.java @@ -0,0 +1,77 @@ +package com.levelup.java.exercises.beginner; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.levelup.java.exercises.beginner.FreezingAndBoilingPoints.FreezeAndBoiling; + +/** + * Unit test for {@link FreezingAndBoilingPoints} + * + * @author Justin Musgrove + * @see Freezing and Boiling Points + */ +public class FreezingAndBoilingPointsTest { + + @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()); + } + +}