# Thread: Will someone please tell me what I'm doing wrong with this?

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();
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 02:51 PM. Reason: Edited program since OP.

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,715
Rep Power
16

## Re: Will someone please tell me what I'm doing wrong with this?

Originally Posted by psx2514
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?
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'.

Last edited by pbrockway2; 10-30-2012 at 11:51 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.
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.
}
}```
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.

4. ## Re: Will someone please tell me what I'm doing wrong with this?

Originally Posted by psx2514
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 http://www.java-forums.org/new-java/...work-help.html

Kindly edit your post (click Go Advanced) and change the subject line to conform to the rules.

db

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.

6. ## Re: Will someone please tell me what I'm doing wrong with this?

Throw some printlns in there and see what it does?

7. Member
Join Date
Oct 2012
Posts
50
Rep Power
0

## Re: Will someone please tell me what I'm doing wrong with this?

Originally Posted by Toll
Throw some printlns in there and see what it does?
Where? In my equals method?

Right.

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. ## 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.

11. Member
Join Date
Oct 2012
Posts
50
Rep Power
0

## Re: Will someone please tell me what I'm doing wrong with this?

Originally Posted by Toll
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.
That won't work.

Can anyone else help me.

12. ## Re: Will someone please tell me what I'm doing wrong with this?

Why won't it work?

13. Member
Join Date
Oct 2012
Posts
50
Rep Power
0

## Re: Will someone please tell me what I'm doing wrong with this?

Originally Posted by Toll
Why won't it work?
I don't know. Who cares? It just doesn't. I'm not worried about that, and I'm not going to waste my time trying to figure it out. Unless you figured out what the problem is, and can actually help me, please don't waste my time.

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.

15. Member
Join Date
Oct 2012
Posts
50
Rep Power
0

## Re: Will someone please tell me what I'm doing wrong with this?

Originally Posted by Toll
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.
You do know that the Temperature class isn't the driver class, right?

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.

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.

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:
Java Code:
`System.out.println(t1.equals(t2));`
Now let's look at how you declare your method:
Java Code:
`public boolean equals()`
See the problem?

19. Member
Join Date
Oct 2012
Posts
50
Rep Power
0

## Re: Will someone please tell me what I'm doing wrong with this?

Originally Posted by Toll
Okay then. Let's look at one place where you call your code:
Java Code:
`System.out.println(t1.equals(t2));`
Now let's look at how you declare your method:
Java Code:
`public boolean equals()`
See the problem?
Should it be
Java Code:
` public boolean equals(t1)`
?
Last edited by psx2514; 10-30-2012 at 02:25 PM.

20. ## Re: Will someone please tell me what I'm doing wrong with this?

Have a look at the Java Method tutorial.

Page 1 of 2 12 Last