Ласло Краус |
СИ2ОО2 Други колоквијум 6. 5. 2010. |
|---|
Одговорити концизно (по једна или две реченице) и прецизно на следећа питaња:
а) Да ли интерфејс може да садржи променљиве атрибуте и зашто?
б) Како се назива класа која се дефинише у неком изразу и да ли се објекти такве класе могу стварати изван датог израза?
в) Да ли редефинисани метод run() може да баца кориснички дефинисане изузетке и зашто?
Написати на језику Java следећи пакет типова (грешке пријављивати изузецима опремљеним текстовима порука):
Аид(x,y).
(ширина,дужина), у првом реду, иза чега у посебним редовима следе описи садржаних аутомобила.
(10 поена) Написати на језику Java програм који створи аутодром са неколико аутомобила, а када се сви аутомобили зауставе испише на главном излазу аутодром, као и идентификаторе аутомобила који су се сударили са другим аутомобилима. Користити константне параметре (не треба ништа учитавати с главног улаза).
// Pokretan.java
package autodrom;
public interface Pokretan { void pomeriSe(); }
// Automobil.java
package autodrom;
public class Automobil extends Thread implements Pokretan {
public static final int A = 1, X = 2, Y = 3;
private static int posId;
private int id = ++posId;
private double x, y, dx, dy, r;
private long t;
private Autodrom adrom;
public Automobil( Autodrom _a, double _x, double _y,
double _dx, double _dy, double _r, long _t) {
adrom = _a; x = _x; y = _y; dx = _dx; dy = _dy; r = _r; t = _t;
}
public int id() { return id; }
public double x() { return x; }
public double y() { return y; }
public double dx() { return dx; }
public double dy() { return dy; }
public double r() { return r; }
public double t() { return t; }
public void pokreni() { start(); }
public void zaustavi() { interrupt(); }
public void sudaren() { dx = dy = 0; }
public void pomeriSe() { x += dx; y+=dy;
switch (adrom.proveri(this)) {
case A: dx = dy = 0; break;
case X: dx = -dx; break;
case Y: dy = -dy; break;
}
}
public double rastojanje( Automobil a) {
return Math.sqrt(Math.pow(x-a.x,2) + Math.pow(y-a.y,2)) - r - a.r;
}
public void run() {
try {
while (! interrupted()) { sleep( t); pomeriSe(); }
} catch (InterruptedException g) {}
}
public String toString() { return "A" + id + "(" + x + "," + y + ")"; }
}
// GPun.java
package autodrom;
public class GPun extends Exception {
public String toString() { return "*** Autodrom je pun!"; }
}
// GIndeks.java
package autodrom;
public class GIndeks extends Exception {
public String toString() { return "*** Nedozvoljen indeks!"; }
}
// Autodrom.java
package autodrom;
public class Autodrom {
public static final int A = 1, X = 2, Y = 3;
private Automobil[] auti;
private int n;
private double sir, duz;
public Autodrom( double s,double d,int kap){
sir = s; duz = d; auti = new Automobil [kap];
}
public double sir() { return sir; }
public double duz() { return duz; }
public int brAuti() { return n; }
public Autodrom dodaj( Automobil a) throws GPun {
if (n == auti.length) throw new GPun();
(auti[n++] = a).pokreni();
return this;
}
public Automobil auto(int ind) throws GIndeks {
if (ind<0 || ind>=n) throw new GIndeks();
return auti[ind];
}
public synchronized int proveri( Automobil a) {
if (sudar(a)) { zaustaviSve(); return A; }
if (a.x()<a.r() || a.x()>sir-a.r()) return X;
if (a.y()<a.r() || a.y()>duz-a.r()) return Y;
return 0;
}
private boolean sudar( Automobil a) {
boolean ima = false;
for (int i=0; i<n; i++)
if (auti[i]!=a && a.rastojanje( auti[i])<=0) {
auti[i].sudaren(); ima = true;
}
return ima;
}
private void zaustaviSve() {for (int i=0; i<n; auti[i++].zaustavi());}
public void cekajSudar() throws InterruptedException {
for (int i=0; i<n; auti[i++].join());
}
public String toString() {
String s = "(" + sir + "," + duz + ")";
for (int i=0; i<n; s+="\n"+auti[i++]);
return s;
}
}
// Program.java
import autodrom.*;
public class Program {
public static void main(String[] vpar) {
try {
Autodrom adrom = new Autodrom( 1000, 800, 20);
adrom.dodaj( new Automobil(adrom, 10, 10, 5, 3, 10, 100));
adrom.dodaj( new Automobil(adrom, 900, 300, -3, -5, 20, 100));
adrom.dodaj( new Automobil(adrom, 500, 500, 4, -2, 10, 100));
adrom.dodaj( new Automobil(adrom, 990, 10, -6, 2, 10, 100));
adrom.dodaj( new Automobil(adrom, 10, 780, 5, -3, 10, 100));
adrom.cekajSudar();
System.out.println( adrom + "\n");
for (int i=0; i<adrom.brAuti(); i++)
if (adrom.auto(i).dx() == 0 && adrom.auto(i).dy() == 0)
System.out.print( adrom.auto(i).id()+" ");
System.out.println();
} catch (Exception g) { System.out.println( g); }
}
}
(1000.0,800.0)
A1(205.0,127.0)
A2(783.0,105.0)
A3(656.0,422.0)
A4(762.0,86.0)
A5(205.0,663.0)
2 4
(⇒ почетак)
Copyright © 2011, Laslo Kraus
Последња ревизија: 17.3.2011.