class Point { int x; int y; Point (int x1, int y1) { x = x1; y = y1; } int distanceToOrigin() { return sqrt(x * x + y * y); } }
class Line { Point pt1; Point pt2; Line (Point p, Point q) { pt1 = p; pt2 = q; } }
class Line { ... Line (Point p, Point q) { if ((p.x == q.x) && (p.y == q.y)) { System.exit(0); } pt1 = p; pt2 = q; } ... }Meglio cosí
class Point { ... boolean equals (Point pt) { return ((x == pt.x) && (y == pt.y)); } ... } class Line { ... Line (Point p, Point q) { if (p.equals(q)) System.exit(0); pt1 = p; pt2 = q; } .... }
class Point { int x; int y; static int scaling = 1; Point (int x1, int y1) { x = x1; y = y1; } int relativeX() { return x * scaling; } int relativeY() { return y * scaling; } }
Point pt = new Point(3, 4); pt.scaling = 5;equivalente a
Point.scaling = 5;Dopo
Point pt1 = new Point(3, 4), pt2 = new Point(4, 5); int x1 = pt1.relativeX(); pt1.scaling = 2; int x2 = pt1.relativeX(); int x3 = pt2.relativeX();x1 vale 3, x2 vale 6, x3 vale 8.
int distanceToOrigin() { return sqrt(x * x + y * y); }
boolean equals(Point pt) { return x == pt.x & y == pt.y; }
void setX(int x1) { x = x1; return; }
Errore:
int method () { int bar; bar += 1; ... }Errore:
int method () { int bar; if (cond) { bar = 1; } bar += 1; .... }
class Obj { int x; int method () { int x; x = 1; ... } }
class Obj { int x; int method () { int x; this.x = 1; ... } }
class Point { int x, y; static int scaling = 1; static int getScaling() { return scaling; } }
Point pt = new Point(3, 4); int s = pt.getScaling();
int s = Point.getScaling();
class Print { static String toStringPt(Point pt) { return "(" + pt.x + ", " + pt.y + ")"; } static String toStringLine(Line l) { return "[" + toStringPt(l.pt1) + ";" + toStringPt(l.pt2) + "]"; } }
class Print { static String toString(Point pt) { return "(" + pt.x + ", " + pt.y + ")"; } static String toString(Line l) { return "[" + toString(l.pt1) + ";" + toString(l.pt2) + "]"; } }
class Point { int x; int y; Point (int x1, int y1) { x = x1; y = y1; } }
Point pt = new Point(3, 4);
class Point { int x; int y; Point (int x1, int y1) { x = x1; y = y1; } Point () { x = 0; y = 0; } Point (int x) { this.x = x; y = x; } }
Point pt1 = new Foo(); Point pt2 = new Foo(1);
class Point { int x, y; Point (int x1, int y1) { x = x1; y = y1; } Point () { this(0, 0); } Point (int x) { this(x, x); } }
per boolean è false
per int è 0
per char è \0
per oggetto è null
Costruttore di default:
class Point { ... Point () { } ... }
class Obj { int[] i = new int[2]; { i[0] = 2; i[1] = 4; } }
class Obj { static int[] i = new int[2]; static { i[0] = 2; i[1] = 4; } }
C2.
Scrivere per Point e Line un metodo equals tale che
pt1.equals(pt2) ritorna true se i due punti coincidono,
l1.equals(l2) ritorna true se le due linee coincidono.
C3. Scrivere una class Prime che ha un metodo statico isPrime. Questo metodo prende un numero intero n e ritorna true se n è un numero primo. L'implementazione di isPrime deve usare la tecnica descritta nell'esercizio B5. Per questo, la class Prime ha un campo statico primes che contiene i primi p numeri primi, con p arbitrario. Primo, il metodo isPrime deve assicurarsi che il campo statico primes sia sufficientemente grande per decidere se n è primo. Secondo, il metodo deve utilizzare il campo primes per verificare se un elemento di primes divide n.
C4*. Scrivere una classe Matrix che rappresenta le matrici di numeri interi. Tale classe ha: