In: Computer Science
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements.
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. The returned type is that of the this. 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
public class TemperatureDemoWithoutArrays
{
public static final int ARRAY_SIZE = 5;
public static void main(String[] args)
{
int x;
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 tempAve = new Temperature(50.0, 'C');
System.out.println(temp2 + " to Celcius is " +
temp2.toCelsius());
System.out.println("Temp1 is " + temp1);
temp1 = temp1.toKelvin();
System.out.println("Temp1 to Kalvin is " + temp1);
if (temp2.equals(tempAve))
{
System.out.println("These two temperatures are equal");
}
else
{
System.out.println("These two temperature are not equal");
}
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("temp4 is " + temp4);
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);
temp2 = new Temperature(150.0, 'k');
temp4 = new Temperature(100.0, 'c');
System.out.print("Subtracting " + temp2 + " from " + temp4 +" gives
" );
temp4 = temp4.subtract(temp2);
System.out.println(temp4);
}
}
There are many problems in your testing class. I have implemented exactly as required.
Code:
Temperature.java
public class Temperature {
private double temperature;
private char type;
public Temperature(){
temperature=0;
type='C';
}
public Temperature(double temperature,char
type){
this.type=type;
set(temperature);
}
public double getTemperature(){
return temperature;
}
public char getType(){
return type;
}
private void set(double temperature){
double temp;
switch(this.type){
case 'C':
if(temperature<-273.15){
System.out.println("Celcius temperature should
not be less than 273.15.\nSetting to minimum value.");
temp=-273.15;
}
else{
temp=temperature;
}
break;
case 'F':
if(temperature<-459.67){
System.out.println("Fahrenheit temperature
should not be less than 459.67.\nSetting to minimum value.");
temp=-459.67;
}
else{
temp=temperature;
}
break;
case 'K':
if(temperature<0){
System.out.println("Kelvin temperature should
not be less than 0.\nSetting to minimum value.");
temp=0;
}
else{
temp=temperature;
}
break;
}
this.temperature=temperature;
}
public Temperature toCelsius(){
double temp=this.temperature;
switch(this.type){
case 'C':
temp=this.temperature;
break;
case 'F':
temp=(this.temperature-32)*(5/9.0);
break;
case 'K':
temp=(this.temperature-273.15);
break;
}
Temperature obj=new
Temperature(temp,'C');
return obj;
}
public Temperature toFahrenheit(){
double temp=this.temperature;
switch(this.type){
case 'F':
temp=this.temperature;
break;
case 'C':
temp=(this.temperature*1.8)+32;
break;
case 'K':
temp=(this.temperature*9/5-459.67);
break;
}
Temperature obj=new
Temperature(temp,'F');
return obj;
}
public Temperature toKelvin(){
double temp=this.temperature;
switch(this.type){
case 'K':
temp=this.temperature;
break;
case 'C':
temp=(this.temperature+273.15);
break;
case 'F':
temp=(this.temperature+459.67)*5/9.0;
break;
}
Temperature obj=new
Temperature(temp,'F');
return obj;
}
public Temperature add(Temperature obj){
double temp=this.temperature;
switch(this.type){
case 'K':
temp=this.temperature+obj.toKelvin().getTemperature();
break;
case 'C':
temp=this.temperature+obj.toCelsius().getTemperature();
break;
case 'F':
temp=this.temperature+obj.toFahrenheit().getTemperature();
break;
}
Temperature obj2=new
Temperature(temp,this.type);
return obj2;
}
public Temperature subtract(Temperature obj){
double temp=this.temperature;
switch(this.type){
case 'K':
temp=this.temperature-obj.toKelvin().getTemperature();
break;
case 'C':
temp=this.temperature-obj.toCelsius().getTemperature();
break;
case 'F':
temp=this.temperature-obj.toFahrenheit().getTemperature();
break;
}
Temperature obj2=new
Temperature(temp,this.type);
return obj2;
}
public Temperature multiply(Temperature obj){
double temp=this.temperature;
switch(this.type){
case 'K':
temp=this.temperature*obj.toKelvin().getTemperature();
break;
case 'C':
temp=this.temperature*obj.toCelsius().getTemperature();
break;
case 'F':
temp=this.temperature*obj.toFahrenheit().getTemperature();
break;
}
Temperature obj2=new
Temperature(temp,this.type);
return obj2;
}
public Temperature divide(double d){
double
temp=this.temperature/d;
Temperature obj2=new
Temperature(temp,this.getType());
return obj2;
}
public boolean equals(Temperature obj){
switch(this.type){
case 'K':
return
(this.temperature==obj.toKelvin().getTemperature());
case 'C':
return
(this.temperature==obj.toCelsius().getTemperature());
case 'F':
return
(this.temperature==obj.toFahrenheit().getTemperature());
}
return true;
}
public boolean greaterThan(Temperature obj){
switch(this.type){
case 'K':
return
(this.temperature>=obj.toKelvin().getTemperature());
case 'C':
return
(this.temperature>=obj.toCelsius().getTemperature());
case 'F':
return
(this.temperature>=obj.toFahrenheit().getTemperature());
}
return true;
}
}
TemperatureDemoWithoutArrays.java
public class TemperatureDemoWithoutArrays{
public static final int ARRAY_SIZE = 5;
public static void main(String[] args)
{
int x;
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 tempAve = new Temperature(50.0,
'C');
System.out.println(temp2.getTemperature() + " to
Celcius is " +
temp2.toCelsius().getTemperature());
System.out.println("Temp1 is " +
temp1.getTemperature());
temp1 = temp1.toKelvin();
System.out.println("Temp1 to Kalvin is " +
temp1.getTemperature());
if (temp2.equals(tempAve))
{
System.out.println("These two temperatures are
equal");
}
else
{
System.out.println("These two temperature are not
equal");
}
System.out.println("tempAve is " +
tempAve.getTemperature());
System.out.println("temp1 is " +
temp1.getTemperature());
System.out.println("temp2 is " +
temp2.getTemperature());
System.out.println("temp3 is " +
temp3.getTemperature());
System.out.println("temp4 is " +
temp4.getTemperature());
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.getTemperature());
temp2 = new Temperature(150.0, 'K');
temp4 = new Temperature(100.0, 'C');
System.out.print("Subtracting " +
temp2.getTemperature() + "K from " + temp4.getTemperature() +"C
gives " );
temp4 = temp4.subtract(temp2);
System.out.println(temp4.getTemperature()+"C");
}
}
Output: