0

In this simple program i cant return 2 integer values , can you help me ? How can i do ?

public class Aritmetica 
{

public static int div(int x , int y)

  { 

    int q = 0 ;
    int r = x ; 
    while ( r >= y ) 
    {
      r = r - y ;  
      q = q + 1 ;  

    }
    return r && q; **// Here i want to return x and y**
 }

public static void main(String[ ] args)
 {

 if ( ( x <=0 ) & ( y > 0 ) )

  throw new IllegalArgumentException ( " X & Y must be >0  " ) ;

  int res4= div(x,y);

  System.out.println( " q and r : "+ res4) ; **// and here i want to display q and r** 

}

}

4 Answers 4

5

Create a result type: DivisionResult, as follows:

class DivisionResult {
    public final int quotient;
    public final int remaineder;
    public DivisionResult(int quotient, int remainder) {
        this.quotient = quotient;
        this.remainder = remainder;
    }
}

and do

    ...
    return new DivisionResult(q, r);
}

And to print the result:

  DivisionResult res4= div(x,y);

  System.out.println("q and r: " + res4.quotient + ", " + res4.remainder);
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of returning two integers at once, you can write separate methods for each operation. For example:

public static int div1(int x , int y) { 

// i replaced r with x for readibility
   while ( x >= y ) {
       x = x - y ;  
   }
   return x; // this is your variable r
}

public static int div2(int x, int y) {
    int q = 0;
    int r = x;
    while ( r >= y ) {
        r = r - y ;  // r is required here because it is your update statement in while loop
        q = q + 1 ;  
    }
    return q;
}

In your main method, all you have to is call each method (div1 and div2 to get variables r and q, respectively). You can then print them with a statement like:

System.out.println( " q and r : "+ div2(x,y) + " and " + div1(x,y)) ;

I hope this is simple enough to understand. Good luck!

1 Comment

Thank you very much ! It works ! And it is very simple , because i started to study java for 2 weeks, and im quite noob ;)
0

Use array of integers to return more than one integers.

Like :

public int[] method() {  
    int[] a = {1,2,3,4,5};  
    return a;   
}  

4 Comments

At school we havent't studied arrays yet , so i cant fo that
I don't think this is good practice. result[1] simply isn't as readable as result.remainder.
@aioobe What about that result[REMAINDER]
@VikasVerma, almost worse. REMAINDER could be confused with the variable containing the actual remainder. REMAINDER_INDEX is slighly better, but still a lot worse than creating a DivisionResult class and do result.remainder.
0

Assume q and r are less than p(can be any integer greater than q and r)

Now Do it like this

return result=q*p+r

Now, in main when you want to print the result

print q=result/p
r=result-p*q

4 Comments

It's probably not the best practice but it's kinda fun :)
i have only to use + and - for doing the division , but ty aniway , seem it works ;)
Ya.. It's the usage of modulo arithmetic. Why can't you use multiplication and division. Is it a puzzle or programming exercise. I am sure they would like if you give different solutions
Because i have to write some while programs using Peano's Axioms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.