Question
1: Temperature Complete the provided Temperature class. Add any attributes and helper methods as needed. You must complete the constructors and methods in the provided
1: Temperature Complete the provided Temperature class. Add any attributes and helper methods as needed. You must complete the constructors and methods in the provided class (without changing any signatures, return types, or modifiers). In this problem you will need to be able to convert temperatures between Celsius, Fahrenheit and Kelvin. For help, see https://en.wikipedia.org/wiki/Conversion_of_units_of_temperature A temperature object holds a single temperature and displays it in one of the three scales. Once a scale has been set, it will display the temperature in that scale until changed. The default scale is Celsius if not specified. Examples: Temperature t = new Temperature(10.1); System.out.println(t.getScale()); // outputs the char 'C' System.out.println(t); // outputs 10.1C t.setScale("F"); System.out.println(t); // outputs 50.18F System.out.println(t.getScale()); // outputs the char 'F' When you set a temperature (without explicitly stating the scale), whatever scale is currently used by the object will be used. Note: Repeatedly changing the scale should not change the value of the temperature. For example, Temperature t = new Temperature(10.1); System.out.println(t); for(int i=0; i<10000; i+=1){ t.setScale("F"); t.setScale("C"); } System.out.println(t); Should print out identical strings. Note: You should have no static attributes or methods in your class (unless they were supplied in the starter code). Note: Your code must use encapsulation. Your grade will be reduced by 5 marks if you do not use encapsulation.
/** * A Temperature object represents temperature (with a value and scale) * * COMP 1006/1406 * Summer 2018 * Assignment 2 */ public class Temperature{ /** different scale names */ public static String[] scales = {"Celsius", "Fahrenheit", "Kelvin"}; /** Initializes a temperature object with given value in Celcius * * If the initial temperature is less than -273.15 then the temperature * object will be initialized with -273.15C. * * @param temp is the initial temperature in Celsius. */ public Temperature(double temp){ } /** Initializes a temperature object with given value using the specified scale ** If the temperature is lower than absolute zero, then set the temperature to * absolute zero (in whichever scale is specified). * * Examples: new Temperature(12.3, "K") * new Temperature(-90.2, "Celsius") * * @param temp is the initial temperature * @param scale is the scale of initial temperature and must either be * one of the Strings in the scales
array, or * the first letter (capitalized) of one of those strings. */ public Temperature(double temp, String scale){ } /** getter for the scale * * The output of this getter method must always be the first letter of one * of the strings in thescales
array, capitalized. * * @return the current scale of the object as a single char (the first letter, * capitalized of one of the strings fromscales
) */ public char getScale(){ return 'X'; } /** getter for the temperature * * @return the temperature of the object using the current scale */ public double getTemp(){ return -Double.MAX_VALUE; } /** setter for scale * * * * @param scale is the new scale of the temperature and must either be * one of the Strings in thescales
array, or * the first letter (capitalized) of one of those strings. */ public void setScale(String scale){ } /** setter for temperature * * @param temp is the new temperature (in the objects current scale) */ public void setTemp(double temp){ } /** setter for temperature * * * @param temp is the new temperature * @param scale is the scale of the new temperature. It must be * the first letter (capitalized) of one of the strings in * thescales
array. */ public void setTemp(double temp, char scale){ } /** setter for temperature * * @param temp is the new temperature * @param scale is the scale of the new temperature. It must be one * of the strings in thescales
array, or the first letter * (capitalized) of one of those strings. */ public void setTemp(double temp, String scale){ } /* ------------------------------------------------- */ /* ------------------------------------------------- */ /* do not change anything below this */ /* ------------------------------------------------- */ /* ------------------------------------------------- */ /** String representation of a temperature object */ @Override public String toString(){ return "" + this.getTemp() + this.getScale(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started