File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Getting an Average Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting an Average" Watch "Getting an Average" New topic
Author

Getting an Average

Kirstie Fran
Ranch Hand

Joined: Feb 16, 2011
Posts: 33

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

Joined: Mar 19, 2011
Posts: 55


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.

Twitter: @realEdwinTorres
Blog: java Friendly
Kirstie Fran
Ranch Hand

Joined: Feb 16, 2011
Posts: 33

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

Joined: Mar 19, 2011
Posts: 55

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

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Feb 16, 2011
Posts: 33

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

Joined: Apr 06, 2010
Posts: 4490
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting an Average