Please answer the question correctly showing the correct code and the out put. T
ID: 3859228 • 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
//Temapareture program
import java.util.*;
class farenheattemparetureToCelsius {
public static void main(String[] args) {
float temperatue;
Scanner in = new Scanner(System.in);
System.out.println("Enter temperatue in farenheattempareture");
temperatue = in.nextInt();
temperatue = ((temperatue - 32)*5)/9;
System.out.println("Temperatue in Celsius = " + temperatue);
}
}
------------------
//same one but in c program
#include <stdio.h>
int main()
{
float celsius, farenheattempareture;
// Reads temperature in celsius
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
// celsius to farenheattempareture conversion formula
farenheattempareture = (celsius * 9 / 5) + 32;
printf("%.2f Celsius = %.2f farenheattempareture", celsius, farenheattempareture);
return 0;
}