Hello,
I am working on a program for school. I am required to have the methods that are in my program. I also need to add three comparison methods to it. I just want to make sure I am on the right track before I attempt the comparison methods.
Thanks,
Eric
Here is what I have so far.
Code:public class TestTemp { //Two parameters, a temperature value (a floating-point number) //and a character for the scale, either 'C' for Celsius or 'F' //for Fahrenheit. private double tempValue; //in degrees private char tempScale; //in either 'C' or 'F' //------------------------------------------------------------------------- public void writeOutput() { System.out.println("The temperature is " + (tempValue) + " degrees " + tempScale + "."); } //------------------------------------------------------------------------- //Four constructor methods, one for each instance variable, //(assume zero degrees if no value isspecified and Celsius // if no scale is specified), one with two parameters for the //two instance variables, and a default constructor (set to //zero degreees Celsius. //---------------------------------------------------------------------- //constructor method for the instance variable tempValue public TestTemp(double initialTempValue) { tempScale = 'C'; if(initialTempValue < 0) { System.out.println("Error: Negative temperature value."); System.exit(0); } else { round(initialTempValue); tempValue = initialTempValue; } } //---------------------------------------------------------------------- //constructor method for the instance variable tempScale public TestTemp(char initialTempScale) { tempValue = 0; initialTempScale = Character.toUpperCase(initialTempScale); if(initialTempScale == 'C' || initialTempScale == 'F') { tempScale = initialTempScale; } else { System.out.println("Error: Must be C or F."); System.exit(0); } } //---------------------------------------------------------------------- //constructor method for both instance variables tempValue and tempScale public TestTemp(double initialTempValue, char initialTempScale) { if(initialTempValue < 0) { System.out.println("Error: Negative temperature value."); System.exit(0); } else { round(initialTempValue); tempValue = initialTempValue; } initialTempScale = Character.toUpperCase(initialTempScale); if(initialTempScale == 'C' || initialTempScale == 'F') { tempScale = initialTempScale; } else { System.out.println("Error: Must be C or F."); System.exit(0); } } //---------------------------------------------------------------------- //default constructor method public TestTemp() { tempValue = 0.0; tempScale = 'C'; } //---------------------------------------------------------------------- //Two accessor (get) methods, one to return the degrees Celsius, //and one to return the degrees Fahrenheit. //Round to the nearest tenth of a degree. //accessor method to return the degrees Fahrenheit. public double getF() { if(tempScale == 'F') { System.out.println("Error: The temp scale is already Fahrenheit."); return 0; } else { double tempF; tempF = ((9 * tempValue)/5 + 32); //tempF = ((9*tempC)/5 + 32) tempValue = tempF; tempScale = 'F'; round(tempValue); return tempF; } } //accessor method to return the degrees Celsius public double getC() { if(tempScale == 'C') { System.out.println("Error: The temp scale is already Celsius."); return 0; } else { double tempC; tempC = (5 * (tempValue - 32)/9); //tempC = 5*(tempF - 32)/9 tempValue = tempC; tempScale = 'C'; round(tempValue); return tempC; } } //------------------------------------------------------------------------ //round method returns the tempValue rounded to tenths public double round(double roundTempValue) { tempValue = Math.round(tempValue * 10.0)/10.0; return tempValue; } //------------------------------------------------------------------------- //Three reset methods, one to reset the value, one to reset the //scale ('F' or 'C'), and one to reset both the value and the scale. //---------------------------------------------------------------------- //reset method to reset tempValue public void set(double newTempValue) { if(newTempValue < 0) { System.out.println("Error: Negative temperature value."); System.exit(0); } else { round(newTempValue); tempValue = newTempValue; } //tempScale is unchanged } //---------------------------------------------------------------------- //reset method to reset tempScale public void set(char newTempScale) { newTempScale = Character.toUpperCase(newTempScale); if(newTempScale == 'C' || newTempScale == 'F') { tempScale = newTempScale; } else { System.out.println("Error: Must be C or F."); System.exit(0); } //tempValue is unchanged } //---------------------------------------------------------------------- //reset method to reset tempValue and tempScale public void set(double newTempValue, char newTempScale) { if(newTempValue < 0) { System.out.println("Error: Negative temperature value."); System.exit(0); } else { round(newTempValue); tempValue = newTempValue; } newTempScale = Character.toUpperCase(newTempScale); if(newTempScale == 'C' || newTempScale == 'F') { tempScale = newTempScale; } else { System.out.println("Error: Must be C or F."); System.exit(0); } //tempValue and tempScale are reset } //------------------------------------------------------------------------ /* public static boolean equals(TestTemp otherTemp) { initialTempScale = Character.toUpperCase(initialTempScale); return((this.tempValue == otherTemp.tempValue) && (this.tempScale == otherTemp.tempScale)); } */ } //------------------------------------------------------------------------- //Driver program that tests all the methods. //Use each one of the constructors. //Include at least one true and one false //case for each of the comparison methods. //Test the following: // 0.0 degrees C = 32 degrees F // -40 degrees C = -40 degrees F // 100 degrees C = 212 degrees F public class TestTempDriver { public static void main(String[] args) { //t1, t2, t3, and t4 test the four constructors TestTemp t1 = new TestTemp(); t1.writeOutput(); TestTemp t2 = new TestTemp(22.222); t2.writeOutput(); TestTemp t3 = new TestTemp('f'); t3.writeOutput(); TestTemp t4 = new TestTemp(44.444, 'f'); t4.writeOutput(); //t5 tests the set method with both the double and char parameters TestTemp t5 = new TestTemp(55.555, 'f'); System.out.println("The initial temp for t5 is:"); t5.writeOutput(); System.out.println("Please enter the correct temp:"); double correctTemp = SavitchIn.readLineDouble( ); System.out.println("Please enter the correct temp scale:"); char correctScale = SavitchIn.readLineNonwhiteChar( ); t5.set(correctTemp, correctScale); System.out.println("The updated temp for t5 is:"); t5.writeOutput(); //t6 tests the set method with the double parameter TestTemp t6 = new TestTemp(66.666, 'f'); System.out.println("The initial temp for t6 is:"); t6.writeOutput(); System.out.println("Please enter the correct temp:"); correctTemp = SavitchIn.readLineDouble( ); t6.set(correctTemp); System.out.println("The updated temp for t6 is:"); t6.writeOutput(); //t7 tests the set method with the char parameter TestTemp t7 = new TestTemp(77.777, 'f'); System.out.println("The initial temp for t7 is:"); t7.writeOutput(); System.out.println("Please enter the correct temp scale:"); correctScale = SavitchIn.readLineNonwhiteChar( ); t7.set(correctScale); System.out.println("The updated temp for t7 is:"); t7.writeOutput(); */ //t8 tests the getF accessor method TestTemp t8 = new TestTemp(); double convertTemp = 88.888; char convertScale = 'c'; t8.set(convertTemp, convertScale); t8.getF(); t8.writeOutput(); //t9 tests the getC accessor method TestTemp t9 = new TestTemp(); convertTemp = 99.999; convertScale = 'f'; t9.set(convertTemp, convertScale); t9.getC(); t9.writeOutput(); //t10 tests the equals comparison method TestTemp t10 = new TestTemp(1010.10, 'f'); double testValueEqual = 1010.10; char testScaleEqual = 'f'; t10.set(testValueEqual, testScaleEqual); t10.equals(t10); t10.writeOutput(); TestTemp t11 = new TestTemp(5, 'f'); TestTemp t12 = new TestTemp(5, 'f'); if(t11 == t12) { System.out.println("Match with ==."); } else { System.out.println("Do Not match with ==."); } if(t11.equals(t12)) { System.out.println("Match with the method equals."); } else { System.out.println("Do Not match with the method equals."); } /* System.out.println("Press Enter key to end program."); String junk; junk = SavitchIn.readLine(); */ } }


Reply With Quote


Bookmarks