Originally Posted by
Bill123
...
What I am trying to do is take x, and return the square of x.
But your square() method doesn't use its argument for anything, so I'm not sure what you are trying to do.
It looks like you want to return an object whose value of x is the square of the value of x for the current object.
Why not something like:
public class Addition
{
.
.
.
public Addition square()
{
// New object created with this object's x squared
Addition result = new Addition(x*x);
return result;
}
.
.
.
}
Then maybe
public class Whatever
{
public static void main(String [] args)
{
Addition addObj = new Addition(12);
System.out.print("Originally addObj: ");
System.out.println(addObj);
// A new object that contains x*x of the original object
Addition addObj2 = addObj.square();
System.out.print("addObj2: ");
System.out.println(addObj2);
// Can even replace the original object with one that has x*x:
addObj = addObj.square();
System.out.print("After square(): addObj: ");
System.out.println(addObj);
}
}
Output:
Originally addObj: x = 12
addObj2: x = 144
After square(): addObj: x = 144
On the other hand, if you made the square() method look like this:
public void square(Addition z)
{
z.x = x*x;
}
You would set the value of x in the argument's object to the square of the value of x in the current object.
You could call it like:
Addition addObj = new Addition(12);
System.out.print("Originally addObj: ");
System.out.println(addObj);
Addition addObj2 = new Addition();
System.out.print("Originally addObj2: ");
System.out.println(addObj2);
addObj.square(addObj2);
System.out.print("After addObj.square(addObj2): addObj2: ");
System.out.println(addObj2);
Output might look like
Originally addObj: x = 12
Originally addObj2: x = 0
After addObj.square(addObj2): addObj2: x = 144
If that's not what you had in mind, then maybe tell us a little more about how you intend to use it. (Show some code that calls the method, and tell us what you would like to see as a result.)
Cheers!
Z