// Prime
//
// This program gives a dialog allowing the user to test whether
// numbers are prime.
//
// The only part of this that is directly relevant to the class is
// the first function, PrimeTestAll. The rest, you can ignore if you'd
// like. (I'm not stopping you from looking over it, too, if you're
// interested, but it's not necessary.)
import java.awt.*;

public class Prime extends Frame {
	protected Label header;
	protected TextField query;
	protected Button dotest;
	protected Button doquit;
	
	// Prime-Test-All
	//
	// determines whether the input <n> is prime (i.e., if
	// there are no divisors of <n> save 1 and <n> itself).
	//
	// The function returns 1 if <n> is prime and 0 otherwise.
	public static int PrimeTestAll(int n) {
		int i;
		
		// small numbers are never prime.
		if(n < 2) {
			return 0;
		}
		
		// try all <i> between 2 and sqrt(<n>)
		for(i = 2; i * i <= n; i = i + 1) {
			if(n % i == 0) {
				// then <i> divides <n>
				return 0;
			}
		}
		return 1;
	}
	
	// Prime
	//
	// creates the dialog box
	public Prime() {
		super("Test for primality");

		Panel p;
		GridBagConstraints c;
		
		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.ipady = 5;
		c.anchor = GridBagConstraints.CENTER;
		c.weightx = 1.0;
		
		// create the label giving output to user
		this.setLayout(new GridBagLayout());
		header = new Label("Hello, world! Enter the number to test.");
		c.gridy = 0;
		c.weighty = 0.0;
		c.fill = GridBagConstraints.HORIZONTAL;
		((GridBagLayout) this.getLayout()).setConstraints(header, c);
		this.add(header);
		
		// create text field to get input from user
		query = new TextField("42");
		c.gridy = 1;
		c.weighty = 1.0;
		c.fill = GridBagConstraints.HORIZONTAL;
		((GridBagLayout) this.getLayout()).setConstraints(query, c);
		this.add(query);
		
		// create a Panel to hold the buttons.
		p = new Panel();
		p.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15));
		
		// add the ``Test'' button
		dotest = new Button("Test");
		p.add(dotest);
		
		// add the ``Quit'' button
		doquit = new Button("Quit");
		p.add(doquit);
		
		// put Panel in dialog
		c.gridy = 2;
		c.weighty = 0.0;
		c.fill = GridBagConstraints.NONE;
		((GridBagLayout) this.getLayout()).setConstraints(p, c);
		this.add(p);
		this.pack();
	}
	
	// action
	//
	// handle button clicks
	public boolean action(Event e, Object arg) {
		String input;
		int num;
		int is_prime;
		
		if(e.target == dotest || e.target == query) {
			// we should test the query

			// first, get the input from the query field
			input = query.getText();

			// translate this into a number
			try {
				num = Integer.parseInt(input);
			} catch(Exception dummy) {
				header.setText("The input is not a number. "
					+ "Enter next number to test.");
				return true;
			}

			// test to see whether it is prime
			is_prime = PrimeTestAll(num);

			// put output in the header label
			if(is_prime == 1) {
				header.setText(num + " is prime. Enter next number to test.");
			} else {
				header.setText(num + " is not prime. Enter next number to test.");
			}
			return true;
		} else if(e.target == doquit) {
			this.dispose();
			return true;
		} else {
			return false;
		}
	}
	
	// gotFocus
	//
	// sets the default focus location
	public boolean gotFocus(Event e, Object arg) {
		query.requestFocus();
		return true;
	}
	
	// main
	//
	// this is where the program begins
	public static void main(String[] args) {
		Frame f;
		Prime dialog;
		
		// create the dialog box
		f = new Prime();
		f.resize(300, 100);
		f.show();
	}
}
