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):
Code :
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!
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.
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.
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.
Code Java:
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
}