Question

Java please The kelvin is the base unit of temperature in the International System of Units...

Java please

The kelvin is the base unit of temperature in the International System of Units (SI), having the unit symbol K. It is named after the Belfast-born, Glasgow University engineer and physicist William Thomson, 1st Baron Kelvin (1824–1907). It uses absolute zero as its null point. The Celsius scale, also known as the centigrade scale, is a temperature scale used by the International System of Units (SI). The Celsius scale is based on 0 °C for the freezing point of water and 100 °C for the boiling point of water at 1 atm pressure. Kelvin temperatures and Celsius temperatures are related. This means that a temperature difference of one degree Celsius and that of one kelvin are exactly the same. On the Fahrenheit scale, the freezing point of water is 32 degrees Fahrenheit (°F) and the boiling point is 212 °F (at standard atmospheric pressure). There are three conversion methods. toCesius() converts the this temperature to a Celsius value and returns it. toKelvin () converts the this temperature to a Kelvin value and returns it. toFahrenheit() converts the this temperature to a Fahrenheit value and returns it. These conversions do not change the value of the this. There will be a method for each of the arithmetic operations. For divide it will be divide(double), which returns a temperature in the scale of the this. For add it will be add(Temperature), which returns a temperature in the scale of the this. For Subtract it will be subtract(Temperature), which also returns a temperature in the scale of the this. The value of this is not changed in these three methods. There are two boolean methods. Method equals(Temperature) tests the value of the parameter and this and returns true it they are equal., Method greaterThan(Temperature) compares the this with the parameter and returns true if this is greater than the parameter. There should be a read() method and a toString() method in your class. Remember methods add, subtract, and 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(), 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, 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Temperature Class written in Java

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

package com.temperature.test;

public class Temperature {

   private double degreeValue;
   private String unitSymbol;
   public enum UnitSymbolType {K,C,F};
   private final double absoluteZeroKelvin = 0;
   private final double absoluteZeroFahrenhiet = -459.67;
   private final double absoluteZeroCelcius = -273.15;
   private boolean isValidTemperature = false;
  
   public Temperature() {
       this.degreeValue = absoluteZeroKelvin;
       this.unitSymbol=UnitSymbolType.K.toString();
       set(this.unitSymbol,this.getDegreeValue());
   }
   public Temperature( double degreeValue) {
       this.degreeValue = degreeValue;
       this.unitSymbol=UnitSymbolType.K.toString();
       set(this.unitSymbol,this.getDegreeValue());
   }
  
   public Temperature(String unitSymbol, double degreeValue) {
       this.degreeValue = degreeValue;
       this.unitSymbol = unitSymbol;
       set(this.unitSymbol,this.getDegreeValue());
   }
  
   public Temperature(UnitSymbolType unitSymbol, double degreeValue) {
       this.degreeValue = degreeValue;
       this.unitSymbol = unitSymbol.toString();
   }
  
   public double getDegreeValue() {
       return degreeValue;
   }

   public void setDegreeValue(double degreeValue) {
       this.degreeValue = degreeValue;
   }

   public String getUnitSymbol() {
       return unitSymbol;
   }

   public void setUnitSymbol(String unitSymbol) {
       this.unitSymbol = unitSymbol;
   }

   public Temperature divide(double factor) {
       Temperature temperature = new Temperature(this.unitSymbol,this.getDegreeValue()/factor);
      
       return temperature;
   }
  
   public Temperature add(Temperature temperaturetobeadded) {
      
       String originalTempUnit =this.unitSymbol;
       String additionTempUnit =temperaturetobeadded.unitSymbol;
       double degreeChangedValue=0;
       if(originalTempUnit.equals(additionTempUnit)) {
           degreeChangedValue =this.degreeValue + temperaturetobeadded.getDegreeValue();
           Temperature temperature = new Temperature(this.unitSymbol,degreeChangedValue);
           return temperature;
       }else {
           switch (originalTempUnit) {
           case "K":
               degreeChangedValue = this.degreeValue + temperaturetobeadded.toKelvin().getDegreeValue();
               Temperature temperatureK = new Temperature(this.unitSymbol,degreeChangedValue);
               return temperatureK;
              
           case "C":
               degreeChangedValue = this.degreeValue + temperaturetobeadded.toCelcius().getDegreeValue();
               Temperature temperatureC = new Temperature(this.unitSymbol,degreeChangedValue);
               return temperatureC;
              
           case "F":
               degreeChangedValue = this.degreeValue + temperaturetobeadded.toFahrenheit().getDegreeValue();
               Temperature temperatureF = new Temperature(this.unitSymbol,degreeChangedValue);
               return temperatureF;

           default:
               return null;
              
           }
          
       }
      
   }

   public Temperature subtract(Temperature temperaturetosubtracted) {
       String originalTempUnit =this.unitSymbol;
       String additionTempUnit =temperaturetosubtracted.unitSymbol;
       double degreeChangedValue=0;
       if(originalTempUnit.equals(additionTempUnit)) {
           degreeChangedValue =this.degreeValue - temperaturetosubtracted.getDegreeValue();
           Temperature temperature = new Temperature(this.unitSymbol,degreeChangedValue);
           return temperature;
       }else {
           switch (originalTempUnit) {
           case "K":
               degreeChangedValue = this.degreeValue - temperaturetosubtracted.toKelvin().getDegreeValue();
               Temperature temperatureK = new Temperature(this.unitSymbol,degreeChangedValue);
               return temperatureK;
              
           case "C":
               degreeChangedValue = this.degreeValue - temperaturetosubtracted.toCelcius().getDegreeValue();
               Temperature temperatureC = new Temperature(this.unitSymbol,degreeChangedValue);
               return temperatureC;
              
           case "F":
               degreeChangedValue = this.degreeValue - temperaturetosubtracted.toFahrenheit().getDegreeValue();
               Temperature temperatureF = new Temperature(this.unitSymbol,degreeChangedValue);
               return temperatureF;

           default:
               return null;
              
           }
       }
   }
  
   public Temperature toKelvin() {
      
       Temperature returnTemperature = new Temperature();
       double kelvinvalue=0;
      
       String key= this.unitSymbol;
       switch (key) {
           case "C":
               kelvinvalue = this.getDegreeValue() + 273;
               returnTemperature.setDegreeValue(kelvinvalue);
               returnTemperature.setUnitSymbol(UnitSymbolType.K.toString());
               break;
           case "F":
               kelvinvalue = (9/5)*(this.getDegreeValue()-32) +273;
               returnTemperature.setDegreeValue(kelvinvalue);
               returnTemperature.setUnitSymbol(UnitSymbolType.K.toString());
               break;
           case "K":
               returnTemperature.setDegreeValue(this.getDegreeValue());
               returnTemperature.setUnitSymbol(UnitSymbolType.K.toString());
               break;
           default:
               break;
          
       }
      
       return returnTemperature;
   }
  
   public Temperature toCelcius() {
      
       Temperature returnTemperature = new Temperature();
       double celciusvalue=0;
      
       String key= this.unitSymbol;
       switch (key) {
           case "K":
               celciusvalue = this.getDegreeValue() -273;
               returnTemperature.setDegreeValue(celciusvalue);
               returnTemperature.setUnitSymbol(UnitSymbolType.C.toString());
               break;
           case "F":
               celciusvalue = (9/5)*(this.getDegreeValue()-32) ;
               returnTemperature.setDegreeValue(celciusvalue);
               returnTemperature.setUnitSymbol(UnitSymbolType.C.toString());
               break;
           case "C":
               returnTemperature.setDegreeValue(this.getDegreeValue());
               returnTemperature.setUnitSymbol(UnitSymbolType.C.toString());
               break;
           default:
               break;
          
       }
      
       return returnTemperature;
   }
  
