Why is my while loop being skipped? - java

I think my program is skipping my while loop, but I'm honestly not sure exactly what is happening. The function is supposed to reduce fractions by finding the GCD and then dividing numerator and denominator by that number.
class Rational {
private int numerator, denominator;
//Constructor
public Rational (int num, int den) {
numerator = num;
denominator = den;
}
//Method for multiplying fractions
public Rational times (Rational that) {
Rational x = new Rational (this.numerator*that.numerator, this.denominator*that.denominator);
x = x.reduce();
return x;
}
//Method for displaying fractions as strings
public String toString() {
return new String(numerator+"/"+denominator);
}
//Method for adding fractions
public Rational plus(Rational that) {
Rational x = new Rational ((this.numerator*that.denominator)+(that.numerator*this.denominator),
this.denominator*that.denominator);
//x = x.reduce();
return x;
}
//Method for subtracting fractions
public Rational minus(Rational that) {
Rational x = new Rational ((this.numerator*that.denominator)-(that.numerator*this.denominator),
this.denominator*that.denominator);
//x = x.reduce();
return x;
}
//Method for dividing fractions
public Rational divideBy(Rational that) {
Rational x = new Rational (this.numerator*that.denominator, this.denominator*that.numerator);
//x = x.reduce();
return x;
}
public Rational reduce() {
int a = Math.abs(this.numerator);
int b = Math.abs(this.denominator);
int c = Math.min(a, b);
System.out.println(c);
System.out.println(a%c);
System.out.println(b%c);
if (a==0) {
return new Rational (0,1);
}
else {
while (((a%c)!= 0) && ((b%c)!= 0)) {
c = c-1;
System.out.println(c);
}
System.out.println(c);
return new Rational (this.numerator/c,this.denominator/c);
}
}
}
public class RationalTester {
public static void main(String[] args) {
Rational x = new Rational (6,4); //The fraction 6/4
Rational y = new Rational (5,2); //The fraction 5/2
Rational z = x.times(y); //Their product
Rational w = x.plus(y); //Their sum
Rational v = x.minus(y); //Their difference
Rational u = x.divideBy(y); //Their quotient
JOptionPane.showMessageDialog(null, x.toString()+" * "+y.toString()+" = "+z.toString());
JOptionPane.showMessageDialog(null, x.toString()+" + "+y.toString()+" = "+w.toString());
JOptionPane.showMessageDialog(null, x.toString()+" - "+y.toString()+" = "+v.toString());
JOptionPane.showMessageDialog(null, x.toString()+" / "+y.toString()+" = "+u.toString());
}
}
I'm getting the absolute value of the numerator and denominator to ensure that if the fraction is negative I'll be keeping that at the end. If the numerator is 0, I was asked to return (0,1). The question is about the while loop... it seems that it's being skipped completely. Any suggestions?

Because always its condition is false.
In the first lines you set c equal to either a or b. So there are two possibilities:
If c == a, then a%c will be zero. So the while condition is false.
If c == b, then b%c will be zero. So the while condition is false.

Related

About rational number in java programming, to calculate (1/2+3/4+...+99/100)^2

