Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA: Its Getting Hot package cs5004.temperature; /** * Represents a temperature. */ public interface Temperature extends Comparable { /** * Absolute zero, in degrees Celsius.

JAVA: Its Getting Hot

image text in transcribed

package cs5004.temperature; /** * Represents a temperature. */ public interface Temperature extends Comparable { /** * Absolute zero, in degrees Celsius. For our purposes, no temperature can be below * this value. */ double ABS_ZERO_C = -273.15f; /** * The temperature in degrees Celsius. * * @return the temperature in degrees Celsius */ double inCelsius(); /** * The temperature in degrees Fahrenheit. * * @return the temperature in degrees Fahrenheit */ double inFahrenheit(); /** * The temperature in degrees Kelvin. * * @return the temperature in degrees Kelvin */ double inKelvin(); /** * Add two temperatures together and return the resulting temperature. * * @return the new temperature */ Temperature add(Temperature t); }

import org.junit.Before; import org.junit.Test; import cs5004.temperature.CelsiusTemperature; import cs5004.temperature.FahrenheitTemperature; import cs5004.temperature.Temperature; import static org.junit.Assert.assertEquals; /** * Tests for Temperature, both Celsius and Fahrenheit representations. */ public class TemperatureTest { private Temperature cTemp; private Temperature fTemp; @Before public void init() { cTemp = new CelsiusTemperature(100); fTemp = new FahrenheitTemperature(100, true); } @Test(expected = IllegalArgumentException.class) public void testForInvalidFTemp() { new FahrenheitTemperature(-1000); } @Test public void testObservers() { assertEquals(100, cTemp.inCelsius(), 0.001); assertEquals(212, cTemp.inFahrenheit(), 0.001); assertEquals(373.15, cTemp.inKelvin(), 0.001); } @Test public void testInF() { assertEquals(212, fTemp.inFahrenheit(), 0.001); } @Test public void testAdd() { // test your add() method here according to its specification } @Test public void testToString() { assertEquals("100.0 Celsius", cTemp.toString()); assertEquals("212.0 Fahrenheit", fTemp.toString()); } }
1.1 It's Getting Hot The purpose of this exercise is to give you practice with the new concepts introduced through the Shape example, such as inheritance and interface polymorphism, along with the default (no keyword) and protected access modifiers. We are also introducing a new concept, the idea of static and static methods. In the starter code, you are given an interface representing a temperature value. Your task is to implement the Temperature interface in two different ways: using a field to represent the temperature in degrees Celsius. using a field to represent the temperature in degrees Fahrenheit. You will need two separate classes: a public classed named CelsiusTemperature, and a public class named FahrenheitTemperature. Each class will need two constructors: CelsiusTemperature will have one constructor that takes a single double representing temperature in Celsius; the other constructor will take a double representing temperature in Fahrenheit and a boolean whose value must be true. Conversely, FahrenheitTemperature will have one constructor that takes a single double representing temperature in Fahrenheit; its other constructor will take a double representing temperature in Celsius and a boolean whose value must be true. Question for discussion: What's the point of this second constructor argument, if its value is always true? All implementations should override toString(). The toString() method should return a String as follows: "NN.N Fahrenheit", namely, the temperature to one decimal place precision followed by the degree symbol and Fahrenheit, e.g., "30.1 Fahrenheit", for the FahrenheitTemperature class, and similarly for the CelsiusTemperature class but substituting "Celsius" for "Fahrenheit". You may want to copy and paste the degree symbol ( from this Web page into IntelliJ. You may find it useful to use String.format("%.1f") to convert a double to a string with one decimal place. The formulas for converting between Celsius and Fahrenheit are as follows: [F] = [C] x % +32 [C] = ([F] 32) 5, The formula for converting from Celsius (C) to Kelvin (K) is: K= C - ABS_ZERO_C Where ABS_ZERO_c represents absolute zero in degress Celsius, namely -273.1C. This value is provided for you as a (static) constant in the Temperature interface. Note about the add() method: Adding temperatures together has no clear interpretation physically; you may decide what the most appropriate behavior for this method is. Document the behavior of your add() method by modifying its JavaDoc in Temperature.java as necessary

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Database Systems

Authors: Carlo Zaniolo, Stefano Ceri, Christos Faloutsos, Richard T. Snodgrass, V.S. Subrahmanian, Roberto Zicari

1st Edition

155860443X, 978-1558604438

More Books

Students also viewed these Databases questions

Question

b. What groups were most represented? Why do you think this is so?

Answered: 1 week ago

Question

3. Describe phases of minority identity development.

Answered: 1 week ago

Question

5. Identify and describe nine social and cultural identities.

Answered: 1 week ago