   public Temperature toFahrenheit() {
      
       Temperature returnTemperature = new Temperature();
       double fahrenheitvalue=0;
      
       String key= this.unitSymbol;
       switch (key) {
           case "K":
               fahrenheitvalue = (9/5)*(this.getDegreeValue() -273) + 32;
               returnTemperature.setDegreeValue(fahrenheitvalue);
               returnTemperature.setUnitSymbol(UnitSymbolType.F.toString());
               break;
           case "C":
               fahrenheitvalue = (9/5)*(this.getDegreeValue()) + 32;
               returnTemperature.setDegreeValue(fahrenheitvalue);
               returnTemperature.setUnitSymbol(UnitSymbolType.F.toString());
               break;
           case "F":
               returnTemperature.setDegreeValue(this.getDegreeValue());
               returnTemperature.setUnitSymbol(UnitSymbolType.F.toString());
               break;
           default:
               break;
          
       }
      
       return returnTemperature;
   }

  
   public boolean equals(Temperature obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Temperature other = (Temperature) obj;
       if (Double.doubleToLongBits(degreeValue) != Double.doubleToLongBits(other.degreeValue))
           return false;
       if (unitSymbol == null) {
           if (other.unitSymbol != null)
               return false;
       } else if (!unitSymbol.equals(other.unitSymbol))
           return false;
       return true;
   }
  
   public boolean greaterThan(Temperature temperature) {
       if (this == temperature)
           return true;
       if (temperature == null)
           return false;
       if (getClass() != temperature.getClass())
           return false;
       String originalTempUnit =this.unitSymbol;
       String additionTempUnit =temperature.unitSymbol;
       if(originalTempUnit.equals(additionTempUnit)) {
           if(this.getDegreeValue()>temperature.getDegreeValue())
               return true;
       }else {
           double comparablevalue=0;
           switch (originalTempUnit) {
           case "K":
               comparablevalue = temperature.toKelvin().getDegreeValue();
               if(this.getDegreeValue()>comparablevalue)
                   return true;
               break;
           case "C":
               comparablevalue = temperature.toCelcius().getDegreeValue();
               if(this.getDegreeValue()>comparablevalue)
                   return true;
               break;
           case "F":
               comparablevalue = temperature.toFahrenheit().getDegreeValue();
               if(this.getDegreeValue()>comparablevalue)
                   return true;
               break;

           default:
               break;
           }
          
       }
      
       return false;
   }

  
   private void set(String unitSymbol, double degreeValue) {
       switch (unitSymbol) {
       case "K":
           if (degreeValue >= absoluteZeroKelvin)
               this.isValidTemperature = true;
           break;
       case "C":
           if (degreeValue >= absoluteZeroCelcius)
               this.isValidTemperature = true;      
           break;
       case "F":
           if (degreeValue >= absoluteZeroFahrenhiet)
               this.isValidTemperature = true;
           break;

       default:
           break;
       }
              
   }
  
   @Override
   public String toString() {
       return "Temperature [degreeValue=" + degreeValue + ", unitSymbol=" + unitSymbol + " isValidTemperatue="+ Boolean.toString(isValidTemperature)+"]";
   }
  
}

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

Test Code to test the Temperature Class

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

package com.temperature.test;

public class TemperatuerTest {

   public static void main(String[] args) {

       Temperature temp1 = new Temperature(Temperature.UnitSymbolType.C,45);
       Temperature temp2 = new Temperature(Temperature.UnitSymbolType.F,77);
       Temperature temp3 = new Temperature(Temperature.UnitSymbolType.K,318);
       Temperature temp4 = new Temperature(Temperature.UnitSymbolType.C,45);
       Temperature temp5 = new Temperature();
       Temperature temp6 = new Temperature(389);
       Temperature temp7 = new Temperature(698);

       System.out.println(temp1.equals(temp3));
       System.out.println(temp1.equals(temp2));
       System.out.println(temp1.equals(temp4));
       System.out.println("Temp "+ temp5.getUnitSymbol());
       System.out.println(temp1.toFahrenheit());
       System.out.println(temp1.toKelvin());
       System.out.println(temp2.toCelcius());
       System.out.println(temp3.toCelcius());
       System.out.println(temp1.add(temp2));
       System.out.println(temp1.greaterThan(temp2));
       System.out.println(temp1.greaterThan(temp6));
       System.out.println(temp6.greaterThan(temp1));
       System.out.println(temp3.add(temp7));
       System.out.println(temp3.subtract(temp7));
   }

}

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

Result of Test

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

false
false
true
Temp K
Temperature [degreeValue=77.0, unitSymbol=F isValidTemperatue=true]
Temperature [degreeValue=318.0, unitSymbol=K isValidTemperatue=true]
Temperature [degreeValue=45.0, unitSymbol=C isValidTemperatue=true]
Temperature [degreeValue=45.0, unitSymbol=C isValidTemperatue=true]
Temperature [degreeValue=90.0, unitSymbol=C isValidTemperatue=true]
false
false
true
Temperature [degreeValue=1016.0, unitSymbol=K isValidTemperatue=true]
Temperature [degreeValue=-380.0, unitSymbol=K isValidTemperatue=false]

