Modula vs Java MODULE Show; CONST PI = ; 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. public class Point { int x,y; double speed; double angle; } public class Show { public static void 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 public class Point { public int x,y; public private double speed; public double angle; public void setSpeed(double newSpeed) { if (newSpeed > speed) { System.out.println("we gaan sneller!"); } speed = newSpeed; } public double getSpeed() { return speed; }
Constructor public class Point { public int x,y; public double angle; private double 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; } public void setSpeed(double newSpeed) { if (newSpeed > speed) { System.out.println("we gaan sneller!"); } speed = newSpeed; } public class Show { public static void 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 lengte breedte Auto passagiers Vrachtwagen oplegger Oplegger inhoud Taxi kost class Voertuig { private double lengte; private double breedte; } class Auto extends Voertuig { public int passagiers; } class Oplegger extends Voertuig { public List goederen; } class Vrachtwagen extends Voertuig { private Oplegger oplegger; public void sluitOpleggerAan(Oplegger o) { oplegger = o; } public void verwijderOplegger() { oplegger = null; }
Inheritance II class Voertuig { private double lengte; private double breedte; double lengte() { return lengte; } double breedte() { return breedte; } class Vrachtwagen extends Voertuig { private Oplegger oplegger; public void sluitOpleggerAan(Oplegger o) { oplegger = o; } public void verwijderOplegger() { oplegger = null; } double lengte() { if (oplegger == null) return super.lengte(); else return (super.lengte()+oplegger.lengte()); } double breedte() { if (oplegger == null) return super.breedte(); else return Math.max(super.breedte(),oplegger.breedte()); }
class TolWachtrij { List rij = new ArrayList (); 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!"); return false; } else { rij.add(v); return true; } 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; }