Very Basic Question: Reference Data Types
I have this code:
Code :
public class Addition {
private int x;
private int y;
public Addition(){
x = 0;
}
public Addition(int x) {
this.x = x;
}
public void setX(int value){
x = value;
}
public Addition square(Addition z){
Addition result = new Addition();
result.x = x * x;
return result;
}
public String toString(){
return String.format("x = " + x);
}
}
What I am trying to do is take x, and return the square of x. I want to be able to instantiate this object in another class. In that same class, I will set the value for x. I have all of that working, but what I can't do is return the square of x Instead, it just returns the value of x. Sorry if my terminology is lacking, I am just a beginner. I know that an easy way to do this is to do: return String.format("x = " + x*x); instead of what I have. However, this is not the solution I'm looking for. I want it to be done in the square method.
This is a template so I can't change anything too drastically. Hopefully someone can tell me where I am going wrong! :)
Re: Very Basic Question: Reference Data Types
Quote:
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:
Code java:
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
Code java:
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:
Code :
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:
Code java:
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:
Code java:
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
Code :
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