Results 1 to 7 of 7
  1. #1
    nihirus's Avatar
    nihirus is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default About the method which return an object

    Hey all <3

    I was working randomly and trying stupid things to learn something. And I have a little problem. Firstly, here is my code.

    Java Code:
    public class Rasyonel {
    	private int pay, payda;
    	
    	public Rasyonel()
    	{
    		pay = 0;
    		payda = 1;
    		
    	}
    	
    	public Rasyonel (int a, int b)
    	{
    		pay = a;
    		payda = b;
    		
    	}
    	
    	
    	public Rasyonel addition(Rasyonel s1, Rasyonel s2)
    	{
    		Rasyonel s3 = new Rasyonel();
    		s3.pay = s1.payda*s2.pay + s1.pay*s2.payda;
    		s3.payda = s1.payda*s2.payda;
    		return s3;
    		
    	}
    	
    	public void write(){
    		
    		System.out.println(pay +"/" + payda);
    		
    	}
    }
    Here is the main:
    Java Code:
    public class demo {
    	public static void main (String[]args){
    		
    		Rasyonel s1 = new Rasyonel (2, 5);
    		Rasyonel s2 = new Rasyonel (3 ,7);
    		Rasyonel s3 = new Rasyonel (9 , 2);
    		
    		
    	}
    }
    This code aims to calculate summation of rational numbers.(Rasyonel = rational ,pay = numerator, payda = denominator. ) The problem is, I don't know which way to use sending objects to addition method. For instance, I tried this in the main:

    Java Code:
    Rasyonel s4 = new Rasyonel();
    		s4.addition(s1, s3);
    		s4.write();
    This gives me 0/1 which is completely wrong answer. I have no idea how to print the value of addition method to the screen. Any help would be appeciated. Thanks.
    Last edited by JosAH; 02-19-2012 at 07:02 PM. Reason: change [quote] tags to [code] tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default Re: About the method which return an object

    way to use sending objects to addition method
    s4.addition(s1, s3);
    That would call the addition method passing it two objects. Is that what you mean by "sending objects"?
    how to print the value of addition method to the screen.
    Somewhere you would execute a System.out.println() statement with the values you want shown on the screen.
    There are several ways to do that.
    One is to add a print() method to the Rasyonel. You currently have a method called write() that does that.
    Another would be to add a toString() method to the Rasyonel class which would return a String representing the contents of the class.

    This gives me 0/1 which is completely wrong answer
    Try debugging your code by adding printlns to show the values of the variables as they are changed by your code.
    Print out all the variables used so you can see what the program is doing.

  3. #3
    nihirus's Avatar
    nihirus is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: About the method which return an object

    Firstly, thanks for the answer. My problem is to reach s3 value in the addition method. What I mean is, after sending s1 and s3 objects to addition, I am expecting to take 49/10 as a result. Because s1's value is 2/5, s3's value is 9/2,(main) so other local s3 object in the addition method has to be 49/10. How can I reach that value?

    Java Code:
     Rasyonel s4 = new Rasyonel();
            s4.addition(s1, s3);
            s4.write();
    This one gives me 0/1, because I defined a empty constuctor and whenever calling s4(or whatever you named), it gives me default(0/1) instead of 49/10.

    By the way, I tried to explain as much as I can. My english is not very good and if you have any problem about my writing, feel free to ask.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default Re: About the method which return an object

    My problem is to reach s3 value in the addition method.
    When you pass s3 to the method, the method can call any method in object referenced by s3.

    If you have logic problems in the addition method, add some println statements to print the values of the variables.

    Otherwise you need to post a simple complete program that compiles, executes and shows the problem.
    The program should have everything in it and not require user input.


    A problem I see is the method's returned value is not used.

  5. #5
    nihirus's Avatar
    nihirus is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: About the method which return an object

    I partly solved it. I realized that local s3 object is unnecessary and I did this:

    Java Code:
    public static Rasyonel addition(Rasyonel s1, Rasyonel s2)
    	{
    		
    		s1.pay = s1.payda*s2.pay + s1.pay*s2.payda;
    		s1.payda = s1.payda*s2.payda;
    		
    		return s1;
    		
    	}
    and main:
    Java Code:
    public class demo {
    	public static void main (String[]args){
    		
    		Rasyonel s1 = new Rasyonel (2, 5);
    		Rasyonel s2 = new Rasyonel (3 ,7);
    		Rasyonel s3 = new Rasyonel (9 , 2);
    		
    		s1.addition(s1, s3);
    		s1.write();
    		
    	}
    }
    It gives me 49/10. finally. So, variables are normal, nothing wrong. But, this time, s1's value changes to 49/10 and I am still wondering how to do that with using local s object and without changing s1,s2 and s3 values. Here is the all code again:
    Java Code:
    public class Rasyonel {
    	private int pay, payda;
    	
    	
    	
    	
    	public Rasyonel()
    	{
    		pay = 0;
    		payda = 1;
    		
    	}
    	
    	public Rasyonel (int a, int b)
    	{
    		pay = a;
    		payda = b;
    		
    	}
    	public void üzerineEkle(Rasyonel s)
    	{
    		
    		pay = pay*s.payda +payda*s.pay;
    		payda =payda * s.payda;
    		 
    		
    	}
    	
    	public Rasyonel addition(Rasyonel s1, Rasyonel s2)
    	{
    		Rasyonel s3 = new Rasyonel();
    		s3.pay = s1.payda*s2.pay + s1.pay*s2.payda;
    		s3.payda = s1.payda*s2.payda;
    		
    		return s3;
    		
    	}
    	
    	public void write(){
    		
    		System.out.println(pay +"/" + payda);
    		
    	}
    		
    			
    }
    Java Code:
    public class demo {
    	public static void main (String[]args){
    		
    		Rasyonel s1 = new Rasyonel (2, 5);
    		Rasyonel s2 = new Rasyonel (3 ,7);
    		Rasyonel s3 = new Rasyonel (9 , 2);
    		
    		Rasyonel s4 = new Rasyonel();
    		s4.addition(s1, s3);
    		s4.write();
    			
    	}
    }

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default Re: About the method which return an object

    What do you do with the value returned by the addition() method?

    The addition method has 3 Rasyonel class object's values available to it, the current object (this) plus the two args passed to it.
    It acts like a static method because it does not use the values of its class instance.
    Perhaps you should look at the design of the method. It doesn't make sense the way you have coded it.

    This version of the method does an unexpected thing. It changes the values of one of its args:
    Java Code:
      public static Rasyonel addition(Rasyonel s1, Rasyonel s2)     {
             s1.pay = s1.payda*s2.pay + s1.pay*s2.payda;  // ??? Changes contents of s1???
    This type of coding will be a problem!!!
    Last edited by Norm; 02-19-2012 at 11:05 PM.

  7. #7
    nihirus's Avatar
    nihirus is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: About the method which return an object

    Norm, thanks for your helping. I guess I solved it. I add that code to main:

    Java Code:
    Rasyonel s4 = s1.addition(s1, s3);
    		s4.write();
    or (if addition is a static method)

    Java Code:
    Rasyonel s4 = Rasyonel.addition(s1,s3); 
    s4.write();
    and it gives me 49/10 plus without changing objects first value. <3

Similar Threads

  1. Return custom object in Java Webservice
    By Nela in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-23-2012, 04:58 PM
  2. Consuming web service with return custom object
    By dream_ in forum Networking
    Replies: 1
    Last Post: 04-16-2009, 06:56 AM
  3. Return an object to use in another method
    By TidusSolan in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 08:00 PM
  4. How I can return an object?
    By CFW in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 10:54 AM
  5. how to return an object from an arraylist
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 06:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •