Gcd - add(prompt2); //put prompt2 on applet a input2 = new...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/* Author: Eric Adlam * Section #: D5 * Instructor: Coach * Project Name: Greatest Common Denominator * Assignment #: 6 * Due Date: 10/01/09 * * Description: Java Applet which finds the Greatest Common * Denominator of two user provided numbers. */ package week5; p import java.applet.Applet; import java.awt.*; import java.awt.event.*; i public class Gcd extends Applet implements ActionListener { Label prompt1; // prompt user to input first value TextField input1; // input first value here Label prompt2; // prompt user to input second value TextField input2; // input second value here int num1; // store input values int num2; // store input values // setup the graphical user interface components // and initialize variables public void init(){ prompt1 = new Label("Enter first number: "); add(prompt1); //put prompt1 on applet a input1 = new TextField(10); add(input1); //put input1 on applet a prompt2 = new Label("Enter second number:");
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: add(prompt2); //put prompt2 on applet a input2 = new TextField(10); input2.addActionListener(this); add(input2); //put input2 on applet } //finds the greates common denominator of two integers public int findGcd(int a, int b){ int current = (a < b)? a : b; while ((a % current) + (b % current) > 0){ current -= 1; } return current; } //display the results public void paint(Graphics g){ int gcd = findGcd(num1,num2); i // put totals on applet g.drawString("Num1: " + num1, 70, 100); g.drawString("Num2: " + num2, 70, 125); g.drawString("GCD: " + gcd, 70, 150); showStatus(Integer.toString(gcd)); } //process user's action on the input2 text field public void actionPerformed( ActionEvent e ){ num1 = Integer.parseInt( input1.getText() ); num2 = Integer.parseInt( input2.getText() ); n repaint(); } }...
View Full Document

This note was uploaded on 10/12/2009 for the course CS 120 taught by Professor Robertdependahl during the Fall '09 term at Santa Barbara City.

Page1 / 2

Gcd - add(prompt2); //put prompt2 on applet a input2 = new...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online