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
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), and 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 greaterThan(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 appropriate values for each possible scale. The set method is a void method. This private set() method can be used to guarantee temperature values are in the proper range.
The add(), subtract(), multiply(), and divide() methods can call the constructor which calls the set() method.
The set method will check the degree value and if it is in the proper range a new Temperature will be made to be returned by the add() method, subtract() method, multiply() method and divide() method.
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 Celsius -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.
Part two
Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where arrays can be used. After the topic of arrays has been covered create arrays of class Temperature. Write a program that satisfies the next 6 requirements using Temperatures. Create a class similar to the Math class. Put the next 5 static methods in it. (These static methods could also be in the Demo class.) On my website are two example programs that demonstrate passing arrays and returning arrays: Passing Arrays using static methods and ChangeArgumentDemo. Use the random number generator as described on the bottom of page 412 to create 3 arrays of random sizes of 1 to 5 elements.
1) Create a static void method that has an array parameter and is called 3 times to read in Temperature values for each array.
2) Create a static method that computes and returns the average Temperature for each array and is also called 3 times.
3) Create a static method that prints the Temperatures of an array.
4) Create a static helper method that has 3 array parameters and either returns the largest array or the largest size.
5) Create a static method that returns an array of Temperatures that has the same number of elements as the largest of the three arrays. This method will have 3 array parameters and possibly an integer parameter. It determines the largest Temperature value from the three arrays at each index and creates a copy of this Temperature and stores it at that index of the new array. This array is then returned.
6) As this program is running it should generate user friendly text for input and output explaining what the program is doing.
Create a set of test value Temperatures that demonstrate that your program runs correctly.
Here is the Demo
public class TemperatureDemoWithoutArrays { public static void main(String[] args) { Temperature temp1 = new Temperature(100.0, 'C'); Temperature temp2 = new Temperature(122, 'F'); Temperature temp3 = new Temperature(32.0, 'F'); Temperature temp4 = new Temperature(100.0, 'C'); Temperature temp5 = new Temperature(212, 'F'); System.out.println("temp1 is " + temp1); System.out.println("temp5 is " + temp5); System.out.println("The sum of temp1 and temp5 should be a Fahrenheit value of 424f,"); Temperature sum = new Temperature(); sum = temp5.add(temp1); System.out.println("The sum of temp1 and temp5 is " + sum); System.out.println(" Temp2 is " + temp2 + " which is 50C."); System.out.println("temp2 to Celcius is " + temp2.toCelsius()+ "."); System.out.println(" Temp1 is " + temp1 + " and the Kalvin value is 373.15 " ); temp1 = temp1.toKelvin(); System.out.println("Temp1 to Kalvin is " + temp1); System.out.println(" The temperature 122F and 50C are the same."); Temperature tempAve = new Temperature(50.0, 'C'); System.out.println("temp2 is " + temp2); System.out.println("tempAve is " + tempAve); System.out.println("Are temp2 and tempAve the same?"); if (temp2.equals(tempAve)) { System.out.println("Yes, these two temperatures are equal."); } else { System.out.println("No, these two temperatures are not equal."); } tempAve = new Temperature(0.0, 'C'); System.out.println(" tempAve is " + tempAve); System.out.println("temp1 is " + temp1); System.out.println("temp2 is " + temp2); System.out.println("temp3 is " + temp3); System.out.println("temp5 is " + temp5); System.out.println("The average of these 5 temperatures should be 50C"); tempAve = tempAve.add(temp1); tempAve = tempAve.add(temp2); tempAve = tempAve.add(temp3); tempAve = tempAve.add(temp4); tempAve = tempAve.divide(5); System.out.println("The average temperature is " + tempAve); System.out.println(" Test the greaterThan() method."); System.out.println("The first temperature is temp4 and is " + temp4); System.out.println("The second temperature is temp2 and is " + temp2); System.out.println("Is temp4 greater than temp2?"); if (temp4.greaterThan(temp2)) { System.out.println("Yes, temp4 is greater then temp2."); } else { System.out.println("No, temp4 is not greater then temp2."); } } }
Here is the the first part (temperature class) Please help me part two.
import java.util.Scanner; public class Temperature{ double temperature; char degree;
public Temperature(){} public Temperature(double temperature,char degree){ this.temperature = temperature; this.degree = degree; }
Temperature toCelsius(){ Temperature temp = new Temperature(); temp.degree='C'; if(degree == 'K' || degree =='k'){ temp.temperature = temperature - 273.15; }else if(degree == 'F' || degree =='f'){ temp.temperature = (temperature -32.0) *(5.0/9.0); }else{ temp.temperature = temperature; }
return temp; }
Temperature toKelvin(){ Temperature temp = new Temperature(); temp.degree='K'; if(degree == 'C' || degree =='c'){ temp.temperature = temperature + 273.15; }else if(degree == 'F' || degree =='f'){ temp.temperature = ((temperature - 32.0) *(5.0/9.0)) + 273.15; }else{ temp.temperature = temperature; } return temp; }
Temperature toFahrenheit(){ Temperature temp = new Temperature(); temp.degree='F'; if(degree == 'C' || degree =='c'){ temp.temperature = (temperature * (9.0/5.0)) + 32.0; }else if(degree == 'K' || degree =='k'){ temp.temperature = ((temperature - 273.15) * (9.0/5.0)) + 32.0; }else{ temp.temperature = temperature; } return temp; }
Temperature add(Temperature t){ Temperature temp = new Temperature(); if(this.degree=='C'){ temp = t.toCelsius(); }else if(this.degree =='F'){ temp = t.toFahrenheit(); }else if(this.degree =='K'){ temp = t.toKelvin(); }
temp.temperature = this.temperature + temp.temperature; return temp; }
Temperature subtract(Temperature t){ Temperature temp = new Temperature(); if(this.degree=='C'){ temp = t.toCelsius(); }else if(this.degree =='F'){ temp = t.toFahrenheit(); }else if(this.degree =='K'){ temp = t.toKelvin(); }
temp.temperature = this.temperature - temp.temperature; return temp;
}
Temperature multiply(Temperature t){ Temperature temp = new Temperature(); if(this.degree=='C'){ temp = t.toCelsius(); }else if(this.degree =='F'){ temp = t.toFahrenheit(); }else if(this.degree =='K'){ temp = t.toKelvin(); }
temp.temperature = this.temperature * temp.temperature; return temp; }
Temperature divide(double d){ Temperature temp = new Temperature(); temp.temperature = this.temperature / d; temp.degree = this.degree; return temp; }
boolean equals(Temperature t){ Temperature temp = new Temperature(); if(this.degree=='C'){ temp = t.toCelsius(); }else if(this.degree =='F'){ temp = t.toFahrenheit(); }else if(this.degree =='K'){ temp = t.toKelvin(); } if(temp.temperature == this.temperature){ return true; }else{ return false; } }
boolean greaterThan(Temperature t){ Temperature temp = new Temperature(); if(this.degree=='C'){ temp = t.toCelsius(); }else if(this.degree =='F'){ temp = t.toFahrenheit(); }else if(this.degree =='K'){ temp = t.toKelvin(); } if(this.temperature > temp.temperature){ return true; }else{ return false; } }
public void read(){ Scanner sc = new Scanner(System.in);
System.out.print("Enter Temperature: "); this.temperature = sc.nextDouble(); System.out.print("Enter Degree(C,F,K): "); sc.nextLine(); char d = sc.nextLine().charAt(0); if(d=='C' || d=='c' || d=='K' || d=='k'|| d=='F' || d=='f'){ this.degree = d; }else{ System.out.println("Wrong degree entered"); } }
public String toString(){ return temperature +" "+degree; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
