Results 1 to 12 of 12

Thread: How can I return multiple values from a Java function?

  1. #1

    Thread Starter
    Fanatic Member sbasak's Avatar
    Join Date
    Aug 2001
    Location
    Globe Trotter
    Posts
    524

    How can I return multiple values from a Java function?

    How can I return multiple values from a Java function?

    In C we can use pointer to return multiple value from a function. But how do I achive the same in Java?

    For example, how do write the following C code in Java?

    prototype -> int solve_quad(int p, int q, int r, float *q1, float *q2)

    while calling -> status = solve_quad(a,b,c,&root1,&root2);

    Thanx
    Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
    Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    All objects are passed byref in Java. So instead of using basic types like float or long, use their wrapper classes...
    Code:
    int solve_quad(int p, int q, int r, Float q1, Float q2)
    When you change the value inside the function, the change is reflected in the calling method.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Fanatic Member sbasak's Avatar
    Join Date
    Aug 2001
    Location
    Globe Trotter
    Posts
    524
    Thanx but what do these wrapper classes do actually?
    Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
    Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Since everything in Java is supposed to be object-oriented, they provide a way to make the basic types into Objects. Mostly, I use them to be able to put numbers into Collections, which only take in Objects.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Thread Starter
    Fanatic Member sbasak's Avatar
    Join Date
    Aug 2001
    Location
    Globe Trotter
    Posts
    524
    Hi, I tried to return a value by reference but it didn't work. Can you please help?

    [java]
    import java.lang.*;

    public class Test
    {
    public static void main(String [] args)
    {
    float x=5,y=0;
    SquareIt(x,y);
    System.out.println("Square of 5 is " +y);
    }

    void SquareIt(float a, Float b)
    {
    b=a*a;
    }
    }
    [/java]
    Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
    Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Hmmm, it seems that with the Number classes, all you can do is reinitialize the class...
    Code:
    public class Test 
    { 
        public static void main(String [] args) 
        { 
            float x=5,y=0; 
            SquareIt(x,y); 
            System.out.println("Square of 5 is " +y); 
        } 
    
        void SquareIt(float a, Float b) 
        { 
            b = new Float(a*a); 
        } 
    }
    Not the best solution, but it should work.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7

    Thread Starter
    Fanatic Member sbasak's Avatar
    Join Date
    Aug 2001
    Location
    Globe Trotter
    Posts
    524
    Gives error as follows

    Test.java:8: Incompatible type for method. Can't convert float to java.lang.Float.
    SquareIt(x,y);
    ^
    Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
    Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.

  8. #8
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Code:
    public class Test 
    { 
        public static void main(String [] args) 
        { 
            float x=5;
            Float y = new Float(0); 
            SquareIt(x,y); 
            System.out.println("Square of 5 is " +y.floatValue()); 
        } 
    
        void SquareIt(float a, Float b) 
        { 
            b = new Float(a*a); 
        } 
    }
    Try that. I don't have docs at my disposal right now, but that should work.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  9. #9

    Thread Starter
    Fanatic Member sbasak's Avatar
    Join Date
    Aug 2001
    Location
    Globe Trotter
    Posts
    524
    sorry again, your earlier version didn't compile giving error
    can't make static reference to method void SquareIt

    Anyway, I tried to make the above program as an applet with following code

    [javacode]
    import java.awt.*;
    import java.applet.*;
    import java.text.*;
    import java.lang.*;

    public class Test extends Applet
    {
    public void init()
    {
    resize(200,200);
    }

    public void paint(Graphics g)
    {
    float x=5;
    Float y = new Float(0.0);
    SquareIt(x,y);
    g.drawString("Square of 5 is " +y.floatValue(),10,10);
    }

    void SquareIt(float a, Float b)
    {
    b = new Float(a*a);
    }
    }


    [/javacode]

    Now evething is fine except the answer shows
    Square of 5 is 0.0 instead of 25... !!!!
    That means the value wasn't passed by reference??
    Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
    Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.

  10. #10
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    If you can wait a while, I don't have access to anything Java here at work, but when I get home I'll make it work correctly.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  11. #11
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    It seems that it will not work the way I thought (its been a while since I've done Java). If you could change the value of the Float object, then it would work, but it seems that the Number classes are immutable.

    Can you give an actual instance of when you would want to do this, since your SquareIt example doesn't really qualify.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  12. #12

    Thread Starter
    Fanatic Member sbasak's Avatar
    Join Date
    Aug 2001
    Location
    Globe Trotter
    Posts
    524
    Take the original function of returning two roots of a quadratic equation

    int solve_quad(int p, int q, int r, float *q1, float *q2)

    I myself wrote a solution, but persoanlly I feel there should be a better alternative.

    Anyway, I attach my version of the applet.

    -----------------------------------------------------

    // a quadratic equation solver
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.applet.*;
    /* <applet code= "QuadSolverApp" width=160 height=250>
    </applet>
    */
    public class QuadSolverApp extends JApplet implements ActionListener
    {
    JTextField jtfa,jtfb,jtfc,jtfr1,jtfr2;
    JLabel jlt,jla,jlb,jlc,jlr1,jlr2;
    JButton jb;
    public void init()
    {
    resize(160,250);
    Container contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    // add text fields & lables
    jlt = new JLabel("Quadratic Equation Solver");
    contentPane.add(jlt);
    jla = new JLabel("Enter a");
    contentPane.add(jla);
    jtfa = new JTextField(8);
    contentPane.add(jtfa);
    jlb = new JLabel("Enter b");
    contentPane.add(jlb);
    jtfb = new JTextField(8);
    contentPane.add(jtfb);
    jlc = new JLabel("Enter c");
    contentPane.add(jlc);
    jtfc = new JTextField(8);
    contentPane.add(jtfc);
    jlr1 = new JLabel("Root 1");
    contentPane.add(jlr1);
    jtfr1 = new JTextField(8);
    contentPane.add(jtfr1);
    jlr2 = new JLabel("Root 2");
    contentPane.add(jlr2);
    jtfr2 = new JTextField(8);
    contentPane.add(jtfr2);
    // add button
    JButton jb = new JButton("Solve"); // button label
    jb.setActionCommand("SolveEquation"); // button action command
    jb.addActionListener(this);
    contentPane.add(jb);
    }
    public void actionPerformed(ActionEvent ae)
    {
    if(ae.getActionCommand()=="SolveEquation") // button action command
    {
    // create a new instance of SolveClass
    SolveClass sc = new SolveClass();
    SolveClass sc2;
    // call Solve method of SolveClass
    sc2 =
    sc.Solve(Float.parseFloat(jtfa.getText()),Float.parseFloat(jtfb.getText()),Float.parseFloat(jtfc.get Text()));
    if(sc2.root1==0)
    {
    jtfr1.setText("No real root");
    jtfr2.setText("No real root");
    }
    else
    {
    jtfr1.setText(String.valueOf(sc2.root1));
    jtfr2.setText(String.valueOf(sc2.root2));
    }
    }
    }
    }
    class SolveClass
    {
    static double root1,root2;
    SolveClass Solve(double a, double b, double c)
    {
    double determinant;
    SolveClass scf = new SolveClass();
    determinant = b*b-4*a*c;
    if(determinant > 0)
    {
    System.out.println("Two real roots");
    scf.root1 = (-b + Math.sqrt(determinant))/(2*a);
    scf.root2 = (-b - Math.sqrt(determinant))/(2*a);
    }
    else
    {
    scf.root1 = 0;
    scf.root2 = 0;
    System.out.println("No real root");
    }
    return scf;
    } // end of method Solve
    } // end of class SolveClass
    -------------------------------------------------------------
    Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
    Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.

Posting Permissions

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



Click Here to Expand Forum to Full Width