The purpose of this code is to calculate (1/2+3/4+...+99/100)^2. But my loop can't be executed correctly.
The result of r1 is 3/4 instead of 99/100, what's wrong with my code?
I think my loop can be run because the y I can get it correctly.
So how can i correct my code and make it able to calculate (1/2+3/4+...+99/100)^2 ? Thank you for answering.
import java.math.BigInteger;
public class Rational {
// Data fields for numerator and denominator
private BigInteger numerator = BigInteger.ZERO;
private BigInteger denominator = BigInteger.ONE;
/** Construct a rational with default properties */
public Rational() {
this(BigInteger.ZERO, BigInteger.ONE);
}
/** Construct a rational with specified numerator and denominator */
public Rational(BigInteger numerator, BigInteger denominator) {
BigInteger gcd=new BigInteger(String.valueOf(gcd(numerator,
denominator)));
BigInteger r1=new
BigInteger(String.valueOf(denominator.compareTo(BigInteger.ZERO)));
this.numerator = (r1.multiply(numerator)).divide(gcd);
this.denominator = (denominator.abs()).divide(gcd);
}
/** Find GCD of two numbers */
private static long gcd(BigInteger n, BigInteger d) {
BigInteger n1 = n.abs();
BigInteger n2 = d.abs();
int gcd = 1;
for (int k = 1; (new BigInteger(String.valueOf(k))).compareTo(n1)<=0 &&
(new BigInteger(String.valueOf(k))).compareTo(n2)<=0; k++) {
if (n1.mod(new BigInteger(String.valueOf(k))).equals(BigInteger.ZERO) &&
n2.mod(new BigInteger(String.valueOf(k))).equals(BigInteger.ZERO))
gcd = k;
}
return gcd;
}
/** Return numerator */
public BigInteger getNumerator() {
return numerator;
}
/** Return denominator */
public BigInteger getDenominator() {
return denominator;
}
/** Add a rational number to this rational */
public Rational add(Rational secondRational) {
BigInteger n =
numerator.multiply(secondRational.getDenominator())
.add(denominator.multiply(sec
ondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
/** Subtract a rational number from this rational */
public Rational subtract(Rational secondRational) {
BigInteger n =
(numerator.multiply(secondRational.getDenominator()))
.subtract(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
/** Multiply a rational number to this rational */
public Rational multiply(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
/** Divide a rational number from this rational */
public Rational divide(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.numerator);
return new Rational(n, d);
}
/** Compute the square of this rational number*/
public Rational square() {
BigInteger n = numerator.multiply(numerator);
BigInteger d = denominator.multiply(denominator);
return new Rational(n, d);
}
/** toString */
public String toString() {
return numerator + "/" + denominator;
}
}
and this is the testRational class
import java.math.BigInteger;
public class TestRational {
public static void main(String[]args){
int y = 1;
BigInteger i=new BigInteger(String.valueOf(1));
BigInteger a=new BigInteger(String.valueOf(2));
BigInteger b=new BigInteger(String.valueOf(3));
BigInteger c=new BigInteger(String.valueOf(5));
Rational sum = new Rational(BigInteger.ZERO,a);
Rational r0 = new Rational(b,b.add(i));
Rational r2 = new Rational(a,c);
Rational r3 = new Rational(a,c);
Rational s1 = r3.multiply(r2);
Rational s2 = r3.square();
Rational s3 = r2.divide(r3);
Rational r1 = new Rational(i,a);
do{
sum = sum.add(r0);
b = b.add(a);
y++;
}while(y<49);
System.out.println(sum.multiply(sum));
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(r0);
}
}
The purpose of this code is to calculate (1/2+3/4+...+99/100)^2. But my loop can't be executed correctly.
The result of r1 is 3/4 instead of 99/100, what's wrong with my code?
I think my loop can be run because the y I can get it correctly.
So how can i correct my code and make it able to calculate (1/2+3/4+...+99/100)^2 ? Thank you for answering.
The purpose of this code is to calculate (1/2+3/4+...+99/100)^2. But my loop can't be executed correctly.
The result of r1 is 3/4 instead of 99/100, what's wrong with my code?
I think my loop can be run because the y I can get it correctly.
So how can i correct my code and make it able to calculate (1/2+3/4+...+99/100)^2 ? Thank you for answering.
Let's write it less confusingly, without all the unnecessary stuff and confusing loop. The definition of the sum is (1/2 + 3/4 ... 99/100) so let's start by creating all the fractions in the sum:
for (int i = 1; i <= 99; i += 2) {
BigRational t = new BigRational(BigInteger.valueOf(i), BigInteger.valueOf(i + 1));
}
They have to be summed, so there has to be a variable declared outside the loop to sum up all those fractions into:
Rational sum = new Rational();
for (int i = 1; i <= 99; i += 2) {
Rational t = new Rational(BigInteger.valueOf(i), BigInteger.valueOf(i + 1));
sum = sum.add(t);
}
Then square that and you have your answer. I get:
87593039510089573189394173247956745677798336081
-----------------------------------------------
38416307357189261992010230523038591203840000
Which I can't verify but it looks reasonable enough. The expected answer is "a bit less than 502" (because it's the square of 50 terms that are nearly 1, if 0.5 can be called that) and this is close enough.
By the way, stop using String.valueOf everywhere in Rational. Just work with numbers. And BigInteger already implements gcd, you don't have to write your own (less efficient) version. I had to replace this otherwise it took too long.
It's not clear what your sequence of numbers is, but I will go with the following assumption:
If your goal is the simply return the value of (1/2 + 3/4 + 5/6 + ... + 97/98 + 99/100) ^ 2. Then I would suggest the following:
//This method will return the value of (1/2 + 3/4 + 5/6 + ... + 97/98 + 99/100) ^ 2
public int calc(){
double denominator = 2;
double numerator = denominator - 1; //in your sequence, numerator is always 1 less than denominator
double sum = 0;
while(denominator <= 100){
sum = sum + (numerator / denominator); //shorthand sum += (numerator / denominator);
denominator = denominator + 2; //shorthand denominator += 2;
numerator = denominator - 1;
}
return sum * sum; //this is equivalent to sum ^ 2
}

While building an object for rational number

public class Rational {
int num, denom; /*I'm building an object named "Rational, which takes in two int values, num and denom, and represent them as a rational number(num/denom)*/
public Rational(int a, int b){//this is the constructor
this.num = a;
this.denom = b;
}
public Rational(){//this is just another form of constructor
this.num = 0;
this.denum = 0;
}
public static void printRational(Rational x){/*this is the method that prints the rational number in a fractional format*/
System.out.println(x.num+"/"+x.denom);
}
public static int gcd(int a, int b){/*this is the method which finds the greatest common denominator of numerator of denominator. This will be used to simplify the fraction*/
if(b == 0){
return a;
}
else{
return gcd(b, a%b);
}
}
public static Rational add(Rational x, Rational y){/*this is a method which adds the two rational numbers(or objects) together, then simplify it utilizing the gcd method*/
Rational z = new Rational();
z.denom = (x.denom * y.denom);
z.num = y.num*x.denom + x.num*y.denom;
z.denom = z.denom/gcd(z.num, z.denom);
z.num = z.num/gcd(z.num, z.denom);
return z;
}
public static void main(String[] args) {
Rational y = new Rational(1, 2); //1st Rational Object: y
Rational z = new Rational(2, 6); //2nd Rational Object: z
printRational(add(y, z)); //implementing the method
//result? so far so good. I get the correct result
}
}
The problem I'm facing is I don't understand is that why this won't work instead and give me an error.
public static Rational add(Rational x, Rational y){
Rational z = new Rational();
int a = (x.denom * y.denom);
int b = y.num*x.denom + x.num*y.denom;
z.denom = a/gcd(z.num, z.denom);
z.num = b/gcd(z.num, z.denom);
return z;
}
Why would this give me an error message: "Exception in thread "main" java.lang.ArithmeticException: / by zero
at Rational.add(Rational.java:47)
at Rational.main(Rational.java:58)" when the instances of the object are int types and I'm temporarility storing the value into int a, b; Help me please if you see something I don't see!! Thank you.
I do not know what is the exact problem, when you say "It is not working". It would have been helpful if you could tell what error is it giving. However, from what you have said, Could it be because you have not initialized z.num and z.denom and are sending them to gcd() ?? They will both be zero, as they are primitive data type "int" so your gcd method will return 0 and cause an exception.
public static Rational add(Rational x, Rational y){
Rational z = new Rational();
int a = (x.denom * y.denom);
int b = y.num*x.denom + x.num*y.denom;
z.denom = a/gcd(z.num, z.denom); <--------------
z.num = b/gcd(z.num, z.denom); <--------------
return z;
}
You are using the wrong implementation for dividing with gcd as the number changes in this case.
z.denom = z.denom/gcd(z.num, z.denom);
z.num = z.num/gcd(z.num, z.denom);//z.denom is changed here.
The z.denom can be 0 as it is changed so gcd is 0 and hence the exception.
So what you can do is.
int gcd = gcd(z.num, z.denom);
z.denom /= gcd;
z.num /= gcd;
Also this fails when either of num or denom is a 0.

Issue with compare two fractions

I have two classes: Fraction and Test. I already do well with class Fraction, but class Test has some issues.
I want to allow the user enter the fractions and store in ArrayList, the user can compare two fraction from the array by choosing the index of the array. But when I compare two fraction, it doesn't work well!
class Fraction:
class Fraction {
private int numerator;
private int denominator;
Fraction(int n, int d) {
numerator = n;
denominator = d;
}
public Fraction(int n) {
this(n, 1);
}
public Fraction() {
numerator = 0;
denominator = 1;
}
public int getNumerator() {
return numerator;
}
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public int getDenominator() {
return denominator;
}
public void setDenominator(int denominator) {
this.denominator = denominator;
}
public void display() {
String s = this.getNumerator() + "/" + this.getDenominator();
System.out.println(s);
}
public double evaluate() {
double n = numerator;
double d = denominator;
return (n / d);
}
public boolean isEquals(Fraction f){
int gcd1 = gcd(f.getNumerator(), f.getDenominator());
double fractionFloatValue = (f.getNumerator()/gcd1) / (f.getDenominator()/gcd1);
int gcd2 = gcd(this.getNumerator(), this.getDenominator());
double fractionFloatValue2 = (this.getNumerator()/gcd2) / (this.getDenominator()/gcd2);
return (fractionFloatValue == fractionFloatValue2) ? true : false;
}
public Fraction add(Fraction f2) {
Fraction r = new Fraction((numerator * f2.denominator)
+ (f2.numerator * denominator), (denominator * f2.denominator));
return r;
}
static private int gcd(int x, int y) {
return y == 0 ? x : gcd(y, x % y);
}
public static String asFraction(int x, int y) {
int gcd = gcd(x, y);
return (x / gcd) + "/" + (y / gcd);
}
/*public static void main(String[] argv) {
Fraction f0 = new Fraction();
Fraction f1 = new Fraction(3);
Fraction f2 = new Fraction(20, 60);
Fraction f3 = new Fraction(1, 3);
System.out.println("--------------Testing constructors--------------");
f0.display();
f1.display();
f2.display();
System.out.println("--------------Test if two fractions is equal--------------");
System.out.println(f2.isEquals(f1));
}*/
}
and class Test:
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void enterFraction(){
ArrayList<Fraction> arr = new ArrayList<Fraction>();
Scanner scanner = new Scanner(System.in);
boolean check = false;
int i = 1;
while(!check){
System.out.println("Enter fraction"+i+":");
Fraction f = new Fraction();
System.out.println("Enter Numerator: ");
int numerator = scanner.nextInt();
scanner.nextLine();
f.setNumerator(numerator);
System.out.println("Enter Denominator: ");
int denominator = scanner.nextInt();
scanner.nextLine();
f.setDenominator(denominator);
System.out.println("Your fraction"+i+" is: "+f.getNumerator()+"/"+f.getDenominator());
arr.add(f);
System.out.println("Want to compare fractions? (Y/Yes or N/No)");
String compareRequest = scanner.nextLine();
if(compareRequest.equalsIgnoreCase("y")){
System.out.println("Choose your target fraction!!! (enter the index of the array)");
int position = scanner.nextInt();
scanner.nextLine();
Fraction targetFraction = arr.get(position);
targetFraction.display();
System.out.println("Choose your second fraction to compare!!! (enter the index of the array)");
int position2 = scanner.nextInt();
scanner.nextLine();
Fraction secondFraction = arr.get(position2);
secondFraction.display();
boolean compareTwoFractions = secondFraction.isEquals(targetFraction);
if(compareTwoFractions == true){
System.out.println("Two fractions are equal");
}
else if(compareTwoFractions == false){
System.out.println("Two fractions are not equal");
}
}
i++;
System.out.println("Do you want to enter more fraction? (Y/Yes or N/No)");
String checkRequest = scanner.nextLine();
if(checkRequest.equalsIgnoreCase("n")){
check = true;
}
}
}
public static void main(String[] args){
enterFraction();
}
}
I input like this:
Enter fraction1:
Enter Numerator:
2
Enter Denominator:
4
Your fraction1 is: 2/4
Want to compare fractions? (Y/Yes or N/No)
n
Do you want to enter more fraction? (Y/Yes or N/No)
y
Enter fraction2:
Enter Numerator:
1
Enter Denominator:
3
Your fraction2 is: 1/3
Want to compare fractions? (Y/Yes or N/No)
y
Choose your target fraction!!! (enter the index of the array)
0
2/4
Choose your second fraction to compare!!! (enter the index of the array)
1
1/3
Two fractions are equal
Do you want to enter more fraction? (Y/Yes or N/No)
You see it not work, 2/4 == 1/3. Please point me somethings with this.
The problem is that getNumerator(), getDenominator(), and gcd return an int. Therefore, the division inside your equals method is done in integers:
double fractionFloatValue = (f.getNumerator()/gcd1) / (f.getDenominator()/gcd1);
...
double fractionFloatValue2 = (this.getNumerator()/gcd2) / (this.getDenominator()/gcd2);
The value of fractionFloatValue and fractionFloatValue2 are, in fact, integers, even though they are assigned to variables of type double. Both 1/3 and 1/2 are proper fractions, so integer division evaluates to zero in both cases. That's why your equals returns true in both cases.
There are two ways to fix this:
Declare gcd1 and gcd2 as double. This would force the division into double; unfortunately, your code would suffer from double comparison for equality, which is inherently imprecise, or
Use identity n1/d1 == n2/d2 when n1*d2 == n2*d1. This eliminates division, so you get perfect precision in your comparisons until you overflow (and you would not overflow with the constraints that you are using if you use long for the results of your multiplication).
I change two line that #dasblinkenlight has mentioned by:
double fractionFloatValue = ((f.getNumerator()/gcd1)*1.0) / (f.getDenominator()/gcd1);
double fractionFloatValue2 = ((this.getNumerator()/gcd2)*1.0) / (this.getDenominator()/gcd2);
and It worked now.

Computing the nth root of p using BigDecimals

I am currently trying to solve this problem as described here:
http://uva.onlinejudge.org/external/1/113.pdf
The plan was to implement a recursive function to derive the solution. Some of the code here comes from Rosetta code for determining the nth root.
// Power of Cryptography 113
import java.util.Scanner;
import java.math.BigDecimal;
import java.math.RoundingMode;
// k can be 10^9
// n <= 200
// p <= 10^101
class crypto {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
// Given two integers (n,p)
// Find k such k^n = p
int n = in.nextInt();
BigDecimal p = in.nextBigDecimal();
System.out.println(nthroot(n,p));
}
}
public static BigDecimal nthroot(int n, BigDecimal A) {
return nthroot(n, A, .001);
}
public static BigDecimal nthroot(int n, BigDecimal A, double p) {
if(A.compareTo(BigDecimal.ZERO) < 0) return new BigDecimal(-1);
// we handle only real positive numbers
else if(A.equals(BigDecimal.ZERO)) {
return BigDecimal.ZERO;
}
BigDecimal x_prev = A;
BigDecimal x = A.divide(new BigDecimal(n)); // starting "guessed" value...
BigDecimal y = x.subtract(x_prev);
while(y.abs().compareTo(new BigDecimal(p)) > 0) {
x_prev = x;
BigDecimal temp = new BigDecimal(n-1.0);
x = (x.multiply(temp).add(A).divide(x.pow(temp.intValue())).divide(new BigDecimal(n)));
}
return x;
}
}
And here is the resulting error code:
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1616)
at crypto.nthroot(crypto.java:38)
at crypto.nthroot(crypto.java:24)
at crypto.main(crypto.java:19)
Anybody here for a working code snippet? Here we go:
public final class RootCalculus {
private static final int SCALE = 10;
private static final int ROUNDING_MODE = BigDecimal.ROUND_HALF_DOWN;
public static BigDecimal nthRoot(final int n, final BigDecimal a) {
return nthRoot(n, a, BigDecimal.valueOf(.1).movePointLeft(SCALE));
}
private static BigDecimal nthRoot(final int n, final BigDecimal a, final BigDecimal p) {
if (a.compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException("nth root can only be calculated for positive numbers");
}
if (a.equals(BigDecimal.ZERO)) {
return BigDecimal.ZERO;
}
BigDecimal xPrev = a;
BigDecimal x = a.divide(new BigDecimal(n), SCALE, ROUNDING_MODE); // starting "guessed" value...
while (x.subtract(xPrev).abs().compareTo(p) > 0) {
xPrev = x;
x = BigDecimal.valueOf(n - 1.0)
.multiply(x)
.add(a.divide(x.pow(n - 1), SCALE, ROUNDING_MODE))
.divide(new BigDecimal(n), SCALE, ROUNDING_MODE);
}
return x;
}
private RootCalculus() {
}
}
Just set SCALE to however precise you need the calculation to be.
That is expected if the resulting mathematical decimal number is non-terminating. The Javadocs for the 1-arg overload of divide state:
Throws:
ArithmeticException - if the exact quotient does not have a terminating decimal expansion
Use another overload of the divide method to specify a scale (a cutoff) (and a RoundingMode).

