44.1k views
0 votes
Suppose you have a class called Thermometer. The class has a no- arguments constructor that sets the thermometer's temperature to zero Celsius. There are two modification methods (setCelsius and set- Fahrenheit) to change the temperature by a specified amount. (One method has a parameter in Celsius degrees, and the other has a parameter in Fahrenheit.) There are two accessor methods to get the current tem- perature (getCelsius and getFahrenheit). A thermometer keeps track of only one temperature, but it can be accessed in either Celsius or Fahr- enheit. Write code to create a thermometer, add 10 degrees Celsius to it, and then print the Fahrenheit temperature.

User Junkone
by
7.2k points

1 Answer

3 votes

Final answer:

The student's question involves object-oriented programming and creating a class named Thermometer, which handles temperatures in Celsius and Fahrenheit. The provided code in Java initializes a Thermometer object, increases its temperature by 10 degrees Celsius, and prints out the temperature in Fahrenheit.

Step-by-step explanation:

To address the task given, we first need to understand that we are working with the concepts of object-oriented programming, specifically in a programming language that supports classes like Java, Python, or C#. The Thermometer class will encapsulate the functionality of a thermometer that can work with both Celsius and Fahrenheit units.

Here's an example of how you might write this code in Java:

public class Thermometer { private double temperatureCelsius; public Thermometer() { this.temperatureCelsius = 0; } public void setCelsius(double temperature) { this.temperatureCelsius = temperature; } public void setFahrenheit(double temperature) { this.temperatureCelsius = (temperature - 32) * 5 / 9; } public double getCelsius() { return temperatureCelsius; } public double getFahrenheit() { return (temperatureCelsius * 9 / 5) + 32; } } public class Main { public static void main(String[] args) { Thermometer thermo = new Thermometer(); thermo.setCelsius(thermo.getCelsius() + 10); System.out.println("The current Fahrenheit temperature is: " + thermo.getFahrenheit()); } }

Here, we first create a new Thermometer object with the default temperature of 0°C. We then call setCelsius to increase the temperature by 10 degrees Celsius. Finally, we retrieve and print the current temperature in Fahrenheit with getFahrenheit.

Note that this code assumes that the temperature is initially set to 0°C by the no-arguments constructor.

User Avihoo Mamka
by
8.2k points