Define a new class named Temperature. The class has two acessors-toFahrenheit an
ID: 3529038 • Letter: D
Question
Define a new class named Temperature. The class has two acessors-toFahrenheit and toCelsius--- that return the termperature in the specified unit and two mutators -- setFahrenheit and setCelsius-- that assign the temperature in the specified unit. Maintain the temperature internally in degrees Fahrenheit. Using this class, write a program that inputs temperature in degrees Fahrenheit and outputs the temperature in equivalent degrees Celsius.Explanation / Answer
import java.util.Scanner; public class tempConverter { private double celsius; private double fahrenheit; public tempConverter() { celsius = 0.0; fahrenheit = 0.0; } public double getCelsius() { return celsius; } public double getFahrenheit() { return fahrenheit; } public void setCelsius(double temperature) { celsius = temperature; } public void setFahrenheit(double temperature) { fahrenheit = temperature; } public double toCelsius(double fahrenheit) { return (fahrenheit - 32.0) * (5.0 / 9.0); } public double toFahrenheit(double celsius) { return (celsius * (9.0 / 5.0)) + 32.0; } public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int menuSelection = 0; double temperature = 0.0; System.out.printf("Temperature Converter "); System.out.printf(" 1. Convert from Celsius to Fahrenheit."); System.out.printf("2. Convert from Fahrenheit to Celsius. "); System.out.printf(" Enter a selection (1-2): "); menuSelection = keyboard.nextInt(); System.out.printf(" Enter a temperature to convert from: "); temperature = keyboard.nextDouble(); switch(menuSelection) { case 1: // case 1 -> converting from Celsius to Fahrenheit. //Set Celsius to temperature. //Set Fahrenheit using mutator method (setFahrenheit(temperature)). break; case 2: // case 2 -> converting from Fahrenheit to Celsius. //Set Fahrenheit to temperature. //Set Celsius using mutator method (setCelsius(temperature)). break; default: System.out.println("Invalid selection."); break; } //Print Celsius/Fahrenheit conversion temps. } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.