Do I need this whole class to find out if the fraction is reduced or not?

What I have to do is take 2 random variables for a fraction, 1 to 1000, and check to see if they are in reduced terms already or not. I do this 1,000 times and keep track of whether it was or wasn't in reduced terms.
Here is the main class
import java.util.*;
public class ratio1 {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int nonReducedCount = 0; //counts how many non reduced ratios there are
for(int i =1; i<=1000; i++){
Random rand = new Random();
int n = rand.nextInt(1000)+1; //random int creation
int m = rand.nextInt(1000)+1;
Ratio ratio = new Ratio(n,m);
if (ratio.getReduceCount() != 0 ){ // if the ratio was not already fully reduced
nonReducedCount++; // increase the count of non reduced ratios
}
}
int reducedCount = 1000 - nonReducedCount; //number of times the ratio was reduced already
double reducedRatio = reducedCount / nonReducedCount; //the ratio for reduced and not reduced
reducedRatio *= 6;
reducedRatio = Math.sqrt(reducedRatio);
System.out.println("pi is " + reducedRatio);
}
}
And here is the class I am not sure about. All I want from it is to determine whether or not the fraction is already in simplest form. When I currently try to run it, it is giving me an error; "Exception in thread "main" java.lang.StackOverflowError
at Ratio.gcd (Ratio.java:67)
at Ratio.gcd (Ratio.java:66)"
public class Ratio{
protected int numerator; // numerator of ratio
protected int denominator; //denominator of ratio
public int reduceCount = 0; //counts how many times the reducer goes
public Ratio(int top, int bottom)
//pre: bottom !=0
//post: constructs a ratio equivalent to top::bottom
{
numerator = top;
denominator = bottom;
reduce();
}
public int getNumerator()
//post: return the numerator of the fraction
{
return numerator;
}
public int getDenominator()
//post: return the denominator of the fraction
{
return denominator;
}
public double getValue()
//post: return the double equivalent of the ratio
{
return (double)numerator/(double)denominator;
}
public int getReduceCount()
//post: returns the reduceCount
{
return reduceCount;
}
public Ratio add(Ratio other)
//pre: other is nonnull
//post: return new fraction--the sum of this and other
{
return new Ratio(this.numerator*other.denominator+this.denominator*other.numerator,this.denominator*other.denominator);
}
protected void reduce()
//post: numerator and denominator are set so that the greatest common divisor of the numerator and demoninator is 1
{
int divisor = gcd(numerator, denominator);
if(denominator < 0) divisor = -divisor;
numerator /= divisor;
denominator /= divisor;
reduceCount++;
}
protected static int gcd(int a, int b)
//post: computes the greatest integer value that divides a and b
{
if (a<0) return gcd(-a,b);
if (a==0){
if(b==0) return 1;
else return b;
}
if (b>a) return gcd(b,a);
return gcd(b%a,a);
}
public String toString()
//post:returns a string that represents this fraction.
{
return getNumerator()+"/"+getDenominator();
}
}
Here are the lines of the error in the Ratio class;
if (b>a) return gcd(b,a);
return gcd(b%a,a);
A fraction is reducible if its GCD is greater than 1. You can compute the GCD with the static method given in Ratio, so you could instead use:
...
int n = rand.nextInt(1000)+1;
int m = rand.nextInt(1000)+1;
if(Ratio.gcd(n,m) == 1) {
nonReducedCount++;
}
This saves you from instantiating a new Ratio instance.
If that method doesn't work for you, you can always use your own GCD calculator. This one is recursive too and similar to the one in Ratio:
public static int gcd(int a, int b) { return b==0 ? a : gcd(b,a%b); }
You could Google it for non-recursive methods if the StackOverflowError is still a problem.

Categories