Complete the provided Temperature class You will add any att

Complete the provided Temperature class. You will add any attributes and helper methods its needed. You must complete the constructors and methods in the provided class (without changing any signatures or return types). In this problem you need to be able to convert temperatures between Celsius, Fahrenheit and Kelvin. For help, see A temperature object keeps track of which scale it is currently using outputs its temperature value in that scale whenever asked. The scale can be changed at any time. Mark breakdown: 10 for correctness Put your Temperature. java file in your assignment4.zip file. Examples: Temperature t = new Temperature(10.1); System.out.println(t); 10.1C t.setScale(\"F\"); System.out.println(t); System.out.println(t.getTemp(); 283.25

Solution

Hi, Please find my program.

Please let me know in case of any issue:

public class Temperature {

  

private String scale; // store scale

private double temperture; // store temperature

  

// constructor

   public Temperature(double temperature){

       this.temperture = temperature;

       scale = \"C\"; // by default scale

   }

  

   public void setScale(String s){

       if(s.equalsIgnoreCase(\"Kelvin\")){

           if(scale.equalsIgnoreCase(\"C\"))

               convertCtoK();

           else if(scale.equalsIgnoreCase(\"F\"))

               convertFtoK();

       }else if(s.equalsIgnoreCase(\"C\")){

           if(scale.equalsIgnoreCase(\"F\"))

               convertFtoC();

           else if(scale.equalsIgnoreCase(\"Kelvin\"))

               convertKtoC();

       }else if(s.equalsIgnoreCase(\"F\")){

           if(scale.equalsIgnoreCase(\"C\"))

               convertCtoF();

           else if(scale.equalsIgnoreCase(\"Kelvin\"))

               convertKtoF();

       }

   }

  

// helpers methods

  

   private void convertCtoK(){

       temperture = temperture + 273.15;

       scale = \"Kelvin\";

   }

  

   private void convertFtoK(){

       temperture = (5.0/9.0)*(temperture-32) +273;

       scale = \"Kelvin\";

   }

  

   private void convertFtoC(){

       temperture = (temperture - 32)*5.0/9.0;

       scale = \"C\";

   }

  

   private void convertKtoC(){

       temperture = temperture - 273.15;

       scale = \"C\";

   }

  

   private void convertCtoF(){

       temperture = (9.0/5.0)*temperture + 32;

       scale = \"F\";

   }

  

   private void convertKtoF(){

       temperture = temperture * 9.0/5.0 - 459.67;

       scale = \"F\";

   }

  

   public double getTemp(){

       return temperture;

   }

  

   public String getScale(){

       return scale;

   }

   @Override

   public String toString() {

       String s = \"\";

       if(scale.equalsIgnoreCase(\"F\"))

           s = \"F\";

       else if(scale.equalsIgnoreCase(\"C\"))

           s = \"C\";

      

       return temperture+s;

   }

  

  

   public static void main(String[] args) {

      

       Temperature t = new Temperature(10.1);

       System.out.println(t);

       t.setScale(\"F\");

       System.out.println(t);

       t.setScale(\"Kelvin\");

       System.out.println(t.getTemp());

      

   }

  

}

/*

Sample run:

10.1C

50.18F

283.1

*/

 Complete the provided Temperature class. You will add any attributes and helper methods its needed. You must complete the constructors and methods in the provi
 Complete the provided Temperature class. You will add any attributes and helper methods its needed. You must complete the constructors and methods in the provi
 Complete the provided Temperature class. You will add any attributes and helper methods its needed. You must complete the constructors and methods in the provi
 Complete the provided Temperature class. You will add any attributes and helper methods its needed. You must complete the constructors and methods in the provi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site