Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Very Basic Question: Reference Data Types

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Very Basic Question: Reference Data Types

    I have this 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!
    Last edited by Bill123; July 14th, 2012 at 08:58 PM.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Very Basic Question: Reference Data Types

    Quote Originally Posted by Bill123 View Post
    ...
    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
    Last edited by Zaphod_b; July 14th, 2012 at 11:07 PM.

Similar Threads

  1. Tutorial: Primitive Data Types
    By newbie in forum Java Programming Tutorials
    Replies: 3
    Last Post: July 5th, 2012, 11:56 PM
  2. What are the 3 types of self-reference in JAVA
    By wholegrain in forum Java Theory & Questions
    Replies: 1
    Last Post: March 5th, 2012, 05:49 PM
  3. [SOLVED] Converting primitive data types causing NumberFormatException
    By Melawe in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 10th, 2011, 12:30 AM
  4. Creating New File types question.
    By GodspeedPR in forum Java Theory & Questions
    Replies: 1
    Last Post: July 1st, 2011, 09:05 AM
  5. Replies: 4
    Last Post: September 5th, 2010, 10:29 AM