public void writeC switchunits case F case f convertedDegrees 5 degrees 329

Public void writec switchunits case f case f

This preview shows page 6 - 9 out of 20 pages.

public void writeC() { switch(units) { case 'F': case 'f': convertedDegrees = 5 * (degrees -32)/9; //Divide by 10.0 so division result is floating point //(with one decimal place). System.out.println("Temperature = " + Math.round(convertedDegrees*10)/10.0 + " degrees Celsius."); break; case 'C': case 'c': System.out.println("Temperature = " + Math.round(degrees*10)/10.0 + " degrees Celsius."); break; default: System.out.println("Unknown units - cannot determine temperature."); System.out.println("Next time enter either " + "'F' for Fahrenheit or 'C' for Celsius."); } } //Method to display temperature in degrees F. //Note that the temperature is rounded and //displayed to one decimal place. public void writeF() { switch(units) { case 'F': case 'f': System.out.println("Temperature = " + Math.round(degrees*10)/10.0 + " degrees Fahrenheit."); break; case 'C': case 'c': convertedDegrees = degrees * 9/5 + 32; System.out.println("Temperature = " Math.round(convertedDegrees*10)/10.0 + " degrees Fahrenheit."); break; default: System.out.println("Unknown units - cannot determine temperature."); System.out.println("Next time enter either " + "'F' for Fahrenheit or 'C' for Celsius."); } } //Two methods to get (return) temperature value in either degrees C or F. //Method to return temperature in degrees C. //Returns value of variable degrees if units is an invalid character (other //than c, C, f, or F). public double getC() {
Image of page 6
CSJA1DP/203/0/2016 7 switch(units) { case 'F': case 'f': convertedDegrees = 5 * (degrees -32)/9; return Math.round(convertedDegrees*10)/10.0; case 'C': case 'c': return Math.round(degrees*10)/10.0; default: return Math.round(degrees*10)/10.0; } } //Method to return temperature in degrees F. //Returns value of variable degrees if units is an invalid character (other //than c, C, f, or F). public double getF() { switch(units) { case 'F': case 'f': return Math.round(degrees*10)/10.0; case 'C': case 'c': convertedDegrees = degrees * 9/5 + 32; return Math.round(convertedDegrees*10)/10.0; default: return Math.round(degrees*10)/10.0; } } //Three methods to set (reset) the parameter values. //Method to set both parameters. //Postconditions: Does not guarantee legitimate value for //units (allows any character, not just c, C, f, or F. public void set(double newDegrees, char newUnits) { degrees = newDegrees; units = newUnits; } //Method to set temperature only. public void set(double newDegrees) { degrees = newDegrees; } //Method to set units only. //Postconditions: Does not guarantee legitimate value for //units (allows any character, not just c, C, f, or F. public void set(char newUnits) { units = newUnits; }
Image of page 7
8 //Three comparison methods. //Are two temperatures equal? //Convert temperatures to an integer number of tenths of degrees //to avoid errors due to comparing floating point numbers, which //often cannot be stored in memory precisely. public boolean equals(Temperature another) { //Use same units for comparison return (Math.round(this.getC()*10) == Math.round(another.getC()*10)); } //Is one temperature greater than another? //Convert temperatures to an integer number of tenths of degrees //to avoid errors due to comparing floating point numbers, which //often cannot be stored in memory precisely.
Image of page 8
Image of page 9

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture