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: 3827832 • 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 hw5b;

// Name

import java.util.Scanner;

public class Temp
{
   // No additional members of the class
   // Feel free to use local variables as necessary in each method
   private double tValue;
   // If you like, you can change the type of scale to Character
   private char scale;
  
   // 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

package hw5b;

import java.util.Scanner;
public class Temp
{
// No additional members of the class
// Feel free to use local variables as necessary in each method
private double tValue;
// If you like, you can change the type of scale to Character
private char scale;
  
// For use in set()
private Scanner vScan = new Scanner(System.in);
// The default constructor for the class
public Temp()
{
tValue = 32;
scale = 'F';
}
// The parameterized constructor for the class
public Temp(double initT, char initS)
{
this.tValue = initT;
this.scale = initS;
if (initS != 'c' || initS != 'C')
this.scale = 'F';
}

// Input values for the instance variables using the Scanner vScan
public void set()
{
System.out.println("Enter value for temperature: ");
tValue = vScan.nextDouble();

while(true)
{
System.out.println("Enter a value for scale (either of c, C, f, F: ");
char c = vScan.next().charAt(0);
if (c != 'f' || c != 'F' || c!= 'c' || c!= 'C')
{
System.out.println("Please try again by entering correct value for scale.");
}
else
{
this.scale = c;
break;
}
}
}
// Return the temperature in Celsius
public double getC()
{
if (scale == 'c' || scale == 'C')
{
return this.tValue;
}
return (this.tValue - 32)/1.8;
}
// Return the temperature in Fahrenheit
public double getF()
{
if (scale == 'f' || scale == 'F')
{
return this.tValue;
}
return (this.tValue*1.8) + 32;
  
}
}