01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package types.point5;
import java.awt.Color;
interface Colored {
Color getColor();
}
interface Point {
double getX();
double getY();
}
interface ColoredPoint
extends Colored, Point
{ }
final class CartesianPoint implements ColoredPoint {
private final double x;
private final double y;
private final Color color;
public CartesianPoint(double x, double y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
public double getX() { return x; }
public double getY() { return y; }
public Color getColor() {
System.out.println("It's Cartesian!");
return color;
}
}
final class PolarPoint implements ColoredPoint {
private final double theta;
private final double r;
private final Color color;
public PolarPoint(double theta, double r, Color color) {
this.theta = theta;
this.r = r;
this.color = color;
}
public double getX() { return r*Math.cos(theta); }
public double getY() { return r*Math.sin(theta); }
public Color getColor() {
System.out.println("It's Polar!");
return color;
}
}
public class Main {
private Main() {}
public static void printColoredPoint(ColoredPoint cp) {
System.out.println(cp.getX());
System.out.println(cp.getY());
System.out.println(cp.getColor());
}
public static void main(String[] args) {
printColoredPoint(new CartesianPoint(0,0,Color.RED));
printColoredPoint(new PolarPoint(0,0,Color.RED));
}
}
|