OOP Seminarska

Download OOP Seminarska

Post on 08-Nov-2014

122 views

Category:

Documents

3 download

Embed Size (px)

TRANSCRIPT

<p> . - </p> <p> : 1., 2., 3.</p> <p> . . 98/ </p> <p> . -. 2010</p> <p> - </p> <p>: ( ) . : , , P, * , . , , .</p> <p> . . , TVozilo PVozilo, . .</p> <p> package vozila; public abstract class Vozilo { private double sopTezina; public Vozilo (double st) { sopTezina = st; } public abstract char vrsta (); public double tezina () { return sopTezina; } public String toString () { return vrsta() + "(" + sopTezina + ","; } } package vozila;</p> <p>import Citaj.Citaj; public class VozilaT { public static void main (String[] vpar) { Vozilo[] vozila = new Vozilo [100]; int n = 0; raboti: while (true) { System.out.print ("\nTip na vozilo (T,P,*)? "); switch (Citaj.Char ()) { case 't': case 'T': System.out.print ("Sopstvena tezina? "); double sTez = Citaj.Double (); System.out.print ("Tezina na tovarot? "); double ter = Citaj.Int ();; vozila[n++] = new TVozilo (sTez, ter); break; case 'p': case 'P': System.out.print ("Sopstvena tezina? "); sTez = Citaj.Double (); System.out.print ("Sr. tezina na patnicite? "); double srTez = Citaj.Double (); System.out.print ("Broj na patnici? "); int brPatnici = Citaj.Int (); vozila[n++] = new PVozilo (sTez, srTez, brPatnici); break; case '*': break raboti; default: System.out.println ("*** Nepoznat tip na vozilo!"); } } System.out.print ("\nNosivost na mostot? "); double nosivost = Citaj.Double (); System.out.print ("\nMozat da preminat preku mostot:\n"); for (int i=0; i maxs) { maxs = s; vodi = tek.vozilo; } } return vodi; } private synchronized void zastani () { raboti = false; for (Elem tek=prv; tek!=null; tek=tek.sled) tek.vozilo.zastani (); } public void zavrsi () { interrupt (); for (Elem tek=prv; tek!=null; tek=tek.sled) tek.vozilo.zavrsi();</p> <p>} }</p> <p>package trka; import java.awt.*; import java.awt.event.*; public class Trka extends Frame { private static final int BR_VOZILA = 5; private Staza staza; private void popolniProzor () { Panel plo = new Panel (); add (plo, "South"); (staza = new GraStaza (plo)).pocni (); add (plo = new Panel (new GridLayout (0,1)), "Center"); for (int i=0; i vmax) v = vmax; if (v &lt; 0.05*vmax) v = 0.05 * vmax; sleep (100 + (int)(Math.random()*100)); } } catch (InterruptedException g) {} } public void pocni () { start (); } public synchronized void startuvaj () { suma_vdt = 0; t = System.currentTimeMillis (); raboti = true; notify (); } public void zastani () { raboti = false; } public void zavrsi () { interrupt (); } public int id () { return id; } public double v () { return v; } public double s () { return suma_vdt + (System.currentTimeMillis() - t) * v; } public String toString () { return id + " (s=" + (int)s() + ", v=" + (int)v() + ")"; } }</p> <p>package trka; import java.awt.*; import java.awt.event.*; public class GraStaza extends Staza implements ActionListener { private Panel plo; private TextField tksDolzina = new TextField (Double.toString (dolzina), 8); private Button dgmPocni = new Button ("pocni"); private Label oznPobednik = new Label (); public GraStaza (double dolzina, Panel plo) { super (dolzina); popolniPloca (plo); } public GraStaza (Panel plo) { super (); popolniPloca (plo); } private void popolniPloca (Panel plo) { this.plo = plo; plo.add (new Label ("dolzina:", Label.RIGHT)); plo.add (tksDolzina); plo.add (dgmPocni); plo.add (oznPobednik); dgmPocni.addActionListener (this); } public void actionPerformed (ActionEvent d) { dgmPocni.setEnabled (false); oznPobednik.setText (""); plo.validate (); try { dolzina = Double.parseDouble (tksDolzina.getText ()); } catch (NumberFormatException g) {} startuvaj (); } protected void proveri () { super.proveri (); oznPobednik.setText ((gotova ? "Pobednik: " : "Vodi: ") + naCel().id()); plo.validate ();</p> <p>if (gotova) dgmPocni.setEnabled (true); for (Elem tek=prv; tek!=null; tek=tek.sled) tek.vozilo.toString (); } }</p> <p>package trka; import java.awt.*; public class GraVozilo extends Vozilo { private Panel plo; private TextField tksVmax = new TextField (Double.toString(vmax), 3); private Label oznVozilo = new Label (toString()); public GraVozilo (double vmax, Panel plo) { super (vmax); popolniPloca (plo); } public GraVozilo (Panel plo) { super (); popolniPloca (plo); } private void popolniPloca (Panel plo) { this.plo = plo; plo.add (new Label ("vmax:", Label.RIGHT)); plo.add (tksVmax); plo.add (oznVozilo); } public void startuvaj () { try { postavi (Double.parseDouble(tksVmax.getText())); } catch (NumberFormatException g) {} super.startuvaj (); } public String toString () { String s = super.toString (); if (oznVozilo != null) { oznVozilo.setText (s); plo.validate (); } return s; } }</p> <p> , , GraVozilo, GraStaza, Vozilo, Trka Staza, , . , .</p> <p> .1</p> <p>.2</p> <p>: , , , , , . . : . . .</p> <p> , Kalkulator KalkulatorT. Kalkulator , , , ., . KalkulatorT , KalkulatorT.</p> <p>import java.awt.*; import java.awt.event.*; public class Kalkulator extends Frame { private long x, y; private Label[] reg = new Label [2]; private static final int X = 0, Y = 1; private boolean novBroj = true; private int osnova = 10; private Button[] cifri = new Button [16]; private boolean zastani = false; private Panel ploRegistri () { Panel ploca = new Panel (new GridLayout (2, 1)); Color[] podloga = {Color.ORANGE, Color.GRAY}; Color[] bukvi = {Color.BLUE, Color.WHITE}; for (int i=0; i= y; break; } prikazi (); } catch (ArithmeticException g) { reg[Y].setText ("G R E S K A"); } novBroj = true; } } public Kalkulator (int x, int y) { super ("Kalkulator"); setBounds (x, y, 305, 242); setResizable (false); add (ploRegistri(), BorderLayout.NORTH); add (ploCifre(), BorderLayout.CENTER); add (ploOsnova(), BorderLayout.WEST); add (ploOperatori(), BorderLayout.EAST); addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent d) { if (zastani) dispose (); else setVisible (false); } }); } public Kalkulator () { this (250, 50); } public long rezultat () { return x; } public static void main (String[] varg) { Kalkulator kalk = new Kalkulator (); kalk.setVisible (true);</p> <p>kalk.zastani = true; } }</p> <p>import java.awt.*; import java.awt.event.*; public class KalkulatorT extends Frame { Kalkulator kalk = new Kalkulator (420, 50); Label rez = new Label ("", Label.CENTER); private void popolniProzor () { add (rez, "North"); Panel ploca = new Panel (); add (ploca, "South"); Button dugme = new Button ("Otvori"); ploca.add (dugme); dugme.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent d) { kalk.setVisible (true); } }); ploca.add (dugme = new Button ("Procitaj")); dugme.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent d) { rez.setText (Long.toString (kalk.rezultat ())); } }); ploca.add (dugme = new Button ("Zatvori")); dugme.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent d) { kalk.setVisible (false); } }); } private KalkulatorT () { super ("KalkulatorNA"); setBounds (100, 50, 300, 200); popolniProzor ();</p> <p>addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent d) { kalk.dispose (); dispose (); } }); } public static void main (String[] varg) { new KalkulatorT ().setVisible (true); } }</p> <p> , Kalkulator KalkulatorT. . KalkulatorT , , , , . Kalkulator , . .</p> <p> .1</p> <p>.2</p> <p>.3</p>