/***
Repeating Decimals ******************************** In Martin's math class he saw that fractions can be changed to repeating decimals. For example: 6/11 = 0.545454... Martin wrote a program to practice converting repeating decimals BACK into fractions. ********************************************************/ public class RepeatingDecimals { public RepeatingDecimals() { int b = (int)(Math.random()*30+1); int a = (int)(Math.random()*b+1); double d = (double)a / (double)b; double g, top, bot; do { output("Target number = " + d); top = Integer.parseInt(input("Guess top")); bot = Integer.parseInt(input("Guess bottom")); g = (double)top / (double)bot; } while ( g != d ); output("SUCCESS"); } public static void main(String[] args) { new RepeatingDecimals(); } public String input(String prompt) { return javax.swing.JOptionPane.showInputDialog(null,prompt); } public void output(String message) { javax.swing.JOptionPane.showMessageDialog(null,message); } } |
In this program, it's necessary to use int (integer) types to choose the random numbers.
But we need to change these
to double format to do correct
division and produce proper decimal values. If we tried to use double
values for a and b,
e.g. the original random numbers, we would get something like 7.358 and
4.125 for a and b. The result would be a
decimal value where it is impossible to guess the original numbers.
Both int and double
values have their uses in Java. If we need to convert from one to
the other, there are several ways.
Type-cast
from int to double
: double dnum =
(double)inum;
int nnn = (int) dnum ;
Math class
functions:
int iii = (int)Math.round( fnum )
; // rounds up or down appropriately
double fff = Math.floor( inum
); // floor
truncates (throws away) decimal part
It doesn't matter which method you use to do these conversions. But
you DO need to make sure that you use
the appropriate type for the operation you are performing.
int b = (int)(Math.random()*30+1); int a = (int)(Math.random()*b+1);
double d = (double)a / (double)b;
In this program, we must use int values
for the initial choice of random numbers.
After that, the program uses double
values to do the division correctly.
The same idea is used when inputting the user's guess, but in a slightly different way.
top = Integer.parseInt(input("Guess top")); bot = Integer.parseInt(input("Guess bottom"));The user's input is actually a String - that is, a sequence of keyboard characters.
g = (double)top / (double)bot;
It is NOT possible to use a cast (int) to change a String to a
number.
The correct standard Java commands for String to number conversions are:
int n = Integer.parseInt( input("Type an integer") ); double d = Double.parseDouble( input("Type a decimal") );
The problem of guessing factors of a number is closely related to encryption algorithms.
One popular encryption algorithm
is based on choosing to very large prime numbers
and then multiplying them to produce a very large integer to use as an encryption key.
There is only one pair of prime numbers that create the key, and it is
very difficult
to guess these prime numbers. You could read
this for a brief introduction to RSA encryption.