I don't understand why my Program need 0 second to run if there're these 2 lines in "private int ggT(int a, int b)" >>> if (a < 0) a *= -1; if (b < 0 ) b *= -1;
If I delete these 2 lines it needs more than 30 seconds to run. I just want to understand what these 2 lines do, because I did'nt write the code. And I think these 2 lines are superfluous.
Btw I'm coding in Netbeans with java 8.0 I guess.
Here is the whole program:
public class Bruch {
private int zaehler;
private int nenner;
public Bruch(int zaehler, int nenner){
this.zaehler = zaehler;
this.nenner = nenner;
if (zaehler < 0 & nenner < 0) {
this.nenner *= -1;
this.zaehler *= -1;
}
kuerzeDich();
}
private int min(int a, int b) {
if (a < b) return a;
return b;
}
private int ggT(int a, int b) {
if (a < 0) a *= -1;
if (b <0 ) b *= -1;
int ggTeiler = min(a, b);
while (a%ggTeiler != 0 || b%ggTeiler != 0) {
ggTeiler--;
}
return ggTeiler;
}
private void kuerzeDich() {
int ggTeiler = ggT(zaehler, nenner);
zaehler = zaehler / ggTeiler;
nenner = nenner / ggTeiler;
}
@Override
public String toString() {
return String.valueOf(zaehler "/" nenner);
}
public Bruch kehrwert() {
return new Bruch(nenner, zaehler);
}
public Bruch add(Bruch pOther) {
int ggTeiler = ggT(nenner, pOther.nenner);
int kgV = nenner * pOther.nenner / ggTeiler;
int neuerZaehler = zaehler * (kgV/nenner) pOther.zaehler * (kgV/pOther.nenner);
return new Bruch(neuerZaehler, kgV);
}
public Bruch sub(Bruch pOther) {
int ggTeiler = ggT(nenner, pOther.nenner);
int kgV = nenner * pOther.nenner / ggTeiler;
int neuerZaehler = zaehler * (kgV/nenner) - pOther.zaehler * (kgV/pOther.nenner);
return new Bruch(neuerZaehler, kgV);
}
public Bruch mul(Bruch pOther) {
int neuerzaehler = zaehler * pOther.zaehler;
int neuernenner = nenner * pOther.nenner;
return new Bruch(neuerzaehler, neuernenner);
}
public Bruch div(Bruch pOther) {
return new Bruch(zaehler,nenner).mul(pOther.kehrwert());
}
public static void main(String[] args) {
Bruch b1 = new Bruch(1, 6);
Bruch b2 = new Bruch(3, 4);
System.out.println(b1.add(b2));
System.out.println(b1.sub(b2));
System.out.println(b1.mul(b2));
System.out.println(b1.div(b2));
}
}
CodePudding user response:
My assumption is when performing mathematic operations on them, it goes below 0
which are then assigned to back to zaehler
nenner
.
The reason it takes more time is because of this code(modulus)
int ggTeiler = min(a, b);
while (a%ggTeiler != 0 || b%ggTeiler != 0) {
ggTeiler--;
}
x % y
always equals x % -y
You can think of the sign of the second operand as being ignored.
x % 5
(which is the same as x % -5
).
-5 % 11 == -5
5 % -11 == 5
-5 % -11 == -5
Suppose one of them is -11
and 5
.
-11
is smaller than 5
which is then assigned to ggTeiler
.
Now, 5 % -11 = 5
which is != 0 and the loop continues.
ggTeiler--
-> -11-1=-12.
5 % -12 = 5
which remains 5 all the time and the loop continues.
until int
it goes from minimun to maximum of int
. Refer this
So then from max postive int value, it'll come to a value where both 5 % ggTeiler != 0
and -11 % ggTeiler != 0
becomes false when ggTeiler = -1
.
Then the loop exits.