/***
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); } } |

differently in different situations. For example:

Dividing integers : 7 / 4
==>
1

Dividing decimals: 7.0 / 4.0 ==> 1.75

Dividing decimals: 7.0 / 4.0 ==> 1.75

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;

The Integer.parseInt( ) method converts the String input into an int value.

Then the int values are converted to

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") );

- Download the program and run it.

Practice guessing until you manage to get 2 answers correct. - Change the program so that it only chooses numbers between 1 and 12 -

choosing up to 30 makes the problem too difficult.

Make sure the value of A (numerator) is always smaller than B (denomiator).

- Change the program so that it produces a product
of 2 random numbers, e.g.:

a = 5;

b = 12;

p = a*b; ==> 60

Now the user must guess two numbers that MULTIPLY to produce the answer.

These are FACTORS of 60. They don't actually need to be the same as a and b.

So 3 * 20 would be an acceptable answer.

However, the program should NOT accept 1 * 60 - that is too simple. - Write a program where the PROGRAM attempts to guess factors of a
number.

The user types in an int number, say 1234567. Then the program runs a LOOP,

where it repeatedly generates a random integer and checks whether that integer

can divide the original number. This can be done as follows:

num = 1234567;

guess = (int)( random()*num + 1 );

test = (double)num / (double)guess;

if( test * guess == num) { output("it worked"); }

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.