[Math Lair] pytriple.java

Math Lair Home > Interactive content > pytriple.java
// pytriple.java - Calculates Pythagorean triples given m and n.
// Written 05/08/2000, James Yolkowski

import java.applet.*;
import java.awt.*;
import java.lang.*;

public class pytriple extends Applet {

  TextField m, n, a, b, c;
  Button compute;
  Label messages;
  Label mLabel, nLabel, aLabel, bLabel, cLabel;

  public void init() {
    resize(300, 96);
    m = new TextField(5);
    n = new TextField(5);
    a = new TextField(5);
    b = new TextField(5);
    c = new TextField(5);
    compute = new Button("Compute");
    mLabel = new Label("m: ");
    nLabel = new Label("n: ");
    aLabel = new Label("a: ");
    bLabel = new Label("b: ");
    cLabel = new Label("c: ");
    messages = new Label("                                                  ");
    add(mLabel); add(m);
    add(nLabel); add(n);
    add(compute);
    add(aLabel); add(a);
    add(bLabel); add(b);
    add(cLabel); add(c);
    add(messages);
  }

  public void paint(Graphics g) {}

  public boolean action (Event e, Object O) {
    long mInt, nInt, aInt, bInt, cInt;
    String tempstr;
    if(e.target instanceof Button) {

      tempstr = "0";
      tempstr = tempstr.concat(m.getText());
      mInt = Integer.parseInt(tempstr);

      tempstr = "0";
      tempstr = tempstr.concat(n.getText());
      nInt = Integer.parseInt(tempstr);
      
      if (nInt <= 0 || mInt <= 0)
	messages.setText("Value out of range.");
      else if (nInt >= mInt)
	messages.setText("m not greater than n.");
      else {
	messages.setText("");
	aInt = mInt * mInt - nInt * nInt;
	bInt = 2 * mInt * nInt;
	cInt = mInt * mInt + nInt * nInt;
	a.setText(String.valueOf(aInt));
	b.setText(String.valueOf(bInt));
	c.setText(String.valueOf(cInt));
      }
    }
    return true;
  }
}