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

public Temp() : The default constructor for the class, which initializes the obj

ID: 3827828 • Letter: P

Question

public Temp(): The default constructor for the class, which initializes the object to 32 degree Fahrenheit. Both variables must be set using explicit assignment statements. You cannot rely on default values given to variables by Java.

public Temp(double initT, char initS): Another definition of the constructor. It takes as a parameter a temperature measurement and a symbol ('c' or 'C' for Celsius or 'f' or 'F' for Fahrenheit) representing the scale and initializes the object using those values. If the scale provided as a parameter is not valid, meaning the scale is not either Celsius or Fahrenheit, the constructor should use a default of Fahrenheit for the measurement. In either case, do not prompt the user for another value. Simply use the default specified. Both variables must be set using explicit assignment statements. You cannot rely on default values given to variables by Java.

public void set(): This method prompts the user for a decimal value representing a temperature and a character representing a scale (either Celsius or Fahrenheit). If an invalid scale (something other than 'c', 'C', 'f', or 'F') is entered, the method repeatedly asks for the scale again. You may assume that when prompted the user will provide a number (either floating point or integer) for the temperature.

public double getC(): This method returns the temperature in Celsius. If the temperature is stored in Celsius already, it just returns it. Otherwise it computes the equivalent temperature in Celsius (without changing the value in the object) and returns it. Note that to convert a temperature Tf in Fahreheit to one in Celsius you write: (Tf - 32) / 1.8.

public double getF(): This method returns the temperature in Fahrenheit. If the temperature is stored in Fahrenheit already, it just returns it. Otherwise it computes the equivalent temperature in Fahrenheit (without changing the value in the object) and returns it. Note that to convert a temperature Tc in Celsius to one in Fahrenheit you write: Tc * 1.8 + 32.

package temper;

import java.util.Scanner;

public class Temp
{
   // Do not declare any additional members of the class
  
   // For use in set()
   private Scanner vScan = new Scanner(System.in);

    // The default constructor for the class
    public Temp()
    {
  
    }

    // The parameterized constructor for the class
   public Temp(double initT, char initS)
   {

   }
  
    // Input values for the instance variables using the Scanner vScan
    public void set()
    {
  
    }

    // Return the temperature in Celsius
   public double getC()
   {
       // A stub -- replace this by the correct code
       return 0.0;
   }

   // Return the temperature in Fahrenheit
   public double getF()
   {
       // A stub -- replace this by the correct code
       return 0.0;
   }

}

Explanation / Answer

HI, Please find my implementation.

Please let me know in case of any issue.

import java.util.Scanner;

public class Temp

{

   private double temp;

   private char tempC;

   // Do not declare any additional members of the class

   // For use in set()

   private Scanner vScan = new Scanner(System.in);

   // The default constructor for the class

   public Temp()

   {

       temp = 32;

       tempC = 'f';

   }

   // The parameterized constructor for the class

   public Temp(double initT, char initS)

   {

       char c = Character.toLowerCase(initS);

       if(c != 'c' && c != 'f'){

           temp = 32;

           tempC = 'f';

       }

       else{

           temp = initT;

           tempC = c;

       }

   }

   // Input values for the instance variables using the Scanner vScan

   public void set()

   {

       System.out.print("Enter temp value: ");

       temp = vScan.nextDouble();

       System.out.print("Enter type of temperature(c/f): ");

       char c = vScan.next().charAt(0);

       tempC = Character.toLowerCase(c);

   }

   // Return the temperature in Celsius

   public double getC()

   {

       // A stub -- replace this by the correct code

       if(tempC == 'c')

           return temp;

       else{

           return (temp - 32) / 1.8;

       }

   }

   // Return the temperature in Fahrenheit

   public double getF()

   {

       // A stub -- replace this by the correct code

       if(tempC == 'f')

           return temp;

       else{

           return (temp * 1.8 + 32);

       }

   }

}