Er einhver svo klókur að sjá hvað ég er að gera vitlaust hérna?
Þetta forrit á að geta tekið við tvemur brotum, lagt þau saman og skilað fullstytt. Það eina sem er að er að ég fæ ekki rétt svar, annars virkar allt annað…held ég.

/**
* @(#)Verkefni_4_6.java
*
* Verkefni_4_6 Applet application
*
* @author
* @version 1.00 2008/2/12
*/

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

public class Verkefni_4_6 extends Applet {


TextField tBrot1, tBrot2, tFullstytt;
Button bStytta;
Label lBrot1,lBrot2, lFullstytt;
Integer Teljari1, Nefnari1, Teljari2, Nefnari2;
int teljari, nefnari, teljari1, nefnari1, teljari2, nefnari2, t, n, afg, i1, j1, i2, j2,
nefnariSaman, teljariSaman;
String sBrot1, sTeljari1, sNefnari1, sBrot2, sTeljari2, sNefnari2;

public void init()
{
lBrot1 = new Label(“Brot 1”);
tBrot1 = new TextField(5);
lBrot2 = new Label(“Brot 2”);
tBrot2 = new TextField(5);
lFullstytt = new Label(“Fullstytt”);
tFullstytt = new TextField(4);
bStytta = new Button(“Leggja saman og stytta”);
this.add(lBrot1);
this.add(tBrot1);
this.add(lBrot2);
this.add(tBrot2);
this.add(bStytta);
this.add(lFullstytt);
this.add(tFullstytt);
}

public boolean action(Event e, Object o)
{
if (e.target == bStytta)
{
sBrot1 = new String();
sTeljari1 = new String();
sNefnari1 = new String();

sBrot2 = new String();
sTeljari2 = new String();
sNefnari2 = new String();


sBrot1 = tBrot1.getText();
int i1 = sBrot1.indexOf(“/”);
int j1 = sBrot1.length();

sTeljari1 = sBrot1.substring(0,i1);
Teljari1 = new Integer(sTeljari1);
teljari1 = Teljari1.intValue();

sNefnari1 = sBrot1.substring(i1+1, j1);
Nefnari1 = new Integer(sNefnari1);
nefnari1 = Nefnari1.intValue();


sBrot2 = tBrot2.getText();
int i2 = sBrot2.indexOf(“/”);
int j2 = sBrot2.length();

sTeljari2 = sBrot2.substring(0,i2);
Teljari2 = new Integer(sTeljari2);
teljari2 = Teljari2.intValue();

sNefnari2 = sBrot2.substring(i2+1, j2);
Nefnari2 = new Integer(sNefnari2);
nefnari2 = Nefnari2.intValue();


teljariSaman = ((teljari1 * nefnari2) + (teljari2 * nefnari1));
nefnariSaman = nefnari1 * nefnari2;

t = teljariSaman;
n = nefnariSaman;

while (n > 0)
{
afg = t % n;
t = n;
n = afg;
}

teljari = teljariSaman / t;
nefnari = nefnariSaman / t;

tFullstytt.setText(teljari + “/” + nefnari);
return true;
}
return false;
}
}

Btw, er með þetta í JCreator Pro 4 ef það breytir einhverju :)