60 likes | 166 Views
Modula vs Java. MODULE Show; CONST PI = 3.141592653589793238; TYPE PointRc = RECORD x,y : INTEGER; speed : REAL; angle : REAL; END; VAR a,b : PointRc; BEGIN a.x := 0; a.y := 0; a.speed := 12.5; a.angle := PI/2; b.x := 100; b.y := 150; b.speed := 5.0;
E N D
Modula vs Java MODULE Show; CONST PI = 3.141592653589793238; TYPE PointRc = RECORD x,y : INTEGER; speed : REAL; angle : REAL; END; VAR a,b : PointRc; BEGIN a.x := 0; a.y := 0; a.speed := 12.5; a.angle := PI/2; b.x := 100; b.y := 150; b.speed := 5.0; b.angle := 3*PI/4; END Show. publicclass Point { int x,y; double speed; double angle; } publicclass Show { publicstaticvoid main(String args[]) { Point a,b; a = new Point(); a.x = 0; a.y = 0; a.speed = 12.5; a.angle = Math.PI / 2; b = new Point(); b.x = 100; b.y = 150; b.speed = 5.0; b.angle = 3*Math.PI / 4; } }
Public vs Private publicclass Point { publicint x,y; publicprivatedouble speed; publicdouble angle; publicvoid setSpeed(double newSpeed) { if (newSpeed > speed) { System.out.println("we gaan sneller!"); } speed = newSpeed; } publicdouble getSpeed() { return speed; } }
Constructor publicclass Point { publicint x,y; publicdouble angle; privatedouble speed; public Point(int x, int y) { this(x,y,0,0); } public Point(int x, int y, int speed, int angle) { this.x = x; this.y = y; this.speed = speed; this.angle = angle; } publicvoid setSpeed(double newSpeed) { if (newSpeed > speed) { System.out.println("we gaan sneller!"); } speed = newSpeed; } } publicclass Show { publicstaticvoid main(String args[]) { Point a,b,c; a = new Point(0, 0, 12.5, 0.5*Math.PI); b = new Point(100, 150, 5.0, 0.75*Math.PI); c = new Point(200, 400); } }
Inheritance Voertuig class Voertuig { privatedouble lengte; privatedouble breedte; } class Auto extends Voertuig { publicint passagiers; } class Oplegger extends Voertuig { public List goederen; } class Vrachtwagen extends Voertuig { private Oplegger oplegger; publicvoid sluitOpleggerAan(Oplegger o) { oplegger = o; } publicvoid verwijderOplegger() { oplegger = null; } } lengtebreedte Vrachtwagen Oplegger Auto oplegger inhoud passagiers Taxi kost
Inheritance II class Vrachtwagen extends Voertuig { private Oplegger oplegger; publicvoid sluitOpleggerAan(Oplegger o) { oplegger = o; } publicvoid verwijderOplegger() { oplegger = null; } double lengte() { if (oplegger == null) returnsuper.lengte(); else return (super.lengte()+oplegger.lengte()); } double breedte() { if (oplegger == null) returnsuper.breedte(); else return Math.max(super.breedte(),oplegger.breedte()); } } class Voertuig { privatedouble lengte; privatedouble breedte; double lengte() { return lengte; } double breedte() { return breedte; } }
class TolWachtrij { List<Voertuig> rij = new ArrayList<Voertuig>(); double breedte = 2.0; double breedtefactor = 1.2 , lengtefactor = 1.1; public boolean voegtoe(Voertuig v){ if (v.breedte() * breedtefactor > breedte) { System.out.println("deze rij is te smal voor dit voertuig!"); returnfalse; } else { rij.add(v); returntrue; } } public void verwijder(Voertuig v) { rij.remove(v); } public double lengteRij() { double len = 0; for (int i=0; i<rij.size();i++) { Voertuig v = rij.get(i); len += v.lengte() * lengtefactor; } return len; } }