// a simple GCDdemonstration
// John Sklar, CED 512 - fall, 2k
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class gcd extends Applet implements ActionListener
{
TextField integer1,integer2;
Button action = new Button (" Find the GCD ");
Label answer, Banner;
public void init()
{
Font bigFont = new Font("TimesRoman", Font.ITALIC, 24);
Font biggerFont = new Font("TimesRoman", Font.ITALIC, 30);
Font monoFont = new Font("Courier", Font.BOLD, 24);
integer1 = new TextField(5);
integer2 = new TextField(5);
Banner = new Label ("Enter two integers");
answer = new Label(" ");
action.setFont(monoFont);
answer.setFont(biggerFont);
Banner.setFont(bigFont);
integer1.setFont(monoFont);
integer2.setFont(monoFont);
add (Banner);
add (integer1);
add (integer2);
add (action);
add (answer);
action.addActionListener(this);
integer1.addActionListener(this);
integer2.addActionListener(this);
setBackground(Color.white);
}
public void actionPerformed (ActionEvent e)
{
String results = new String("");
int one,two,hold;
if (action.getLabel()==" Find the GCD ")
{
one=Integer.parseInt(integer1.getText());
two=Integer.parseInt(integer2.getText());
if(one < 0 || two < 0)
results = "Please... Positive integers only.";
else{
results = "The GCD of " + one + " and " + two + " is ";
if(one > two){
hold = one;
one = two;
two = hold;
}
if(one != two)
while(two !=0){
hold = one % two;
one = two;
two = hold;
}
results += one; // add the number itself to the string
}
answer.setText (results);
action.setLabel(" Clear ");
integer1.setEnabled(false);// no typing now
integer2.setEnabled(false);// no typing now
}
else{
Banner.setText ("Enter two integers");
integer1.setText("");
integer2.setText("");
answer.setText (" ");
action.setLabel(" Find the GCD ");
integer1.setEnabled(true);// ok, you can type
integer2.setEnabled(true);// ok, you can type
}
}
}