Repeating Decimals

  Download Source Code


Types and Type-Casts - int and float and Strings

In Java, division ( / ) operations are tricky.  The fancy term is that division is polymorphic.  That means it behaves
differently in different situations.  For example:

Dividing integers :   7 / 4  ==>  1           

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 float format to do correct division and produce proper decimal values.  If we tried to use float 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 float values have their uses in Java.  If we need to convert from one to the other, there are several ways.

Processing functions :                int   nnn  =  int( fff );
  (not standard Java)                     float  fnum =  float( inum );

Type-cast from int to float :       float  fnum  =  (float)inum;    
                                                     int    nnn  =  (int) fff ;

Math class functions:                 int   iii  =   (int)Math.round( fnum ) ;    // rounds up or down appropriately
                                                     float  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(random(30)+1);
    int a = int(random(b)+1);

float d = (float)a / (float)b;

In this program, we must use int values for the initial choice of random numbers.
After that, the program uses float values to do the division correctly. 

    top = int(input("Guess top"));
    bot = int(input("Guess bottom"));
g = (float)top / (float)bot;

The same idea is used when inputting the user's guess, but in a slightly different way.
The user's input is actually a String - that is, a sequence of keyboard characters.
Processing's int( ) function converts the String input into an int value. 
Then the int values are converted to float before dividing.

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") );
    float d = Float.parseFloat( input("Type a decimal") ); 

Practice

Further Reading About Factors

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.