Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer the question correctly showing the correct code and the out put. Temperature.java and MaxTemp.java are both given. Temperature Complete the provided Temperature class.

Please answer the question correctly showing the correct code and the out put. Temperature.java and MaxTemp.java are both given.

Temperature

Complete the provided Temperature class. Add any attributes and helper methods as needed. You must complete the constructors and methods in the provided class (without changing any signatures, return types, or modifiers).

In this problem you will need to be able to convert temperatures between Celsius, Fahrenheit and Kelvin. For help, see

https://en.wikipedia.org/wiki/Conversion_of_units_of_temperature

A temperature object holds a single temperature and displays it in one of the three scales. Once a scale has been set, it will display the temperature in that scale until changed. The default scale is Celsius if not specified.

Examples:

Temperature t = new Temperature(10.1); System.out.println(t.getScale()); // outputs the char 'C' System.out.println(t.temp()); // outputs 10.1C t.setScale("F"); System.out.println(t.temp()); // outputs 50.18F System.out.println(t.getScale()); // outputs the char 'F' 

Note: Repeatedly changing the scale should not "change" the value of the temperature. For example,

Temperature t = new Temperature(10.1); for(int i=0; i<10000; i+=1){ t.setScale("F"); t.setScale("C"); } System.out.println(t.temp()); // should output 10.1C 

Note: You should have no static attributes or methods in your class.

Extreme Temperatures

Complete the MaxTemp class. The class consists of a single constructor and a single getter method.

The constructor takes an array of Temperature objects as input.

The getter method either returns an array of doubles with exactly two doubles in it or returns null. If the object was created with one more more Temperature objects in the constructor's input array then the output consists of the maximum temperature of all Temperature objects passed to the constructor and a count of how many times that maximum was present in the array passed to the constructor (in that order). If zero Temperature objects were passed to the constructor (in the array) then the getter returns null.

Note: The max temperature returned must be displayed in the Kelvin scale.

Note: Different Temperature objects in the array passed to the constructor may have different temperature scales set for themselves.

Since the Temperature objects will store a floating point number for the temperature, you will use the provided EPSILON constant in the MaxTemp class and consider two temperatures as equal if their absolute difference is smaller than EPSILON. Therefore, if Math.abs(temp1 - temp2) < ESILON then temp1 and temp2 are considered equal. Note: different Temperature objects passed to the constructor

For example, if the array {new Temperature(101.12, "K"), new Temperature(200.0, "F"), new Temperature(101.11, "K")} is passed to the constructor, and EPSILON = 0.1, then the getMax getter will return [101.12, 2.0].

The return value should still display the maximum temperature (in K). In the example above, even though we consider 101.11 and 101.12 the "same", 101.12 is still the max to be returned.

Temperature.java

public class Temperature{
// names for the different temperature scales
public static String[] scales = {"Celsius", "Fahrenheit", "Kelvin"};
/* ----------------------------------------------------
* constructors
* ----------------------------------------------------
*/
public Temperature(double temp){
// - creates a temperature object with given value in Celsius
}
public Temperature(double temp, String scale){
// - creates a temperature object with given temp using the given scale
// - scale must be one of the three strings in the scales attribute
// OR the the first letter of the name (upper case)
// Examples: new Temperature(12.3, "K")
// new Tempersture(-90.2, "Celsius")
}
/* ----------------------------------------------------
* methods
* ----------------------------------------------------
*/
public char getScale(){
// - returns the current scale of the object
// as a single character
// 'C' for Celsuius, 'F' for Fahrenheit, 'K' for Kelvin
// add your code and return the correct char
return 'X';
}
public double getTemp(){
// - returns the object's temperature using its current scale
return -50000000.1;
}
public void setScale(String scale){
// - scale must be one of the three strings in the scales attribute
// OR the first letter of the name (see the second constructor)
// - sets the scale of the current object
}
public void setTemp(double temp){
// - sets the objects temperature to that specified using the
// current scale of the object
}
public void setTemp(double temp, char scale){
// - sets the objects temperature to that specified using the
// specified scale ('K', 'C' or 'F')
}
public void setTemp(double temp, String scale){
// - sets the objects temperature to that specified using the
// specified scale
// - scale must be one of the three strings in the scales attribute
// OR the the first letter of the name (upper case)
}
/* do not change anything below this line */
/* -------------------------------------- */
/* string representation of object */
public String temp(){
return "" + this.getTemp() + this.getScale();
}
/* Override the toString() method */
/* with this we will not need the temp() method from above */
/* we will cover this soon! */
/* you do not need to use this for this assignment! */
@Override
public String toString(){
return "" + this.getTemp() + this.getScale();
}

}

MaxTemp.java

public class MaxTemp{
/** t1 and t2 are considered the same if Math.abs(t1-t2) < EPSILON */
public static final double EPSILON = 0.01;
/* add attributes as you need */
/* ----------------------------------------------------
* constructor
* ----------------------------------------------------
*/
public MaxTemp(Temperature t){
// add your code here
}
/* ----------------------------------------------------
* getter
* ----------------------------------------------------
*/
public double[] getMax(){
// - returns null if empty array was passed to constructor
// - returns null if null was passed to constructor
// - otherwise, returns an array of length 2 [max, count]
// where max is the maximum temperature (expressed in the Kelvin scale)
// of all Temperature objects passed to the constructor, and count
// is the number of times that temperature was present (in the input
// array of the constructor)
// add your code here
return new double[]{0.1, 0.2, 0.3, 0.4};
}
/* OPTIONAL - use your main method to test your code */
public static void main(String[] args){
// testing code here is optional
}
}

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

The Database Relational Model A Retrospective Review And Analysis

Authors: C. J. Date

1st Edition

0201612941, 978-0201612943

More Books

Students also viewed these Databases questions

Question

=+4 What are non-union workers representations?

Answered: 1 week ago