Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Temperature class. the class will have three conversion methods: toCelsi

ID: 3689188 • Letter: W

Question

Write a Temperature class. the class will have three conversion
methods: toCelsius(), toKelvin(), and toFahrenheit(). These methods
will return a temperature in those three scales equal to the this
temperature. Note that the value of this is not changed in these
conversions. In addition to these three conversion methods the
class will have methods add(Temperature), subtract(Temperature),
multiply(Temperature), divide(double). these four methods all
return a Temperature equaled to the respective operation. Note that
the this value is not changed in these operations. Two Boolean
methods equals(Temperature), and greatThan(Temperature) will return
true if the this is greater than the parameter. Your class should
include a read() method and a toString() method. Remember methods
add, subtract, multiply, divide and the three conversion methods
all return a Temperature. Include at least two constructors: a
default constructor and an explicit constructor. YOU MUST USE A
PRIVATE HELPER METHOD CALLED SET() THAT TAKES THE PARAMETERS OF THE
CONSTRUCTOR AND TESTS FOR APPROPIATE VALUES FOR EACH POSSIBLE
SCALE. This private set() method can be used to guarantee
Temperature values are in proper range. the subtract() and divide()
methods can call the constructor to return a Temperature in a legal
range. a switch statement should be used throughout this class when
choosing between "C", "F" and "K". Absolute zero for kelvin is 0,
for Fahrenheit -459.67, and for celcius is -273.15. your program
must guarantee this absolute value is not violated. for the
EQUALS() method consider changing the this Temperature and the
parameter Temperature to the same scale and then testing the degree
value for equality. create 3 array of random sizes of 1-5 elements

-------------------------------------------------------------------------------------------------

here is the demo:

public class TemperatureDemo {
   public static final int ARRAY_SIZE = 5;

   public static void main(String[] args) {
       int x;
       Temperature temp1 = new Temperature(120.0, 'C');
       Temperature temp2 = new Temperature(100, 'C');
       Temperature temp3 = new Temperature(50.0, 'C');
       Temperature temp4 = new Temperature(232.0, 'K');
       Temperature tempAve = new Temperature(0.0, 'C');
       Temperature[] tempArray = new Temperature[ARRAY_SIZE];// create pointer
                                                               // to array
       Temperature t1;
       for (x = 0; x < tempArray.length; x++) {
           t1 = new Temperature();
           tempArray[x] = t1;// fill the array with Temperatures
       }

       System.out.println("Temp1 is " + temp1);
       // temp1 = temp1.toKelvin();
       temp2.toKelvin();
       System.out.println("Temp1 to Kalvin is " + temp1);
       if (temp1.equals(temp3)) {
           System.out.println("These two temperatures are equal");
       } else {
           System.out.println("These two temperature are not equal");
       }
       System.out.println("Temp1 is " + temp1);
       System.out.println("Temp2 is " + temp2);
       System.out.println("Temp3 is " + temp3);
       System.out.println("Temp4 is " + temp4);

       tempAve = tempAve.add(temp1);
       tempAve = tempAve.add(temp2);
       tempAve = tempAve.add(temp3);
       tempAve = tempAve.add(temp4);
       tempAve = tempAve.divide(4);
       System.out.println("the average temperatrure is " + tempAve);

       Temperature[] temperatureArrayOne;
       Temperature[] temperatureArrayTwo;
       Temperature[] temperatureArrayThree;
       temperatureArrayOne = new Temperature[getRandomArraySize()];
       readTemperatureArray(temperatureArrayOne);
       printTemperatureArray(temperatureArrayOne);
       t1 = getAverage(temperatureArrayOne);
       System.out.println("the average of temperature array one is " + t1);
       temperatureArrayTwo = new Temperature[getRandomArraySize()];
       readTemperatureArray(temperatureArrayTwo);
       printTemperatureArray(temperatureArrayTwo);
       t1 = getAverage(temperatureArrayTwo);
       System.out.println("the average of temperature array two is " + t1);
       temperatureArrayThree = new Temperature[getRandomArraySize()];
       readTemperatureArray(temperatureArrayThree);
       printTemperatureArray(temperatureArrayThree);
       t1 = getAverage(temperatureArrayThree);
       System.out.println("the average of temperature array three is " + t1);
       Temperature[] largest = getLargestArray(temperatureArrayOne, temperatureArrayTwo, temperatureArrayThree);
       Temperature[] arrayWithLargestValues;
       if (temperatureArrayOne == largest)
           arrayWithLargestValues = createArrayWithLargestValues(largest, temperatureArrayTwo, temperatureArrayThree);
       else if (temperatureArrayTwo == largest)
           arrayWithLargestValues = createArrayWithLargestValues(largest, temperatureArrayOne, temperatureArrayThree);
       else// fractionArrayThree is largest
           arrayWithLargestValues = createArrayWithLargestValues(largest, temperatureArrayOne, temperatureArrayTwo);
       System.out.println("An array with the largest values from the 3 arrays is");
       printTemperatureArray(arrayWithLargestValues);
   }

}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

here is how output should look like

thank you

Explanation / Answer

public class Temperature { public double temperature; public char scale; public Temperature(double temperature, char scale) { this.temperature=temperature; this.scale=scale; } public Temperature() { } public Temperature toKelvin() { double converted_Temp; if(scale == 'C') { converted_Temp=(temperature+273.15); Temperature temp = new Temperature(converted_Temp, 'K'); return temp; } if(scale == 'F') { converted_Temp=(double)((temperature-32)*5.0/9.0+273.15); Temperature temp = new Temperature(converted_Temp, 'K'); return temp; } if(temperature < 0) { System.out.println("K cannot be < 0"); System.exit(0); } return this; } public Temperature toCelsius() { double converted_Temp; if(scale == 'K') { converted_Temp=temperature-273.15; Temperature temp = new Temperature(converted_Temp, 'C'); return temp; } if(scale == 'F') { converted_Temp=(double)((temperature-32)*5.0/9.0); Temperature temp = new Temperature(converted_Temp, 'C'); return temp; } if(temperature < -273.15) { System.out.println("C cannot be < -273.15"); System.exit(0); } return this; } public Temperature toFahrenheit() { double converted_Temp; if(scale == 'K') { converted_Temp=(double)(temperature -273.15)*9.0/5.0+32; Temperature temp = new Temperature(converted_Temp, 'F'); return temp; } if(scale == 'C') { converted_Temp=(double)(temperature*9.0/5.0 +32); Temperature temp = new Temperature(converted_Temp, 'F'); return temp; } if(temperature < -459.67) { System.out.println("F cannot be < -459.67"); System.exit(0); } return this; } public Temperature add(Temperature n) { Temperature temp1 = this.toKelvin(); Temperature temp2 = n.toKelvin(); return new Temperature(temp1.temperature+temp2.temperature, 'K'); } public Temperature subtract(Temperature n) { Temperature temp1 = this.toKelvin(); Temperature temp2 = n.toKelvin(); return new Temperature(temp1.temperature-temp2.temperature, 'K'); } public Temperature multiply(Temperature n) { Temperature temp1 = this.toKelvin(); Temperature temp2 = n.toKelvin(); return new Temperature(temp1.temperature*temp2.temperature, 'K'); } public Temperature divide(double d) { Temperature temp1 = this.toKelvin(); double new_temp=(double)(temp1.temperature/d); return new Temperature(new_temp, 'K'); } public boolean equals(Temperature n) { return this.temperature==n.temperature&&this.scale==n.scale; } public String toString() { return "Temperature{" + "temperature=" + temperature + "scale=" + scale + '}'; } public void read() { System.out.println(this); } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote