import java.awt.*;
import java.applet.Applet;

public class ggTApplet extends Applet {

    static final long serialVersion=4711L; 
    
    TextField[] input = new TextField[2];
    
    TextField[] resultat = new TextField[1];
    
    Button ggTButton,clearButton;
    
    String[] str=new String[2];
    
    rechenwerkggT meinRechenwerk;
    
    long[] eingabe=new long[2];
    
    long[] ergebnis=new long[1];
    
	public void init() {
		
		for(int i=0;i<input.length;i++) {
            
            input[i]=new TextField(100);
            
            add(input[i]);
            
            input[i].setBackground(Color.black);
            
            input[i].setForeground(Color.red);
        }
	  
        ggTButton=new Button("ggT");
        
        add(ggTButton);
        
        clearButton=new Button("clear");
        
        add(clearButton);
        
        for(int i=0;i<resultat.length;i++) {
            
            resultat[i]=new TextField(100);
            
            add(resultat[i]);
            
            resultat[i].setBackground(Color.black);
            
            resultat[i].setForeground(Color.green);
        }
	}

	
	public boolean action(Event event,Object eventobject) {
        
        if(event.target==ggTButton) {
            
            for(int i=0;i<str.length;i++) str[i]=input[i].getText();
            
            meinRechenwerk=new rechenwerkggT();
            
            eingabe=meinRechenwerk.konvertiere(str);
            
            ergebnis=meinRechenwerk.ggT(ergebnis,eingabe);
            
            input[0].setText("a="+eingabe[0]);
            
            input[1].setText("b="+eingabe[1]);
            
            resultat[0].setText("ggT="+ergebnis[0]);
            
            return true;
            
        }
        
        if(event.target==clearButton) {
            
            for(int i=0;i<input.length;i++) input[i].setText("");
            
            for(int i=0;i<resultat.length;i++) resultat[i].setText("");
            
            return true;
        }
        
        return true;
        
    }
}

class rechenwerkggT {

    Long[] Eingabe=new Long[2];
    
    long[] eingabe=new long[2];
    
    String[] str=new String[2];
    
    long[] ergebnis=new long[1];
     
    rechenwerkggT() {
        
    }
    
    void konvertiere() {
        
        for(int i=0;i<Eingabe.length;i++) Eingabe[i]=Long.valueOf(str[i]);
        
        for(int i=0;i<eingabe.length;i++) eingabe[i]=Eingabe[i].longValue();
    }
    
    long[] konvertiere(String[] str) {
        
        for(int i=0;i<str.length;i++) this.str[i]=str[i];
        
        konvertiere();
        
        return eingabe;
    }
    
    long[] ggT(long[] ergebnis,long[] eingabe) {
        
        this.ergebnis=ergebnis;
        
        this.eingabe=eingabe;
        
        long h1=eingabe[0];
        
        long h2=eingabe[1];
        
        if((h1==0)||(h2==0)) {
            
            
        }
        
        if(h1<0) h1=h1*(-1);
        
        if(h2<0) h2=h2*(-1);
        
        while(true) 
            
            if(h1<h2) {
                
                h2=h2%h1;
                
                if(h2==0) {
                    
                    ergebnis[0]=h1;
                    
                    break;
                }
            }
            
            else {
                
                h1=h1%h2;
                
                if(h1==0) {
                    
                    ergebnis[0]=h2;
                    
                    break;
                }
            }
            
        return ergebnis;
    }
}