Add a comment
Know the answer?
Add Answer to:
Java please The kelvin is the base unit of temperature in the International System of Units...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Temperature class that will hold a temperature in Fahrenheit, and provide meth- ods to...

    Write a Temperature class that will hold a temperature in Fahrenheit, and provide meth- ods to get the temperature in Fahrenheit, Celsius, and Kelvin. The class should have the following field: • ftemp – A double that holds a Fahrenheit temperature. The class should have the following methods: Constructor – The constructor accepts a Fahrenheit temperature (as a double) and stores it in the ftemp field. setFahrenheit – The setFahrenheit method accepts a Fahrenheit temperature (as a double) and stores...

  • In java code: Write a Temperature class that has two private instance variables: • degrees: a...

    In java code: Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument...

  • Java Write a Temperature class that has two private instance variables: • degrees: a double that...

    Java Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have - four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set...

  • Java program Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit....

    Java program Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit. Use a floating- point number for the temperature and a character for the scale, eitherでfor Celsius or 'F' for fahrenheit. The class should have Four constructors: one for the number of degrees, one for the scale, one for both the degrees and scale, and a default constructor. For each of these constructors, assume zero degrees if no value is specified and celsius if no...

  • RANKINE TEMPERATURE SCA E Like the Kelvin scale, the Rankine scale is an absolute temperature scale:...

    RANKINE TEMPERATURE SCA E Like the Kelvin scale, the Rankine scale is an absolute temperature scale: Absolute zero is zero degrees Rankine (0 R). However, the units of this scale are the same size as those on the Fahrenheit scale F) rather than the Celsius scale (C) Given that water at standard presure freezes at O C which corresponds to 32-F and that it boils at 100 C which comesponds to 212 F. calculate the temperature diference AT in degr...

  • c++ please 2. (50 points) Create a Temperature class that internally stores a temperature in degrees...

    c++ please 2. (50 points) Create a Temperature class that internally stores a temperature in degrees Kelvin as a double. Create mutator functions named setTempKelvin, setTempFahrenheit, and set TempCelsius that take an input temperature in the specified temperature scale (i.e. Kelvin, Fahrenheit, and Celsius, respectively), and convert the temperature to Kelvin, and store that temperature in the class member private variable (only in Kelvin, no other scales). Also, create functions that return the stored temperature in degrees Kelvin, Fahrenheit, or...

  • Imagine that an international commission has defined a new temperature scale, where the absolute zero is...

    Imagine that an international commission has defined a new temperature scale, where the absolute zero is defined as 0◦ N and the freezing point of water as 100◦ N. What is the value of the boiling point of water on this new scale? What would be the value of the Boltzmann constant k in J/◦ N?

  • 36. A. What is the difference between intensive and extensive properties? (Circle one correct answer) a)...

    36. A. What is the difference between intensive and extensive properties? (Circle one correct answer) a) An intensive property is independent of the amount of the substance, whereas extensive property depends on b) Along with the change of the amount of substance, an intensive property changes faster than extensive property. c) Intensive properties are quantitative properties of substance, while extensive properties are qualitative d) An extensive property is independent of the amount of the substance, whereas intensive property depends on...

  • DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to...

    DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to solve the problem using short methods that work together to solve the operations of add, subtract multiply and divide.   A constructor can call a method called setSignAndRemoveItIfItIsThere(). It receives the string that was sent to the constructor and sets a boolean variable positive to true or false and then returns a string without the sign that can then be processed by the constructor to...

  • The following is not one of the basic units of the SI system: meter mole gram...

    The following is not one of the basic units of the SI system: meter mole gram second ampere Currently, the total number of known elements is: 83 90 92 102 118 126 238 294 Concrete contains an aggregate, consisting of silicon dioxide (SiO_2) in the form of sand or gravel, together with cement, which is a combination of calcium silicates and alumni silicates. Accordingly, concrete can be classified as An element A compound A homogeneous mixture A heterogeneous mixture None...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT