Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Temperature class that represents temperatures in degrees in both Celsiu

ID: 3528486 • Letter: W

Question

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 'C' for Celsius or 1F' for Fahrenheit. The class should have Four constructors: one for the number of degrees, one for the scale, one for both the degrees and the scale, and a default constructor. For each of these constructors, assume zero degrees if no value is specified and Celsius if no scale is given. Two accessor methods: one to return the temperature in degrees Celsius, the other to return it in degrees Fahrenheit. Use the formulas from Programming Project 5 of Chapter 3 and round to the nearest tenth of a degree. Three set methods: one to set the number of degrees, one to set the scale, and one to set both. Three comparison methods: one to test whether two temperatures are equal, one to test whether one temperature is greater than another, and one to test whether one temperature is less than another. Write a driver program that tests all the methods. Be sure to invoke each of the constructors, to include at least one true and one false case for each comparison method, and to test at least the following three temperature pairs for equality: 0.0 degrees C and 32.0 degrees F, -40.0 degrees C and -40.0 degrees F, and 100.0 degrees C and 212.0 degrees F.

Explanation / Answer

HOPE THIS HELPS

import java.util.*;

public class Temperature
{
    private String scale;
    private float temp;
    
   /***************************************************************/
   
    public Temperature()
    {
    scale = "Celcius";
    temp = 0;
    }
   
   
    public Temperature(float newTemp)
{

  if (scale.equalsIgnoreCase("C"))
  {
   float temp = (5 * (newTemp - 32) / 9);
   newTemp = temp;
  }
  else if (scale.equalsIgnoreCase("F"))
  {
   float temp = (9 * (newTemp / 5 ) + 32 );
   newTemp = temp;
  }
  else
  System.out.println ("You entered an incorrect temperature scale. Converion can not be done. Try again.");
}

public Temperature(String newScale)
{
  if (scale.equalsIgnoreCase("C"))
   newScale = "Celcius";
  else if (scale.equalsIgnoreCase("F"))
   newScale = "Fahrenheit";
  else
   System.out.println ("You entered an incorrect temperature scale. Try again.");
}

public Temperature(String newScale, float newTemp)
{
  if (scale.equalsIgnoreCase("C"))
  {
   newScale = "Celcius";
   float temp = (5 * (newTemp - 32) / 9);
   newTemp = temp;
  }
  else if (scale.equalsIgnoreCase("F"))
  {
   newScale = "Fahrenheit";
   float temp = (9 * (newTemp / 5 ) + 32 );
   newTemp = temp;
  }
  else
   System.out.println ("Oops, something went wrong. Try again.");
}


   
   /***************************************************************/
   
    public String toString ()
    {
    return ("The temperature scale you entered is: " + scale + " "
       + "The number of degrees you entered is: " + temp + " ");
    }
   
/***************************************************************/
       
    public void readInput()
    {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What is the degree system which you are using? Enter the first letter.");
    scale = keyboard.nextLine();
    String newScale = scale;
   
    System.out.println("What is the number of degrees?");
    temp = keyboard.nextFloat();
    float newTemp = temp;
    }
   
    /***************************************************************/
   
    /***************************************************************/
   
    public void writeOutput()
    {
    System.out.println("Temperature scale that you are using: " + scale + " ");
    System.out.println("Number of degrees: " + temp + " ");

    }

/***************************************************************/

/***************************************************************/
   
public void setTemp(float newTemp)
{
  temp = newTemp;
}

public void setScale(String newScale)
{
  scale = newScale;
}

public void setTempAndScale(String newScale, float newTemp)
{
  temp = newTemp;
  scale = newScale;
}

/***************************************************************/

/***************************************************************/

    public String getScale( )
    {
        return scale;
    }
   
    public float getTemp( )
    {
        return temp;
    }
    public String getTempAndScale( )
    {
        return (temp + scale);
    }


    
/***************************************************************/
   
/***************************************************************/

    public static void main(String[] args)
    {
    Temperature trialOne = new Temperature();
       Scanner keyboard = new Scanner(System.in);
       trialOne.readInput();
       trialOne.writeOutput();
    
       System.out.println("Set Scale is: "+ scale);
       System.out.println("Set Temp is: "+ temp);
        
     }


/***************************************************************/
   
/***************************************************************/
   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote