-1

Fraction switches numerator and denominator. inverse of 2/1 is 1/2

what i got so far:

public void inverse(){
    numerator = denominator;
    denominator= numerator;
}

this makes the inverse of 2/1 equal 2/2.

I know this is happens because num = den (so num=2) and den = num (so den=2).

How would i switch to get num=2 and den =1?

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
J Cole
  • 15
  • 4
  • Does that answer your question? – ItamarG3 Oct 14 '16 at 18:01
  • says incompatible types: possible lossy conversion from double to long. – J Cole Oct 14 '16 at 18:06
  • oh, right then. wait a moment EDIT: does it work now? – ItamarG3 Oct 14 '16 at 18:08
  • Isn't it just a `swapping` issue? – Saurav Sahu Oct 14 '16 at 18:09
  • Thanks. It works now. mind explaining how you got the answer? – J Cole Oct 14 '16 at 18:14
  • Possible duplicate of [Is it possible to swap two variables in Java?](http://stackoverflow.com/questions/10393690/is-it-possible-to-swap-two-variables-in-java) – Joe C Oct 14 '16 at 18:23
  • @JCole Gladly. the error means that I was trying to set the value for a variable of type long, a value of type double. they are different in a few ways, primarily long uses more bytes of memory. by changing the type of the temporary variable, the problem disappear. If the answer is good, you can accept it (tick under the score of the answer) – ItamarG3 Oct 14 '16 at 18:33
  • @JCole did you understand the explanation? – ItamarG3 Oct 14 '16 at 19:58

4 Answers4

4

To solve this, create a temporary variable:

public void inverse(){
    long temp = numerator;
    numerator = denominator;
    denominator = temp;
}
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
0

What you need to do is to swap the numerator and denominator but you are not using a temporal variable, so one of the operators will be lost..

public void inverse(){
    Number temporalNumber = numerator;
    numerator = denominator;
    denominator = temporalNumber;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
-1

Use temp variable:

public void inverse() {
  long x =  numerator; //or int because i don't know what you use
  numerator = denominator;
  denominator = x;
}

Or tricky way for ints:

public void inverse() {
  numerator ^= denominator;
  denominator ^= denominator;
  numerator ^= denominator;
}
Filipp Voronov
  • 4,077
  • 5
  • 25
  • 32
-1

as already the answers are given, here is just another way to swap numbers without temporary variable

    int numerator = 10;
    int denominator = 20;
    System.out.println("Before " + numerator + " " + denominator);

    numerator = numerator + denominator;
    denominator = numerator - denominator;
    numerator = numerator - denominator;
    System.out.println("After " + numerator + " " + denominator);

output

Before 10 20
After 20 10

method

public void inverse() {
    numerator = numerator + denominator;
    denominator = numerator - denominator;
    numerator = numerator - denominator;
}

with bit-wise operations

    numerator = numerator ^ denominator;
    denominator = numerator ^ denominator;
    numerator = numerator ^ denominator;
Saravana
  • 12,647
  • 2
  • 39
  • 57
  • @JCole `^` is called bit-wise ex-or operator, set log, print the a,b values after each statement and print them in binary, you could understand easily. – Saravana Oct 16 '16 at 04:30