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 4 of 4

Thread: Trying to return more than one value as object

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Trying to return more than one value as object

    I am pretty new to Java. I tried to look at a few examples and they made no sense to me. Here is what I am trying to do:
    Create a method that just handles the calculations for the application. Create another method that allows returning all the values from the
    method used to do the calculations. Have the main method only hold the initialized variables and print out the results. Here is what I tried
    (pure conjecture and I cannot figure out how to accomplish what I want):

    public class WorkingWithMults
    {
    	public static void main(String[] args)
    	{
    		int a = 1, b = 2;
    		System.out.print(obj);
    	}
     
    	public static void Calculations(int a, int b)
    	{
    		a = a + 5;
    		b = b + 5;
    	}
     
    	public Calculations returnValue()
    	{
    		Calculations obj = new Calculations();
    		return obj;
    	}
    }

    Thanks in advance!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to return more than one value as object

    If the returnValue method is to return the values computed by the calculate (not Calculations) method, it needs to include those in the object it returns. Your Calculations constructor should take those values as arguments.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Trying to return more than one value as object

    @mwr76: You must start learning from basics. It looks like as you directly started reading classes and that stuff.

  4. #4

    Default Re: Trying to return more than one value as object

    This is dangerous and not very OOPish but you can create an array of Objects and return that. The only danger will be not knowing what comes out on the other side. You will have to cast the data to use it.

    Or:
    Create a class Calculation that performs the calculation and gets returned.

    public class Calculation
    {
         private double binaryOperand1;
         private double binaryOperand2;
         private double result;
     
         public Calculation(double op1, double op2)
         {
              this.binaryOperand1 = op1;
              this.binaryOperand2 = op2;
              this.result = null;
         }
     
         public Calculation()
         {
              this(0,0);
         }
     
         //Getters and Setters elided...
     
         public Calculation addOperation()
         {
              result = binaryOperand1 + binaryOperand2;
              return this;
         }
     
         //... and so on 
    }
    Kenneth Walter
    Software Developer
    http://kennywalter.com

Similar Threads

  1. Calling from one javabean object to other javabean object in a single class
    By kichkich in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2011, 07:20 AM
  2. [SOLVED] How to get the return of a method inside an object in an ArrayList?
    By Hallowed in forum Java Theory & Questions
    Replies: 7
    Last Post: May 1st, 2011, 10:44 PM
  3. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  4. how to return value ..
    By gr8mind in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 11th, 2011, 05:27 AM
  5. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 02:33 PM