What is the default value for HashMap in Java?

Java Programming Temperature Application, back to Celsius?

  • So this is a object of an application Temperatures, which provides the values for the temperature instance. I need a way to convert back to Celsius after the temperatureScale is switch to Fahrenheit of Kelvin. import java.text.DecimalFormat; // Celsius = (Fahrenheit - 32) * 5/9 // Fahrenheit = Celsius * 9/5 + 32 // Kelvin = Celsius + 273.15 // Celsius = Kelvin - 273.15 public class Temperature { // private static: some way to keep track of the current scale: Celsius, Fahrenheit, or Kelvin private static String temperatureScale; // private: a property to keep track of the currently stored temperature. Choose one scale to use internally for storing // the temperature. Make conversions to the other scales only if needed in input/output operations from your object. private double temperature; public Temperature() { //This is the default constructor and it initializes the Temperature instance to 0 degrees Celsius(Internal Scale) temperature = 0.0; // temperature instance is initialized to 0 degress Celsius temperatureScale = "C"; // the internal scale is set to Celsius } public Temperature(double temp) { //This constructor receives a double. It initializes the instance to that value IN THE CURRENT SCALE. temperature = temp; // initializes the instance to the double in the current scale. } public static void useFahrenheit() { // This sets the currently used scale for ALL instances of the temperature object to Fahrenheit. temperatureScale = "F"; } public static void useCelsius() { // This sets the currently used scale for ALL instances of the temperature object to Celsius. temperatureScale = "C"; } public static void useKelvin() { // This sets the currently used scale for ALL instances of the temperature object to Kelvin. temperatureScale = "K"; } public void set(double value) { //This method receives a numeric value and stores it as the current temperature USING THE CURRENT SCALE. //If your internal scale is different, convert. if (temperatureScale == "C"){ temperature = value; //if the temperature scale is Celsius, not need to convert } else if(temperatureScale == "F"){ temperature = (value - 32) * 5/9.0; //if the temperature scale is Fahrenheit, then convert the value to Celsius. }else if (temperatureScale == "K"){ temperature = (value - 273.15); //if the temperture scale is Kelvin, then convert the value to Celsius. } } public String toString(){ //This method returns a string with the temperature IN THE CURRENT SCALE, //followed by the degree symbol and C, F, or K depending on the current scale. //A conversion will be needed if the current scale is not what you use internally. String temperatureString; if(temperatureScale == "F"){ temperature = temperature * 9/5.0 + 32; // Covert from Celsius to Fahrenheit, if the current scale is Fahrenheit. }else if (temperatureScale == "K"){ temperature = temperature + 273.15; // Covert from Celsius to Kelvin, if the current scale is Kelvin. } DecimalFormat df = new DecimalFormat("#.##"); temperatureString = df.format(temperature) + '\u00b0' + temperatureScale; // string with the temperature, degree symbol, and letter for scale. return temperatureString; } public Temperature mean(Temperature otherTemperature) { //This method receives another Temperature instance and returns a Temperature instance as the answer. //It adds the two internal values together and divides by 2. //It creates an "answer" Temperature and sets its internal value to the result, and then returns "answer". double average = (temperature + otherTemperature.temperature) /2.0; // Create Temperature mean and find the mean of the two Temperature instances. Temperature mean = new Temperature (average); return mean; } }

  • Answer:

    The hint I can give you is this: this is an understanding question. Let's say you wanted to make a program that would convert all the world's currencies. You have to store a value amount somewhere in your program. That is to say, somewhere in your program there's a float or a double that says how much money you have. If you were writing that program, would you make up your own unit, or would you borrow a unit as your "base" currency. I'll phrase that another way so it makes more sense. If your program stored the number 1, and I asked you how many Brittish pounds that would be, and then I asked you how many dollars that would be, what would you tell me? No matter what you said for Brittish pounds, I bet you would have told me I have $1.00, am I right? Now take a good hard look at the program you posted.

Matt L at Yahoo! Answers Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.