Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting an Average

 
Kirstie Fran
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a project to do that's getting a little bit complicated for me. I have a starting point and have to work from there to make all of the elements work properly. This is the jist of it:




I've done a few of the required things, but am still getting all kinds of errors, in the ""'s.
 
Edwin Torres
Ranch Hand
Posts: 55
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sum1 and sum2 are of type double. You cannot assign an Average() object to it. As the error says, they are not compatible.


sum2 is a double (primitive type). It is not an object, so it doesn't make sense to invoke a method on it.


Same thing here. You can't treat sum1 and sum2 like objects. They are primitive data types.
 
Kirstie Fran
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see my problem now. Thanks
the code works, well sort of.
When it runs i get values like:
"Sum2 is greater
Final averages: gettinganestimate.Average@5f893efe, gettinganestimate.Average@2b86c6b2"
for"

I've yet to understand why sum1 and sum2 aren't coming out right.
"getanestimate" is the name of the program.
 
Edwin Torres
Ranch Hand
Posts: 55
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is hard to figure out without seeing the changes you made. Did you change the types of sum1 and sum2? If sum1 and sum2 are Average objects, you can't simply print them like that. Again, I'm just guessing here because I can't see your code.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming you have made sum1 and sum2 Average objects - e.g. something like this:(incidentally, having a variable called "sum" actually meaning "average" is very misleading - you should try and have variable names that match their actual meaning)

Then what you see is the default behaviour when you try and convert an object to a String (which is what happens when you print it out like that). If you're trying to print out the averages you've got two options.

1. Use the getAverage() method to get the actual value to print:
2. Override the toString() method to use the value you want. This is the method that is called whenever you convert an object to a String, and you can define the behaviour you want.

One other thing that springs to mind, that you may have already changed. I would expect that your getAverage() method would return a double, not an int - unless you want the average of 1 and 2 to be 1 instead of 1.5.
 
Kirstie Fran
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just changed the part that was giving me the previous error. I created an "Average" method and clarified the issue with sum1 and sum2.
This issue with the output has been here for a while. what i posted is basically my code. sum1 and sum2 are both doubles.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure? Because "gettinganestimate.Average@5f893efe" is exactly what you'll get trying to print an Average object. Maybe you need to post the actual code again if you're still not happy with it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic