SHARE
TWEET

Untitled

a guest Sep 18th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Temperature {
  2.  
  3.     private double temperature;
  4.     private char scale;
  5.  
  6.     Temperature(double temperature, char scale)
  7.     {
  8.  
  9.        set(temperature, scale);
  10.  
  11.     }
  12.  
  13.     private Temperature(double v) {
  14.     }
  15.  
  16.     private void set(double temperature, char scale)
  17.     {
  18.         if(scale == 'K' && temperature < 0 ) {
  19.             System.out.println("Error, Kelvin cannot be less than 0.");
  20.             System.exit(0);
  21.         }if(scale == 'C' && temperature < -273.15) {
  22.         System.out.println("Error, Celsius cannot be less than absolute 0.");
  23.         System.exit(0);
  24.     }if(scale == 'F' && temperature <-459.67){
  25.         System.out.println("Error, Fahrenheit cannot be less than absolute 0.");
  26.         System.exit(0);
  27.     }
  28.         this.temperature=temperature;
  29.         this.scale=scale;
  30.     }
  31.     private Temperature() {
  32.     }
  33.     Temperature toKelvin()
  34.     {
  35.         double converted_Temp;
  36.  
  37.         switch(scale) {
  38.             case 'C': {
  39.                 converted_Temp = (temperature + 273.15);
  40.                 return new Temperature(converted_Temp, 'K');
  41.             }
  42.  
  43.             case 'F': {
  44.                 converted_Temp = ((temperature - 32) * 5.0 / 9.0 + 273.15);
  45.                 return new Temperature(converted_Temp, 'K');
  46.             }
  47.         }
  48.         if (temperature < 0)
  49.         {
  50.             System.out.println("K cannot be < 0");
  51.             System.exit(0);
  52.         }
  53.  
  54.         return new Temperature();
  55.     }
  56.  
  57.     Temperature toCelsius()
  58.     {
  59.         double converted_Temp;
  60.  
  61.         switch (scale) {
  62.             case 'K':
  63.                 converted_Temp = temperature - 273.15;
  64.                 return new Temperature(converted_Temp, 'C');
  65.  
  66.  
  67.             case 'F':
  68.                 converted_Temp =  ((temperature - 32) * 5.0 / 9.0);
  69.                 return new Temperature(converted_Temp, 'C');
  70.             }
  71.  
  72.  
  73.         if(temperature < -273.15)
  74.         {
  75.             System.out.println("C cannot be < -273.15");
  76.             System.exit(0);
  77.         }
  78.  
  79.         return new Temperature();
  80.     }
  81.  
  82.      Temperature toFahrenheit()
  83.     {
  84.         double converted_Temp;
  85.         switch (scale) {
  86.             case 'K':
  87.                 converted_Temp =  (temperature - 273.15) * 9.0 / 5.0 + 32;
  88.                 return new Temperature(converted_Temp, 'F');
  89.  
  90.         case 'C':
  91.                 converted_Temp = (temperature * 9.0 / 5.0 + 32);
  92.                 return new Temperature(converted_Temp, 'F');
  93.             }
  94.  
  95.             if (temperature < -459.67) {
  96.                 System.out.println("F cannot be < -459.67");
  97.                 System.exit(0);
  98.             }
  99.         return new Temperature();
  100.     }
  101.  
  102.  
  103.     Temperature add(Temperature n)
  104.     {
  105.         Temperature result = new Temperature();
  106.         result.set(temperature, scale);
  107.         Temperature temp1 = this.toKelvin();
  108.         Temperature temp2 = n.toKelvin();
  109.         Temperature temp3 = n.toKelvin();
  110.         Temperature temp4 = n.toKelvin();
  111.         result = new Temperature(temp1.temperature + temp2.temperature + temp3.temperature + temp4.temperature);
  112.         return result;
  113.     }
  114.  
  115.     Temperature subtract(Temperature z) {
  116.         Temperature temp2 = this.toKelvin();
  117.         Temperature temp1 = z.toKelvin();
  118.         new Temperature( temp2.temperature - temp1.temperature);
  119.         return new Temperature(temp1.temperature - temp2.temperature, 'K');
  120.     }
  121.     Temperature multiply(Temperature n)
  122.     {
  123.         Temperature temp1 = this.toKelvin();
  124.         Temperature temp2 = n.toKelvin();
  125.  
  126.         return new Temperature(temp1.temperature*temp2.temperature, 'K');
  127.     }
  128.  
  129.     Temperature divide()
  130.     {
  131.         Temperature tempAve = toKelvin();
  132.         double avgtemp =(tempAve.temperature/ 5);
  133.  
  134.         return new Temperature(avgtemp, 'K');
  135.     }
  136.  
  137.     boolean equals(Temperature n)
  138.     {
  139.         return this.temperature == n.temperature && this.scale == n.scale;
  140.     }
  141.     public String toString()
  142.     {
  143.         return "Temperature " + temperature + " is measured in " + scale;
  144.     }
  145.  
  146. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top