PDA

View Full Version : wat is wrong with my program?


Soberbia
06-28-2007, 05:57 AM
i need help with this program, it doesn't show me any error but it doesn't generate enough questions... i'm new at java so i really dont know how to get this to work... the program is for generating questions on subtraction of fractions.. i need an eye to help me on this. thanks in advance




public class FractionSubtraction
{
public static void main(String[] args)
{
int a, b, c, e, f, g, ans, deno;
String sent, num1, num2, fin;
int count = 20; //number of questions
String[] list = new String[count + 1];
for (int i=0; i < list.length; i++)
{
a = (int) (1 + Math.random() * 20);
b = (int) (1 + Math.random() * 20);
c = (int) (1 + Math.random() * 20);
e = (int) (Math.random() * 11);
f = (int) (Math.random() * 11);
while (b == 1)
{
b = (int) (1 + Math.random() * 20);
}
while (e < f)
{
e = (int) (Math.random() * 11);
f = (int) (Math.random() * 11);
}
while (a >= b)
{
a = (int) (1 + Math.random() * 20);
}
while (c >= b || (c == a && b != 2))
{
c = (int) (1 + Math.random() * 20);
}
while ((e*b + a) - (f*b + c) <= 0)
{
while (e < f)
{
e = (int) (Math.random() * 11);
f = (int) (Math.random() * 11);
}
while (a >= b)
{
a = (int) (1 + Math.random() * 20);
}
while (c >= b || (c == a && b != 2))
{
c = (int) (1 + Math.random() * 20);
}
}
g = 0;
ans = (e*b + a) - (f*b + c);
while (ans >= b)
{
ans = ans - b;
g++;
}

deno = b;
if (e > 0)
{
num1 = "" + e + " ";
} else
{
num1 = "";
}
if (f > 0)
{
num2 = "" + f + " ";
} else
{
num2 = "";
}
if (g > 0)
{
fin = "" + g + " ";
} else
{
fin = "";
}
if (ans > 0) //reduce fraction in final answer
{
for (int red = 2; red <= deno; red++)
{
while (ans%red == 0 && deno%red == 0)
{
ans = ans/red;
deno = deno/red;
}
}
sent = "" + num1 + a + "/" + b + " - " + num2 + c + "/" + b + " = " + fin + ans + "/" + deno;
} else
{
sent = "" + num1 + a + "/" + b + " - " + num2 + c + "/" + b + " = " + fin;
}
for (int j = 0; j < i; j++)
{
if (sent.equals(list[j])) // eliminate any repeated questions
{
i--;
break;
} else if (j == i -1)
{
list[i] = sent;
System.out.println(list[i]);
}
}
}
}
}

Soberbia
06-28-2007, 06:02 AM
jsut ignore the (highlight) (/highlight) stuff.. sorry the edit button didn't work for me

brad211987
06-28-2007, 03:08 PM
Could you post a sample output and post the code inside of code tags? Tat will make it easier to read and debug.

Soberbia
06-29-2007, 03:33 AM
this program is suppoesd to generate questions that subtract a fraction with another fraction to get a positive answer... the problem i'm havign now is that sometimes it doesn't generates any questions and soemtimes it generates a few and then gets stuck....

public class FractionSubtraction
{
public static void main(String[] args)
{
int a, b, c, e, f, g, ans, deno;
String sent, num1, num2, fin;
int count = 20; //number of questions
String[] list = new String[count + 1];
for (int i=0; i < list.length; i++) //make a for loop to make sure there are no repeated questions
{
//defining variables
a = (int) (1 + Math.random() * 20);
b = (int) (1 + Math.random() * 20);
c = (int) (1 + Math.random() * 20);
e = (int) (Math.random() * 11);
f = (int) (Math.random() * 11);
while (b == 1) //make sure the denominator does not equal to 1
{
b = (int) (1 + Math.random() * 20);
}
while (e < f) //make sure the first fraction's constant is larger or equal to the second fraction's constant (its a mixed fraction subtraction)
{
e = (int) (Math.random() * 11);
f = (int) (Math.random() * 11);
}
while (a >= b) // make sure the numerator of the first number is smaller than the denominator
{
a = (int) (1 + Math.random() * 20);
}
while (c >= b || (c == a && b != 2)) //make sure the numerator of the second number is smaller than the denominator and doesn't equal to the first number if the denominator is not 2 (since if the denominator is 2, the only choice for the numerator is 1)
{
c = (int) (1 + Math.random() * 20);
}
while ((e*b + a) - (f*b + c) <= 0) //make sure the numbers generated does not give a negative answer, if it does, the numbers will change
{
while (e < f)
{
e = (int) (Math.random() * 11);
f = (int) (Math.random() * 11);
}
while (a >= b)
{
a = (int) (1 + Math.random() * 20);
}
while (c >= b || (c == a && b != 2))
{
c = (int) (1 + Math.random() * 20);
}
}
g = 0; //initializing the constant for the answer
ans = (e*b + a) - (f*b + c); //calculating the numerator of the answer
while (ans >= b) //complete the mixed fraction by addin the constant if the numerator is larger than the denominator in the answer
{
ans = ans - b;
g++;
}
//initializing the denominator for the answer
deno = b;
if (e > 0) //these codes are for the representation afterwards, to make the questions generated look better
{
num1 = "" + e + " ";
} else
{
num1 = "";
}
if (f > 0)
{
num2 = "" + f + " ";
} else
{
num2 = "";
}
if (g > 0)
{
fin = "" + g + " ";
} else
{
fin = "";
}
if (ans > 0) //reduction of the fraction in the final answer
{
for (int red = 2; red <= deno; red++)
{
while (ans%red == 0 && deno%red == 0)
{
ans = ans/red;
deno = deno/red;
}
}
sent = "" + num1 + a + "/" + b + " - " + num2 + c + "/" + b + " = " + fin + ans + "/" + deno;
} else
{
sent = "" + num1 + a + "/" + b + " - " + num2 + c + "/" + b + " = " + fin;
}
for (int j = 0; j < i; j++)
{
if (sent.equals(list[j])) // eliminate any repeated questions
{
i--;
break;
} else if (j == i -1) //if there are no repeated questions, the program will print the current question out after storing it in the array
{
list[i] = sent;
System.out.println(list[i]);
}
}
}
}
}



here are some of the sample outputs

10 11/14 - 3 2/14 = 7 9/14
7 5/6 - 2 1/6 = 5 2/3
5 8/14 - 3 12/14 = 1 5/7
5 2/19 - 1 1/19 = 4 1/19
9 1/10 - 1 2/10 = 7 9/10

after it generated these... it got stuck... its supposed to genreate 15 more =/

Spookster
06-29-2007, 03:48 AM
You can debug this by simply putting some print statements in your loops and print out the variables involved with producing your output That way you can see what happens up to the point where it gets stuck. I would venture to guess you are either getting stuck in an infinite loop or your loop is ending prematurely. In either case you will be able to figure that out by printing out the variables involved. In order to learn how to program you must also learn common debugging techniques. If you cannot use a debugging tool then print statements are you next best friend in debugging.

Soberbia
06-29-2007, 05:33 AM
apprantly the program is goin on an infinite loop for the while loop on line 36, are there any suggestions to help me solve this problem?

Soberbia
06-29-2007, 05:34 AM
sorry i counted accordin to my program but not the one i posted here
the infinite while loop i mentioned is the following:
while ((e*b + a) - (f*b + c) <= 0)

Soberbia
06-29-2007, 05:43 AM
oh! i solved it!
thanks spookster
but still, i'd still love to hear any advice on how to improve my program. thank you