+import java.util.*;
-class CeroDenExc extends Exception {
-
- private int num;
-
- public int getNum() {
- return num;
- }
-
- public void setNum(int n) {
- num = n;
- }
-
- public CeroDenExc(int num) {
- setNum(num);
- }
-
- public String toString() {
- return "Division por 0 (numerador = " + getNum() + ")";
- }
-
-}
-
-interface OperFrac {
-
- public Fraccion mas(Fraccion f);
-
- public Fraccion menos(Fraccion f);
-
-}
-
-interface ComparaFrac {
-
- public int compareTo(Fraccion f);
-
-}
-
-class Fraccion implements OperFrac, ComparaFrac {
+public class Fraccion extends Observable implements OperFrac, ComparaFrac {
private int num;
private int den;
public void setNum(int n) {
num = n;
+ setChanged ();
+ notifyObservers ();
}
public void setDen(int d) {
den = d;
+ setChanged ();
+ notifyObservers ();
}
public Fraccion() {
int d = mcd(getNum(), getDen());
setNum(getNum() / d);
setDen(getDen() / d);
- }
-
- // Test
- public static void main(String args[]) throws CeroDenExc {
- try {
- Fraccion f = new Fraccion(10, 0);
- } catch (CeroDenExc e) {
- System.out.println("Ahhh! Esta rota la fraccion!"
- + " tiene numerador " + e.getNum() + " y divisor 0");
- }
- Fraccion f1 = new Fraccion(2, 10);
- System.out.println("f1 = " + f1);
- System.out.println("f1 + f1 = " + f1.mas(f1));
- Fraccion f3 = f1.menos(new Fraccion(100, 4));
- System.out.println("f3 = " + f3);
- System.out.println("f3 comparado a f1 = " + f3.compareTo(f1));
- //System.out.println("Java esta roto en el manejo de excepciones!!!");
+ setChanged ();
+ notifyObservers ();
}
}