How would I return all the objects, not just K? I suppose I could store the values inside a doSomeMath object with a public modifier, then get the values from the object one by one from the outside; it just seems there must be a more substantial way of doing the whole process.
First off, let's make sure that we understand the difference between a primative like an int and an Object. Objects must be insantiated with new, either directly or from some factory method. Primatives like int, long, double, etc. are just declared. Now for your problem. You could create a class and instantiate an object and return that as you mentioned and many times that is indeed a desireable solution. In the example you gave it would probably be much simpler to return an array of ints:
You could also use a Collection and wrap the ints in an object:
Michael Morris
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher