Prethodna | Nadređena | Naredna

Sistem klasa u jeziku Java

Sadržaj


Tačke u ravni

// Tacka.java - Definicija klase tacaka (Tacka).
class Tacka {
  private double x, y;
  public Tacka () { x = y = 0; }
  public Tacka (double xx) { x = xx; y = 0; }
  public Tacka (double xx, double yy) { x = xx; y = yy; }
  public Tacka (Tacka t) { x = t.x; y = t.y; }
  public double aps () { return x; }
  public double ord () { return y; }
  public void citaj ()
    { x = Usluge.citajDouble (); y = Usluge.citajDouble (); }
  public String toString () { return "(" + x + "," + y + ")"; }
  public static final Tacka ORG = new Tacka ();
}

Geometrijske figure u ravni

// Figura.java - Definicija apstraktne klase figura (Figura).
abstract class Figura {
  protected Tacka T;
  public Figura ()        { this.T = new Tacka () ; }
  public Figura (Tacka T) { this.T = new Tacka (T); }
  public void postavi (double xx, double yy) { T = new Tacka(xx, yy); }
  public void pomeri  (double dx, double dy)
    { T = new Tacka (T.aps()+dx, T.ord()+dx); }
  public abstract Figura kopija ();
  public abstract double O     ();
  public abstract double P     ();
  public abstract void   citaj ();
  public abstract String toString ();
}

Krugovi u ravni

// Krug.java - Definicija klase krugova (Krug).
class Krug extends Figura {
  private static final double PI = 3.14159265359;
  double r;
  public Krug ()                    { super ()  ; r = 1 ; }
  public Krug (double rr)           { super ()  ; r = rr; }
  public Krug (double rr, Tacka tt) { super (tt); r = rr; }
  public Figura kopija () { return new Krug (r, T); }
  public double O () { return 2 * r * PI; }
  public double P () { return r * r * PI; }
  public void citaj () { T.citaj (); r = Usluge.citajDouble (); }
  public String toString () { return "krug [T=" + T + ", r=" + r +
                                     ", O=" + O() + ", P=" + P() + "]"; }
}

Kvadrati u ravni

// Kvadrat.java - Definicija klase kvadrata (Kvadrat).
class Kvadrat extends Figura {
  double a;
  public Kvadrat ()                    { super ()  ; a = 1 ; }
  public Kvadrat (double aa)           { super ()  ; a = aa; }
  public Kvadrat (double aa, Tacka tt) { super (tt); a = aa; }
  public Figura kopija () { return new Kvadrat (a, T); }
  public double O () { return 4 * a; }
  public double P () { return a * a; }
  public void citaj () { T.citaj (); a = Usluge.citajDouble (); }
  public String toString () { return "kvadr[T=" + T + ", a=" + a +
                                     ", O=" + O() + ", P=" + P() + "]"; }
}

Trouglovi u ravni

// Trougao.java - Definicija klase kvadrata (Trougao).
class Trougao extends Figura {
  double a, b, c;
  public Trougao ()                    { super ()  ; a = b = c = 1 ;}
  public Trougao (double aa)           { super ()  ; a = b = c = aa;}
  public Trougao (double aa, Tacka tt) { super (tt); a = b = c = aa;}
  public Trougao (double aa, double bb)
   { super ()  ; a = aa; b = c = bb; }
  public Trougao (double aa, double bb, Tacka tt)
   { super (tt); a = aa; b = c= bb; }
  public Trougao (double aa, double bb, double cc)
   { super ()  ; a = aa; b = bb; c = cc; }
  public Trougao (double aa, double bb, double cc, Tacka tt)
   { super (tt); a = aa; b = bb; c = cc; }
  public Figura kopija () { return new Trougao (a, b, c, T); }
  public double O () { return a + b + c; }
  public double P () {
    double s = (a + b + c) / 2;
    return Math.sqrt (s * (s-a) * (s-b) * (s-c));
  }
  public void citaj () { T.citaj (); a = Usluge.citajDouble ();
    b = Usluge.citajDouble (); c = Usluge.citajDouble ();
  }
  public String toString () { return "troug[T=" + T + ", a=" + a +
           ", b=" + b + ", c=" + c + ", O=" + O() + ", P=" + P() + "]"; }
}

Ispitvanje klasa za geometrijske figure u ravni

// Figuret.java - Ispitivanje klasa za geometrijske figure.
class Elem {
  Figura fig; Elem nar;
  Elem (Figura f)         { fig = f.kopija (); nar = null; }
  Elem (Figura f, Elem n) { fig = f.kopija (); nar = n; }
}
class Figuret {
  public static void main (String[] argi) {
    Elem prvi=null, posl=null; Figura fig = null;
    for (boolean dalje=true; dalje; ) {
      System.out.print ("Figura? ");
      char ff = (char)Usluge.citajString (). getBytes ()[0];
      switch (ff) {
        case 'o': fig = new Krug    (); break;
        case 'k': fig = new Kvadrat (); break;
        case 't': fig = new Trougao (); break;
        default : dalje = false;
      }
    if (! dalje) break;
      fig.citaj (); Elem novi = new Elem (fig);
      if (posl != null) posl.nar = novi; else prvi = novi;
      posl = novi;
    }
    for (Elem tek=prvi; tek!=null; tek=tek.nar)
      System.out.println (tek.fig);
  }
}

Primer rezultata rada

Figura? o 0 0 10
Figura? k 1 1 3
Figura? t 7 2 3 4 5
Figura? t 1 9 1 1 1
Figura? k -2 -4 33
Figura? o -1 0 2.5
Figura? t 7 -2 3 5 5
Figura? x
krug [T=(0.0,0.0), r=10.0, O=62.8318530718, P=314.159265359]
kvadr[T=(1.0,1.0), a=3.0, O=12.0, P=9.0]
troug[T=(7.0,2.0), a=3.0, b=4.0, c=5.0, O=12.0, P=6.0]
troug[T=(1.0,9.0), a=1.0, b=1.0, c=1.0, O=3.0, P=0.4330127018922193]
kvadr[T=(-2.0,-4.0), a=33.0, O=132.0, P=1089.0]
krug [T=(-1.0,0.0), r=2.5, O=15.70796326795, P=19.6349540849375]
troug[T=(7.0,-2.0), a=3.0, b=5.0, c=5.0, O=13.0, P=7.1545440106270926]

Autor: Laslo Kraus
E-pošta: kraus@etf.rs

Copyright © 1998, Laslo Kraus
Poslednja revizija: 13.6.1998.