-
Comparison methods
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();
*/
}
}
-
Please Help With Comparison Methods
Now, I am trying to implement three comparison methods. I am not quite sure if I am doing it right, because when I test for equality, the two values are always equal. Maybe I am just not writing the driver (main) correctly. Here is my code:
Code:
//---------------------------------------------------------------------
//Three comparison methods, one to test whether two temperatures are equal,
//one to test whether one temperature is greater than another, and one to
//test whether one temperature is less than another.
//----------------------------------------------------------------------
//comparison method to test whether two temperatures are equal.
public boolean equals(TestTemp otherTemp)
{
return(this.tempValue == (otherTemp.getC()));
}
//----------------------------------------------------------------------
//comparison method to test whether one temperatures is greater than another.
public boolean greaterThan(TestTemp otherTemp)
{
return(this.tempValue > (otherTemp.getC()));
}
//----------------------------------------------------------------------
//comparison method to test whether one temperature is less than another.
public boolean lessThan(TestTemp otherTemp)
{
return(this.tempValue < (otherTemp.getC()));
}
//----------------------------------------------
//----------------------------------------------
//Here is the code from my driver
TestTemp t10 = new TestTemp();
TestTemp t11 = new TestTemp();
double test1ValueEqual = 0.0;
char test1ScaleEqual = 'c';
double test2ValueEqual = 32.0;
char test2ScaleEqual = 'f';
t10.set(test1ValueEqual, test1ScaleEqual);
t11.set(test2ValueEqual, test2ScaleEqual);
if(t10.equals(t11))
{
System.out.println("0.0 degrees C is equal to 32 degrees F.");
}
else
{
System.out.println("0.0 degrees C is not equal to 32 degrees F.");
}
TestTemp t12 = new TestTemp(0.0, 'c');
TestTemp t13 = new TestTemp(35.0, 'f');
double test3ValueEqual = 0.0;
char test3ScaleEqual = 'c';
double test4ValueEqual = 35.0;
char test4ScaleEqual = 'f';
t12.set(test1ValueEqual, test1ScaleEqual);
t13.set(test2ValueEqual, test2ScaleEqual);
if(t12.equals(t13))
{
System.out.println("0.0 degrees C is equal to 35 degrees F.");
}
else
{
System.out.println("0.0 degrees C is not equal to 35 degrees F.");
}
TestTemp t14 = new TestTemp();
TestTemp t15 = new TestTemp();
double test1ValueLessThan = 18.5;
char test1ScaleLessThan = 'c';
double test2ValueLessThan = 137.9;
char test2ScaleLessThan = 'f';
t14.set(test1ValueLessThan, test1ScaleLessThan);
t15.set(test2ValueLessThan, test2ScaleLessThan);
if(t14.lessThan(t15))
{
System.out.println("18.5 degrees C is less than 137.9 degrees F.");
}
else
{
System.out.println("18.5 degrees C is not less than 137.9 degrees F.");
}
Thanks,
Eric
-
Just a couple of things: If you want to override the java.lang.Object method
equals you must duplicate its methods signature completely, like
public boolean equals (Object ob).
So if you do implement it for the famed class 'MyClass' you do like:
Code:
public boolean equals (Object ob) {
if (!(ob instanceof MyClass)) return false; // not even a MyClass
// the next lines assumes that there is a getName method in MyClass that
// return a string value that is unique for a MyClass instance, It could be
// a numeric value, a set of values and so on...
MyClass aMyClass=(MyClass)ob;
return this.getName().equals(aMyClass.getName());
}
Implemented this way, all other java classes that checks for the containment
of specific objects (like Vectors, ArrayLists, Hashtables) will do their search according
to the conditions set in the equals methos of your class.
Finally: you cannot use the '==' operator for comparing Strings, '==' used on
objects will just compare the pointer address values, use the equals() method for
Strings (and other objects).
Last edited by sjalle; 03-06-2005 at 06:50 PM.
eschew obfuscation
-
I am not familiar with the famed class 'MyClass'. It is not in my textbook.
I am not sure I understand everything that you are doing here.
In my code, If I change this:
Code:
public boolean equals(TestTemp otherTemp)
{
return(this.tempValue == (otherTemp.getC()));
}
and make it this:
Code:
public boolean equals(TestTemp otherTemp)
{
return(this.equals(otherTemp.getC()));
}
I get an error that states: double cannot be dereferenced.
Thank you,
Eric
-
OK, I think I have it figured out now. Here is what I have now. I still have some things to work on, but overall, it's doing what I need it to do. Any suggestions are appreciated.
Thanks,
Eric
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';
round();
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.");
//This should take the user to...?
}
}
//----------------------------------------------------------------------
//constructor method for both instance variables tempValue and tempScale
public TestTemp(double initialTempValue, char initialTempScale)
{
round();
tempValue = initialTempValue;
initialTempScale = Character.toUpperCase(initialTempScale);
if(initialTempScale == 'C' || initialTempScale == 'F')
{
tempScale = initialTempScale;
}
else
{
System.out.println("Error: Must be C or F.");
//This should take the user to...?
}
}
//----------------------------------------------------------------------
//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')
{
return tempScale;
}
else
{
double tempF;
tempF = ((9 * tempValue)/5 + 32); //tempF = ((9*tempC)/5 + 32)
tempValue = tempF;
tempScale = 'F';
round();
return tempF;
}
}
//accessor method to return the degrees Celsius
public double getC()
{
if(tempScale == 'C')
{
return tempScale;
}
else
{
double tempC;
tempC = (5 * (tempValue - 32)/9); //tempC = 5*(tempF - 32)/9
tempValue = tempC;
tempScale = 'C';
round();
return tempC;
}
}
//------------------------------------------------------------------------
//round method returns the tempValue rounded to tenths
public void round()
{
tempValue = Math.round(tempValue * 10.0)/10.0;
}
//----------------------------------------------------------------------
//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)
{
round();
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.");
//This should take the user to...?
}
//tempValue is unchanged
}
//----------------------------------------------------------------------
//reset method to reset tempValue and tempScale
public void set(double newTempValue, char newTempScale)
{
round();
tempValue = newTempValue;
newTempScale = Character.toUpperCase(newTempScale);
if(newTempScale == 'C' || newTempScale == 'F')
{
tempScale = newTempScale;
}
else
{
System.out.println("Error: Must be C or F.");
//This should take the user to...?
}
//tempValue and tempScale are reset
}
//---------------------------------------------------------------------
//Three comparison methods, one to test whether two temperatures are equal,
//one to test whether one temperature is greater than another, and one to
//test whether one temperature is less than another.
//----------------------------------------------------------------------
//comparison method to test whether two temperatures are equal.
public boolean equals(TestTemp otherTemp)
{
return(this.tempValue == (otherTemp.getC()));
}
//----------------------------------------------------------------------
//comparison method to test whether one temperatures is greater than another.
public boolean greaterThan(TestTemp otherTemp)
{
return(this.tempValue > (otherTemp.getC()));
}
//----------------------------------------------------------------------
//comparison method to test whether one temperature is less than another.
public boolean lessThan(TestTemp otherTemp)
{
return(this.tempValue < (otherTemp.getC()));
}
}//end of TestTemp class
//-------------------------------------------------------------------------
//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)
{
//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 and t11 test the equals method to show equality
TestTemp t10 = new TestTemp(0.0, 'c');
TestTemp t11 = new TestTemp(32.0, 'f');
if(t10.equals(t11))
{
System.out.println("0.0 degrees C is equal to 32.0 degrees F.");
}
else
{
System.out.println("0.0 degrees C is not equal to 32.0 degrees F.");
}
//t12 and t13 test the equals method to show equality
TestTemp t12 = new TestTemp(-40.0, 'c');
TestTemp t13 = new TestTemp(-40.0, 'f');
if(t12.equals(t13))
{
System.out.println("-40.0 degrees C is equal to -40.0 degrees F.");
}
else
{
System.out.println("-40.0 degrees C is not equal to -40.0 degrees F.");
}
//t14 and t15 test the equals method to show equality
TestTemp t14 = new TestTemp(100.0, 'c');
TestTemp t15 = new TestTemp(212.0, 'f');
if(t14.equals(t15))
{
System.out.println("100.0 degrees C is equal to 212.0 degrees F.");
}
else
{
System.out.println("100.0 degrees C is not equal to 212.0 degrees F.");
}
//t16 and t17 test the equals method to show inequality
TestTemp t16 = new TestTemp(0.0, 'c');
TestTemp t17 = new TestTemp(35.0, 'f');
if(t16.equals(t17))
{
System.out.println("0.0 degrees C is equal to 35 degrees F.");
}
else
{
System.out.println("0.0 degrees C is not equal to 35 degrees F.");
}
//t18 and t19 test the lessThan method to show less than
TestTemp t18 = new TestTemp(18.5, 'c');
TestTemp t19 = new TestTemp(137.9, 'f');
if(t18.lessThan(t19))
{
System.out.println("18.5 degrees C is less than 137.9 degrees F.");
}
else
{
System.out.println("18.5 degrees C is not less than 137.9 degrees F.");
}
//t20 and t21 test the lessThan method to show not less than
TestTemp t20 = new TestTemp(35.0, 'c');
TestTemp t21 = new TestTemp(85.9, 'f');
if(t20.lessThan(t21))
{
System.out.println("35.0 degrees C is less than 85.9 degrees F.");
}
else
{
System.out.println("35.0 degrees C is not less than 85.9 degrees F.");
}
//t22 and t23 test the greaterThan method to show greater than
TestTemp t22 = new TestTemp(37.4, 'c');
TestTemp t23 = new TestTemp(89.5, 'f');
if(t22.greaterThan(t23))
{
System.out.println("37.4 degrees C is greater than 89.5 degrees F.");
}
else
{
System.out.println("37.4 degrees C is not greater than 89.5 degrees F.");
}
//t24 and t25 test the greaterThan method to show not greater than
TestTemp t24 = new TestTemp(18.7, 'c');
TestTemp t25 = new TestTemp(87.9, 'f');
if(t24.greaterThan(t25))
{
System.out.println("18.7 degrees C is greater than 87.9 degrees F.");
}
else
{
System.out.println("18.7 degrees C is not greater than 87.9 degrees F.");
}
System.out.println("Press Enter key to end program.");
String junk;
junk = SavitchIn.readLine();
}
}
-
I have made much progress. I believe I have everything straighted out without any possible side effects.
Here is one of my comparison methods:
Code:
//comparison method to test whether one temperatures is greater than another.
public boolean greaterThan(Temperature t)
{
if(this.tempScale == (t.tempScale))
{
return(this.tempScale == t.tempScale);
}
else if(this.tempScale == 'C')//this is C
{
return(this.tempValue > (t.getC()));
}
else //this is F
{
return(this.tempValue > (t.getF()));
}
}
Originally, I had my driver (test) code like this:
Code:
//t26 and t27 test the greaterThan method to show not greater than
Temperature t26 = new Temperature(37.4, 'c');
Temperature t27 = new Temperature(89.5, 'f');
if(t26.greaterThan(t27))
{
System.out.println("37.4 degrees C is greater than 89.5 degrees F.");
}
else
{
System.out.println("37.4 degrees C is not greater than 89.5 degrees F.");
}
I have been trying to find a better way to either write the test code or change what the first if statement returns. I am going in circles here. Can anyone lend some advice or at least try to push me in the direction I need to go? I'm all ears.
Thanks,
Eric
-
Does anyone answer questions here?
-
Is this really that hard of a question? I think it's a challenge, but then again, I've only been learning Java for a little over a month.
Thank you,
Eric
-
Well, there's quite a lot to say about your code, Eric. So here's just a bit of a comment. Hope it'll give you some hints.
1. What sjalle wanted to point out:
Try this test with your temperature class:
Code:
TestTemp t1 = new TestTemp(37.4, 'c');
TestTemp t2 = t1;
Object o1 = new Object();
Object o2 = t1;
System.out.println( t1.equals(t2) );
System.out.println( t1.equals(o1) );
System.out.println( t1.equals(o2) );
the output would be:
true
false
false
Is THAT really what you want? (in particular the last one)
2. Implement a suitable toString() method
3. Consider implementing the interface Comparable
(maybe Comparable<TestTemp> if you're using java 1.5)
4. in your greaterThan() that you posted last:
you reuse the already implemented methods getC() and getF(). that's good.
but why then test again if scales match or not?
BTW: look again at the first case of the if there...
5. getF(), getC():
These are getter methods! They should NOT change the state of the object. But they do if the scale is not C or F respectively. That I would call an unexpected side-effect (unnecessary calculations with rounding again and again). Also I'm quite sure that you do not really want to return the _scale_ here...
6. constructors:
You already corrected the thing with negative temperatures...
But you really should raise an IllegalArgumentException in case of an
unsupported scale rather than just print some error message (even not on STDERR!)
7. Introduce some CONSTANTS for the scale (or better use enum as of java 1.5). Just think about when you want your class to also support Kelvin eg.
-
Some of the things you mentioned are above me right now. This is what I needed. I was wanting to compare two temperatures, using a boolean method, but I didn't know what to do if the temperature scales were the same. Please let me know if I am on the right track, or if I am off in left field somewhere.
Thanks,
Eric
Here is what I have.
[CODE]
//----------------------------------------------------------------------
//comparison method to test whether two temperatures are equal.
public boolean equals(Temperature t)
{
if(this.tempScale == 'C')//this is C
{
return(this.tempValue == (t.getC()));
}
else //this is F
{
return(this.tempValue == (t.getF()));
}
}
//----------------------------------------------------------------------
//comparison method to test whether one temperatures is greater than another.
public boolean greaterThan(Temperature t)
{
if(this.tempScale == 'C') //this is C
{
return(this.tempValue > (t.getC()));
}
else //this is F
{
return(this.tempValue > (t.getF()));
}
}
//----------------------------------------------------------------------
//***************************************************
//Here is the test code I have written since my last post
//***********************************************
//////////////////////////////////////////////////////////////////////////////////
//This tests to see if I can change and compare tempScales, uses the equals method
Temperature t28 = new Temperature(69, 'c');
Temperature t29 = new Temperature(96, 'f');
t28.getTempValue();
t29.getTempValue();
t28.getF();
t29.getC();
t28.writeOutput();
t29.writeOutput();
if(t28.getTempScale() == t29.getTempScale())
{
System.out.println("True: Both scales are the same at this point.");
}
else
{
System.out.println("False: Both scales are not the same at this point.");
}
t28.getC();
t28.writeOutput();
t29.writeOutput();
if(t28.getTempScale() == t29.getTempScale())
{
System.out.println("True: Both scales are the same at this point.");
}
else
{
System.out.println("False: Both scales are not the same at this point.");
}
if(t28.getTempScale() == t29.getTempScale())
{
if(t28.equals(t29))
{
System.out.println(t28.getTempValue() + " " + t28.getTempScale() + " equals " + t29.getTempValue() + " " + t29.getTempScale() + ".");
}
else
{
System.out.println(t28.getTempValue() + " " + t28.getTempScale() + " does not equal " + t29.getTempValue() + " " + t29.getTempScale() + ".");
}
}
else
{
System.out.println("Else...");
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//This tests to see if I can change and compare tempScales and tempValues, uses the equals method
Temperature t30 = new Temperature(69, 'c');
Temperature t31 = new Temperature(96, 'f');
t30.getTempValue();
t31.getTempValue();
t30.writeOutput();
t31.writeOutput();
if(t30.getTempScale() == t31.getTempScale())
{
System.out.println("True: Both scales are the same at this point.");
}
else
{
System.out.println("False: Both scales are not the same at this point.");
}
t30.writeOutput();
t31.writeOutput();
if(t30.getTempScale() == t31.getTempScale())
{
System.out.println("True: Both scales are the same at this point.");
}
else
{
System.out.println("False: Both scales are not the same at this point.");
}
if(t30.getTempScale() == t31.getTempScale())
{
if(t30.equals(t31))
{
System.out.println(t30.getTempValue() + " " + t30.getTempScale() + " equals " + t31.getTempValue() + " " + t31.getTempScale() + ".");
}
else
{
System.out.println(t30.getTempValue() + " " + t30.getTempScale() + " does not equal " + t31.getTempValue() + " " + t31.getTempScale() + ".");
}
}
else if(t30.equals(t31))
{
System.out.println(t30.getTempValue() + " " + t30.getTempScale() + " equals " + t31.getTempValue() + " " + t31.getTempScale() + ".");
}
else
{
System.out.println(t30.getTempValue() + " " + t30.getTempScale() + " does not equal " + t31.getTempValue() + " " + t31.getTempScale() + ".");
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//This tests to see if I can change and compare tempScales and tempValues
Temperature t32 = new Temperature(69, 'c');
Temperature t33 = new Temperature(96, 'f');
t32.getTempValue();
t33.getTempValue();
t32.writeOutput();
t33.writeOutput();
if(t32.getTempScale() == t33.getTempScale())
{
System.out.println("True: Both scales are the same at this point.");
}
else
{
System.out.println("False: Both scales are not the same at this point.");
}
t32.writeOutput();
t33.writeOutput();
if(t32.getTempScale() == t33.getTempScale())
{
System.out.println("True: Both scales are the same at this point.");
}
else
{
System.out.println("False: Both scales are not the same at this point.");
}
if(t32.getTempScale() == t33.getTempScale())
{
if(t32.greaterThan(t33))
{
System.out.println(t32.getTempValue() + " " + t32.getTempScale() + " is greater than " + t33.getTempValue() + " " + t33.getTempScale() + ".");
}
else
{
System.out.println(t32.getTempValue() + " " + t32.getTempScale() + " is not greater than " + t33.getTempValue() + " " + t33.getTempScale() + ".");
}
}
else if(t32.greaterThan(t33))
{
System.out.println(t32.getTempValue() + " " + t32.getTempScale() + " is greater than " + t33.getTempValue() + " " + t33.getTempScale() + ".");
}
else
{
System.out.println(t32.getTempValue() + " " + t32.getTempScale() + " is not greater than " + t33.getTempValue() + " " + t33.getTempScale() + ".");
}
//////////////////////////////////////////////////////////////////////////////////
-
Just a note:
If you do what meisl says in pt. 4 you will also be able to sort your
temperatures, practically free of charge, regardless if they are F or C degrees.
eschew obfuscation
-
1. What sjalle wanted to point out:
Try this test with your temperature class:
TestTemp t1 = new TestTemp(37.4, 'c');
TestTemp t2 = t1;
Object o1 = new Object();
Object o2 = t1;
System.out.println( t1.equals(t2) );
System.out.println( t1.equals(o1) );
System.out.println( t1.equals(o2) );the output would be:
true
false
false
Is THAT really what you want? (in particular the last one)
I have made some changes, if this still applies, let me know.
2. Implement a suitable toString() method
I am not going to use a toString() method because it will make things more confusing for me right now. I am trying to stick to the requirements of the project. If you really think it will help me, please tell me how it will help.
3. Consider implementing the interface Comparable
(maybe Comparable<TestTemp> if you're using java 1.5)
I have never heard of it. I just started Java about a month ago. I am not trying to get ahead of the textbook.
4. in your greaterThan() that you posted last:
you reuse the already implemented methods getC() and getF(). that's good.
but why then test again if scales match or not?
BTW: look again at the first case of the if there...
I believe I have changed them since this.
5. getF(), getC():
These are getter methods! They should NOT change the state of the object. But they do if the scale is not C or F respectively. That I would call an unexpected side-effect (unnecessary calculations with rounding again and again). Also I'm quite sure that you do not really want to return the _scale_ here...
I believe I have changed this too.
6. constructors:
You already corrected the thing with negative temperatures...
But you really should raise an IllegalArgumentException in case of an
unsupported scale rather than just print some error message (even not on STDERR!)
I have not learned about exceptions yet. We are in chapter 5 and exceptions do not come until chapter 7. I do not know what stderr means.
7. Introduce some CONSTANTS for the scale (or better use enum as of java 1.5). Just think about when you want your class to also support Kelvin eg.
I am not trying to get this deep yet. I am trying to make sure I can understand everything in my program, that way, if I do want to add Kelvin later, I will know exactly how to do it.
I really appreciate all of your comments. I hope you understand the reasons that I want to start walking before I try to start running.
If you want me to post all of my code (a lot of it has been changed), then let me know.
Thank you,
Eric
-
Ok, Eric. Since I feel that you mean it (learning java), I suggest that we do a little online crash course, right now. How about that?
But there's some rules:
1. We try to do it step by step and systematically. In what is to do next and what systematically means, you have to trust me (or sjalle, if he wants to participate).
2. You try to answer questions as honestly and as accurate and concise as possible.
(that also means not to post thousands of lines of code)
3. You don't feel pissed about me giving these rules (and anything else I say) ;-)
4. We neither touch any 1.5 stuff nor exceptions but:
- we'll have a look at Comparable
- you're gonna implement toString()
If that sounds ok to you, we can start with the most important question of all:
When is your homework due?
;-)
-
Ah, I forgot:
- toString() will help you save a lot of typing you're already doing in your test code and thus make your code easier to understand.
- implementing Comparable will concentrate all comparison code in one place and
thus make your code easier to understand. It will also help you to understand the
problem with your equals(), that still exists as far as I can tell.
- constants are definitely not difficult to understand and will make your code easier to understand PLUS will make it easier to later extend functionality. I can tell you,
teachers LOVE to come up later with such.
- what is missing right now is an idea how to elegantly do the conversion stuff (and keep it in one place). When finished, you will have understood why the assignment as it seems to have been given to you is somewhat misleading but maybe just in being so is all the more suitable for learning.
However, I'm gonna leave the computer now and check back tomorrow.
CU
Last edited by meisl; 03-07-2005 at 09:46 PM.
-
Let's do it.
When is your homework due?
The homework was due on Sunday, March 6. Right now, it is Tuesday Morning 7:35am (I'm on Guam).
Eric
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|