Please answer the question correctly showing the correct code and the out put. T
ID: 3859229 • Letter: P
Question
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:
Note: Repeatedly changing the scale should not "change" the value of the temperature. For example,
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
}
MaxTemp.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 } }Explanation / Answer
Temperature.java
---------------------------
package corejava;
public class Temperature {
private char scale;
private float temp;
private int numberOfTimesSetCalled = 0;
public Temperature(float temp) {
this.temp = temp;
}
public Temperature(double temp) {
this.temp = (float) temp;
}
public Temperature(float temp, char scale) {
if (scale == 'C') {
this.temp = (float) temp;
} else if (scale == 'K') {
this.temp = (float) (temp - 273.15);
} else if (scale == 'F') {
this.temp = (float) ((temp * 1.8) + 32);
}
this.scale = scale;
}
public Temperature(double temp, char scale) {
this.scale = scale;
if (this.scale == 'C') {
this.temp = (float) temp;
} else if (this.scale == 'K') {
this.temp = (float) (temp - 273.15);
} else if (this.scale == 'F') {
this.temp = (float) ((temp - 32) / 1.8);
}
this.scale = 'C';
}
public char getScale() {
return scale;
}
public void setScale(char scale) {
if (numberOfTimesSetCalled == 0) {
this.numberOfTimesSetCalled = 1;
this.scale = scale;
} else {
this.scale = 'C';
}
}
public double getTemp() {
numberOfTimesSetCalled = 0;
if (this.scale == 'C') {
return this.temp;
} else if (this.scale == 'K') {
return this.temp + 273.15;
} else if (this.scale == 'F') {
return (this.temp * (9 / 5)) + 32;
}
return temp;
}
public void setTemp(float temp) {
this.temp = temp;
}
public static void main(String[] args) {
Temperature temp = new Temperature(101);
// System.out.println(temp.getTemp());
temp.setScale('F');
// System.out.println(temp.getTemp());
for (int i = 0; i < 5; i++) {
temp.setScale('K');
}
// System.out.println(temp.getTemp());
temp.setScale('K');
// System.out.println(temp.getTemp());
Temperature[] tArray = { new Temperature(101.12, 'K'),
new Temperature(91.0, 'K'), new Temperature(101.11, 'K') };
for (Temperature temperature : tArray) {
System.out.println(" Sacle set :" + temperature.getScale()
+ " Temp Set " + temperature.getTemp());
}
MaxTemp maxTemp = new MaxTemp(tArray);
double[] result = maxTemp.getMax();
if (result != null) {
System.out.println(" Max Temp in Kalvin" + (result[0] + +273.15)
+ " Count " + (int) result[1]);
}
}
}
MaxTemp.java
------------------------
package corejava;
import java.util.Arrays;
public class MaxTemp {
private Temperature[] tempArray;
private double EPSILON = 0.1;
public MaxTemp(Temperature[] tempArray) {
this.tempArray = tempArray;
}
public double[] getMax() {
double max = 0.0;
double maxTempCount = 1;
if (this.tempArray == null) {
return null;
} else {
double[] tempLocalArray = new double[this.tempArray.length];
for (int i = 0; i < tempArray.length; i++) {
tempLocalArray[i] = tempArray[i].getTemp();
}
Arrays.sort(tempLocalArray);
max = tempLocalArray[tempLocalArray.length - 1];
for (int i = 1; i < tempLocalArray.length - 1; i++) {
if(Math.abs(max - tempLocalArray[i]) < this.EPSILON){
maxTempCount++;;
}
}
}
double[] result = new double[2];
result[0] = max;
result[1] = maxTempCount;
return result;
}
}
-------------------- End---------------------------------------
Output
------------------
Sacle set :C Temp Set -172.02999877929688
Sacle set :C Temp Set -182.14999389648438
Sacle set :C Temp Set -172.0399932861328
Max Temp in Kalvin101.1200012207031 Count 2
Description :
--------------------------
1. Intially program will invoke parameterized constructor and set array of Temparture class objects . Temparture constructor takes two argument first will take temparture value and second argument will take the scale of the temprature value. logica return in consturctor performs conversion and convert temparture into celcisus so that whenever setScale method gets invoked, it will retrun the corresponding converted value.
2. Please check the getMax() method of MaxTemp.java which returns the array of double type having length 2 which contains max temprature and their frequency.
3. We need to run main method of Temparature.java class to get the output of the above program.
Please let me know if face any difficulites to make understanding on above given program.