Write a Temperature class using the Demo below The class wil
Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled 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 threee convesion methods all return a Temperature. Include a least two constructore: a default and a explicit. Use a private helper 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 temperrature valuese are in the 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 throughtout this class when choosong between \"C\", \"K\", and \"F\". Absolute zero for Kelvin is 0, for Fahrenheit -459.67, and fro Calvin -273.15. Your program must guarantee this absolute value is not violated. For the equal() method consider changing the this temperature and the parameter temperature to the same scale and then testing the degree value for equality.
Please use switch
Solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
public class TemperatureDemoWithoutArrays
{
public static 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\');
Console.WriteLine(\"122 to Celcius is \" + temp2.toCelcius(122));
Console.WriteLine(\"Temp1 is \" + temp1.toCelcius(100.0));
Console.WriteLine(\"Temp1 to Kalvin is \" + temp1.toKelvin(100.0));
if (temp2.equals(50.0, \'C\'))
{
Console.WriteLine(\"These two temperatures are equal\");
}
else
{
Console.WriteLine(\"These two temperature are not equal\");
}
Console.WriteLine(\"tempAve is \" + tempAve.toCelcius(50.0));
Console.WriteLine(\"temp1 is \" + temp1.toCelcius(100.0));
Console.WriteLine(\"temp2 is \" + temp2.toFahrenheit(122));
Console.WriteLine(\"temp3 is \" + temp3.toFahrenheit(32.0));
Console.WriteLine(\"temp4 is \" + temp4.toCelcius(100.0));
double temp7 = tempAve.add(temp1.toCelcius(100.0));
temp7 = tempAve.add(temp2.toFahrenheit(122));
temp7 = tempAve.add(temp3.toFahrenheit(32.0));
temp7 = tempAve.add(temp4.toCelcius(100.0));
temp7 = tempAve.divide(5);
Console.WriteLine(\"The average temperatrure is \" + temp7);
Console.WriteLine(\"Subtracting temp2 from temp4 gives \");
Console.WriteLine(temp4.subtract(temp2.toFahrenheit(122)));
Console.ReadLine();
}
}
public class Temperature
{
double temp;
public Temperature()
{
}
public Temperature(double temp, char ch)
{
double myTemp = set(temp, ch);
}
public double set(double temp, char ch)
{
double returnTemp = 0.0;
switch (ch)
{
case \'C\':
returnTemp = toCelcius(temp);
if (returnTemp < 0 && returnTemp > 100)
Console.WriteLine(\"Invalid temperature\");
break;
case \'F\':
returnTemp = toFahrenheit(temp);
if (returnTemp < 32 && returnTemp > 96)
Console.WriteLine(\"Invalid temperature\");
break;
case \'K\':
returnTemp = toKelvin(temp);
if (returnTemp < 0 && returnTemp > 373.16)
Console.WriteLine(\"Invalid temperature\");
break;
}
return returnTemp;
}
public double toCelcius(double temp)
{
double celVal;
celVal = (temp - 32) * 5 / 9;
return celVal;
}
public double toFahrenheit(double temp)
{
double farVal;
farVal = temp * 9/5 +32;
return farVal;
}
public double toKelvin(double temp)
{
double kelVal;
kelVal = temp + 273.15;
return kelVal;
}
public double add(double temp)
{
return this.temp + temp;
}
public double subtract(double temp)
{
return this.temp - temp;
}
public double multiply(double temp)
{
return this.temp * temp;
}
public double divide(double temp)
{
return this.temp / temp;
}
public bool equals(double temp, char ch)
{
double checkTemp = set(temp, ch);
if (this.temp == checkTemp)
return true;
else
return false;
}
public bool greaterThan(double temp)
{
if (this.temp > temp)
return true;
else
return false;
}
public void read()
{
}
public void toString()
{
}
}
}


