Results 1 to 20 of 39
- 10-30-2012, 09:57 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 50
- Rep Power
- 0
Can't get equals() method to work correctly.
I have to write a Temperature class to go with the TemperatureTest driver class. I'm almost finished except for a few things I'm stuck on that I can't figure out. (I came here as a last resort). I AM NOT SUPPOSED TO EDIT THE TEMPERATURETEST CLASS. SO, PLEASE DON'T SUGGEST THAT I DO SO.
I pretty much got everything working correctly except that I can't get test case #18 to return true. I know I'm supposed to convert Fahrenheit to Celsius, which I did, but I'm still stuck. Will someone please point me in the right direction. My Temperature class code is below my result for test case #18, and the TemperatureTest code is below that.
Test case 18: equals test 1.
T1 = 100 degrees C.
T2 = 212 degrees F.
Here are the two temperatures converted to degrees C:
Temperature = 100.0 degrees C
Temperature = 100.0 degrees F should say 100.0 degrees C. What did I do wrong?
Verify results: should be true.
false should be true. what did I do wrong?
Java Code:public class Temperature { private double degrees; private char type; public Temperature() { degrees = 0; type = 'C'; } public Temperature(double degrees) { this.degrees = degrees; type = 'c'; } public Temperature(char type) { this.type = type; degrees = 0.0; } public Temperature(double degrees, char type) { this.degrees = degrees; this.type = type; } public void writeOutput() { System.out.println ("Temperature = " + degrees + " degrees " + type); } public void writeC() { if (type == 'F') { double TempC = (degrees - 32) * 5/9; double result = Math.round(TempC*10)/10.0; System.out.println("Temperature = " + result + " degrees C"); } else if (type == 'C') { System.out.println ("Temperature = " + degrees + " degrees " + type); } } public void writeF() { if (type == 'F') { System.out.println ("Temperature = " + degrees + " degrees " + type); } else if (type == 'C') { double TempF = (9/5) * degrees + 32; System.out.println("Temperature = " + TempF + " degrees F"); } } public double getF() { if (type == 'F') { return degrees; } else { double TempF = (9/5) * degrees + 32; return TempF; } } public double getC() { if (type == 'F') { double TempC = (degrees - 32) * 5/9; double result = Math.round(TempC*10)/10.0; return result; } else { return degrees; } } public void set(double degrees, char type) { this.degrees = degrees; this.type = type; } public void set(double degrees) { this.degrees = degrees; } public void set(char type) { this.type = type; } public boolean equals() { if (type == 'F') { double TempC = (degrees - 32) * 5/9; double result = Math.round(TempC*10)/10.0; return (this.degrees == result); } else { return false; } } public String toString() { return new String(degrees + " " + type); } }Java Code:import java.util.Scanner; public class TemperatureTest { public static void main(String[] args) { //dummy variable to stop scrolling so user can see test results String junk; Scanner scan = new Scanner(System.in); System.out.println(); System.out.println("Test case 1: default constructor and"); System.out.println("writeOutput() method."); System.out.println(); Temperature t1 = new Temperature(); System.out.println("Results of default constructor:"); System.out.println("Verify 0 degrees C."); System.out.println(); t1.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); /*System.out.println("Test case 2: readInput() method."); t1.readInput(); System.out.println(); System.out.println("Verify temperature and units:"); System.out.println("Should be whatever you just entered."); System.out.println(); t1.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println();*/ System.out.println("Test case 3: constructor with just" + " temperature."); Temperature t2 = new Temperature(20.5); System.out.println(); System.out.println("Verify 20.5 degrees C."); System.out.println(); t2.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 4: constructor with just units," + " f."); System.out.println(); System.out.println("Verify 0 degrees f."); System.out.println(); Temperature t3 = new Temperature('f'); t3.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 5: constructor with just units," + " c."); System.out.println(); System.out.println("Verify 0 degrees c."); System.out.println(); Temperature t4 = new Temperature('c'); t4.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 6: constructor with" + " temperature and units."); System.out.println(); System.out.println("Verify -51.2 degrees F."); System.out.println(); Temperature t5 = new Temperature(-51.2, 'F'); t5.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 7: read degrees C, " + "original in F."); System.out.println(); System.out.println("Verify -46.2 degrees Celsius."); System.out.println(); t5.writeC(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 8: read degrees C, " + "original in C."); System.out.println(); System.out.println("Verify 0 degrees Celsius."); System.out.println(); t4.writeC(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 9: read degrees F, " + "original in C."); System.out.println(); System.out.println("Verify 32 degrees Fahrenheit"); System.out.println(); t4.writeF(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 10: read degrees F, " + " original in F."); System.out.println(); System.out.println("Verify -51.2 degrees Fahrenheit"); System.out.println(); t5.writeF(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 11: getF method with " + "original temperature in F."); System.out.println(); System.out.println("Verify -51.2"); System.out.println(); System.out.println(t5.getF()); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 12: getF method with original" + " temperature in C."); System.out.println("Verify 32"); System.out.println(); System.out.println(t4.getF()); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 13: getC method with " + "original temperature in F."); System.out.println("Verify -46.2"); System.out.println(); System.out.println(t5.getC()); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println(); System.out.println("Test case 14: getC method with " + "original temperature in C."); System.out.println("Verify 0"); System.out.println(); System.out.println(t4.getC()); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println(); System.out.println("Test case 15: set method for " + "both parameters."); System.out.println("t5 before:"); t5.writeOutput(); t5.set(72.8, 'C'); System.out.println("After set: verify 72.8 degrees C."); System.out.println(); t5.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 16: set method for just " + "temperature."); System.out.println("t5 before:"); t5.writeOutput(); t5.set(100); System.out.println("After set: verify 100 degrees C."); System.out.println(); t5.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 17: set method for just units."); System.out.println("t5 before:"); t5.writeOutput(); t5.set('F'); System.out.println("After set: verify 100 degrees F."); System.out.println(); t5.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 18: equals test 1."); System.out.println(); t1.set(100, 'C'); t2.set(212, 'F'); System.out.println("T1 = 100 degrees C."); System.out.println("T2 = 212 degrees F."); System.out.println(); System.out.println("Here are the two temperatures " + "converted to degrees C:"); t1.writeC(); t2.writeC(); System.out.println(); System.out.println("Verify results: should be true."); System.out.println(); System.out.println(t1.equals(t2)); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 19: equals test 2."); System.out.println(); t1.set(100.1); System.out.println("T1 changed to 100.1 degrees C."); System.out.println("T2 = 212 degrees F."); System.out.println(); System.out.println("Here are the two temperatures in " + " degrees C:"); t1.writeC(); t2.writeC(); System.out.println(); System.out.println("Verify results: should be false."); System.out.println(); System.out.println(t1.equals(t2)); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 20: equals test 3."); System.out.println(); t1.set(100); t2.set(212.1); System.out.println("T1 changed back to 100 degrees C."); System.out.println("T2 = 212.1 degrees F."); System.out.println(); System.out.println("Here are the two temperatures in " + "degrees C:"); t1.writeC(); t2.writeC(); System.out.println(); System.out.println("Verify results: should be false."); System.out.println(); System.out.println(t1.equals(t2)); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 21 (at last): toString method"); System.out.println("T2 = 212.1 degrees F."); System.out.println(t2); System.out.println(); } }Last edited by psx2514; 10-30-2012 at 01:51 PM. Reason: Edited program since OP.
- 10-30-2012, 10:44 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Will someone please tell me what I'm doing wrong with this?
The first error suggests your writeC() method is wrongly implemented. We can guess, but it's a really good idea to document methods with a description of what they do. In this case I imagine it's supposed to write the temperature in degrees C, but one of the if blocks has it printing the instance's type rather than 'C'.
[Edit] comment about equals() removed after reading what Toll had to say... (and actually *reading* what the original code was!)Last edited by pbrockway2; 10-30-2012 at 10:51 AM.
- 10-30-2012, 10:44 AM #3
Re: Will someone please tell me what I'm doing wrong with this?
Your equals does look rather odd. Let's go through it.
My suggestion: First check if the types match. If they do, just compare the degrees and return the results. If they don't match, convert one to match the other and compare the two degrees then.Java Code:public boolean equals(Temperature t) { double TempC = (degrees - 32) * 5/9; // Shouldn't this only be done when the temperature is in Fahrenheit? double TempF = (9/5) * degrees + 32; // As above, shouldn't this only be done when the temperature is in Celsius? if (TempC == degrees) { // This will almost never be true, as you declare TempC to be calculated from degrees. return true; } else { return (this.degrees == t.degrees && this.type == t.type); // This doesn't do any kind of conversion. } }
- 10-30-2012, 10:54 AM #4
Re: Will someone please tell me what I'm doing wrong with this?
You're continuing to ignore paragraph 3 of the Forum Rules which I pointed you to in Need homework help
Kindly edit your post (click Go Advanced) and change the subject line to conform to the rules.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-30-2012, 11:57 AM #5
Member
- Join Date
- Oct 2012
- Posts
- 50
- Rep Power
- 0
Re: Will someone please tell me what I'm doing wrong with this?
I edited my OP to account for the fact that I fixed everything in my program except for what's wrong with my equals() method (which I still can't figure out). Please take a look.
- 10-30-2012, 12:05 PM #6
Re: Will someone please tell me what I'm doing wrong with this?
Throw some printlns in there and see what it does?
- 10-30-2012, 12:07 PM #7
Member
- Join Date
- Oct 2012
- Posts
- 50
- Rep Power
- 0
- 10-30-2012, 12:12 PM #8
Re: Will someone please tell me what I'm doing wrong with this?
Right.
- 10-30-2012, 12:15 PM #9
Member
- Join Date
- Oct 2012
- Posts
- 50
- Rep Power
- 0
Re: Will someone please tell me what I'm doing wrong with this?
I don't understand. Aren't I supposed to return a boolean?
- 10-30-2012, 12:24 PM #10
Re: Will someone please tell me what I'm doing wrong with this?
Outputting text does not equal returning it. It's extremely helpful in trying to figure out what's wrong; for instance, to make sure the variables are what you think they should be. It will make your output a bit messy while you try to find your error, but it will make it far easier for you.
- 10-30-2012, 12:35 PM #11
Member
- Join Date
- Oct 2012
- Posts
- 50
- Rep Power
- 0
- 10-30-2012, 12:36 PM #12
Re: Will someone please tell me what I'm doing wrong with this?
Why won't it work?
- 10-30-2012, 12:43 PM #13
Member
- Join Date
- Oct 2012
- Posts
- 50
- Rep Power
- 0
Re: Will someone please tell me what I'm doing wrong with this?
- 10-30-2012, 12:49 PM #14
Re: Will someone please tell me what I'm doing wrong with this?
So I take it you added your printlns but it didn't print anything? Because that would mean that part of the code isn't run, and would be where your problem is. Look at the code where you call your method, and then look at how your method is declared, and see if you can spot the problem.
And yes, I have figured out what the problem is. What I'm trying to do now is help you figure it out too by showing you how to do some basic bughunting.
- 10-30-2012, 12:53 PM #15
Member
- Join Date
- Oct 2012
- Posts
- 50
- Rep Power
- 0
- 10-30-2012, 12:55 PM #16
Re: Will someone please tell me what I'm doing wrong with this?
Of course, but I fail to see what that has to do with anything. You should be able to use println in there without any problems whatsoever.
- 10-30-2012, 01:00 PM #17
Member
- Join Date
- Oct 2012
- Posts
- 50
- Rep Power
- 0
Re: Will someone please tell me what I'm doing wrong with this?
Could you please be more straightforward? Why don't you tell me WHAT the problem is, so I can fix it? I'm not asking you to fix it for me.
- 10-30-2012, 01:02 PM #18
Re: Will someone please tell me what I'm doing wrong with this?
Okay then. Let's look at one place where you call your code:
Now let's look at how you declare your method:Java Code:System.out.println(t1.equals(t2));
See the problem?Java Code:public boolean equals()
- 10-30-2012, 01:23 PM #19
Member
- Join Date
- Oct 2012
- Posts
- 50
- Rep Power
- 0
- 10-30-2012, 01:26 PM #20
Re: Will someone please tell me what I'm doing wrong with this?
Have a look at the Java Method tutorial.
Similar Threads
-
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 4Last Post: 06-11-2013, 01:37 AM -
I can't find anything wrong with this but somehow it's wrong.
By Biscuit Tickler in forum New To JavaReplies: 2Last Post: 09-12-2012, 09:28 PM -
What am I doing wrong?!
By WidmarkRob in forum New To JavaReplies: 4Last Post: 04-20-2012, 05:19 AM -
What Am I doing Wrong?
By siren1111 in forum New To JavaReplies: 3Last Post: 12-20-2011, 02:01 AM -
what am i doing wrong here?
By GPB in forum New To JavaReplies: 3Last Post: 03-21-2010, 04:04 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks