// Scott DeRuiter 7/5/02
// GCF.java
// Program prompts the user for two positive integers
// The program then calculates and prints the GCF
// 7/15/2011 - Greenstein: Changed TextReader to Scanner
import java.util.Scanner;
public class GCF
{
private int x, y, remainder;
public GCF ( )
{
x = y = remainder = 0;
}
public static void main ( String [] args )
{
GCF gcf = new GCF ( );
gcf.GetIntegers ( );
gcf.CalculateGCF ( );
gcf.PrintGCF ( );
}
public void GetIntegers ( )
{
x = GetAnInt ( "first " );
y = GetAnInt ( "second" );
System.out.print ( "\n\nThe GCF for " + x + " and " + y + " is: " );
}
public int GetAnInt ( String name )
{
Scanner console = new Scanner ( System.in );
System.out.print ( "\n\nEnter the " + name + " positive integer -> " );
int temp = console.nextInt ( );
return temp;
}
public void CalculateGCF ( )
{
remainder = x % y;
while ( remainder != 0 )
{
x = y;
y = remainder;
remainder = x % y;
}
}
public void PrintGCF ( )
{
System.out.println ( y + "\n\n" );
}
}
Back to Lesson 7 Examples
Back to Java Main Page