Question

In: Computer Science

For My Programming Lab - Java: Write a Temperature class that will hold a temperature in...

For My Programming Lab - Java:

Write a Temperature class that will hold a temperature in Fahrenheit and provide methods 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 set Fahrenheit method accepts a Fahrenheit temperature (as a double ) and stores it in the ftemp field.
• getFahrenheit: Returns the value of the ftemp field as a Fahrenheit temperature (no conversion required)
• getCelsius: Returns the value of the ftemp field converted to Celsius. Use the following formula to convert to Celsius:
Celsius = (5/9) * (Fahrenheit - 32)
• getKelvin: Returns the value of the ftemp field converted to Kelvin. Use the following formula to convert to Kelvin:
Kelvin = ((5/9) * (Fahrenheit - 32)) + 273

Demonstrate the Temperature class by writing a separate program that asks the user for a
Fahrenheit temperature. The program should create an instance of the Temperature class ,
with the value entered by the user passed to the constructor . The program should then
call the object 's methods to display the temperature in the following format (for example,
if the temperature in Fahrenheit was -40):

The temperature in Fahrenheit is -40.0
The temperature in Celsius is -40.0
The temperature in Kelvin is 233.0

Solutions

Expert Solution

import java.util.*;

class Temperature
{
    private double ftemp;
  
    public Temperature(double ftemp) //constructor
    {
        this.ftemp = ftemp;
    }
    public void setFahrenheit(double ftemp) //set Fahrenheit Temperature
    {
        this.ftemp = ftemp;
    }
    public double getFahrenheit() //get temperature in Fahrenheit
    {
     return ftemp;
    }
    public double getCelsius() //get temperature in Celsius
    {
     double ctemp = (5.0/9.0) * (ftemp - 32.0);
     return ctemp;
    }
    public double getKelvin() //get temperature in Kelvin
    {
        double ktemp = ((5.0/9.0) * (ftemp - 32.0)) + 273.0;
        return ktemp;
    }
  
  
}
class Test
{
   public static void main (String[] args)
   {
      Scanner scan = new Scanner(System.in);
    
      System.out.println("Enter temperature in Fahrenheit : ");
      double ftemp = scan.nextDouble(); //input temperature in Fahrenheit
    
      Temperature t = new Temperature(ftemp);
    
      //display temperature in different units
        System.out.println("The temperature in Fahrenheit is "+t.getFahrenheit());
        System.out.println("The temperature in Celsius is "+t.getCelsius());
        System.out.println("The temperature in Kelvin is "+t.getKelvin());
   }
}

Output:

Enter temperature in Fahrenheit : -40

The temperature in Fahrenheit is -40.0
The temperature in Celsius is -40.0
The temperature in Kelvin is 233.0


Related Solutions

Write a Temperature class that will hold a temperature in Fahrenheit, and provide methods to get...
Write a Temperature class that will hold a temperature in Fahrenheit, and provide methods to get the temperature in Fahrenheit, Celsius and Kelvin. The class should have the following field: ftemp – A double that hold 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 it in...
This is for my Advanced Java Programming class. The book we use is Murach's Java Servlet's...
This is for my Advanced Java Programming class. The book we use is Murach's Java Servlet's and JSP 3rd Edition. I need help modifying some code. I will post the code I was told to open that needs to be modified below. In this exercise, you'll enhance the Future Value application to store the amount and interest rate in the user's session. That way, the user can experiment with different numbers of years to see the value of his or...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: aString representing the telephone number. Include a constructor, the accessor and mutator, and methods 'toString' and 'equals'. Also include methods returning the AREA CODE (the first three digits/characters of the phone number; if there are fewer than three characters in the phone number of if the first three characters are not digits, then this method should...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Required components: A controller class with Java main() method and a PaymentCalculator class to hold the...
Required components: A controller class with Java main() method and a PaymentCalculator class to hold the data and methods for the application. Scenario/Information: If you carry a balance on a credit card, it can be nice to see how long it would take to payoff the card. For this scenario, you should design a Java application the prints a credit card payment schedule. Inputs for your program should include: Customer’s name (first and last), the account number (as an integer),...
Postfix Evaluation (JAVA PROGRAMMING) Write class PostfixEva1uator that evaluates a postfix expression such as 6 2...
Postfix Evaluation (JAVA PROGRAMMING) Write class PostfixEva1uator that evaluates a postfix expression such as 6 2 + 5 * 8 4 / - The program should read a postfix expression consisting of single digits and operators into a StringBuilder, The program should read the expression and evaluate it (assume it's valid). The algorithm to evaluate a postfix expression is shown below. Use +, -, *, /, and ^. ^ is the exponent. Append a right parenthesis ') ' to the...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices)...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices) Write a method to add two matrices. The header of the method is as follows: public static double[][] addMatrix(double[][] a, double[][] b In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each element cij is aij + bij. For example, for two 3 * 3 matrices...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner object to read input. int Age;     Write a java statement to read the Age input value 4 . Redo 1 to 3 using JOptionPane
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT