interface NumberProperty {
public double Magnitude();
};
interface ObjectProperty {
public String ObjectType();
};
interface CommonInterfaces extends NumberProperty, ObjectProperty {
};
class Vector2D implements CommonInterfaces {
private double ex1,ey1,ex2,ey2;
Vector2D(double x1, double y1, double x2, double y2)
{
ex1 = x1;
ey1 = y1;
ex2 = x2;
ey2 = y2;
};
public double Magnitude()
{
double mag;
mag = Math.sqrt((ex2-ex1)*(ex2-ex1) + (ey2-ey1)*(ey2-ey1));
return mag;
};
public String ObjectType()
{
return "Vector2D";
}
};
class Vector3D implements CommonInterfaces{
private double ex1,ey1,ez1,ex2,ey2,ez2;
Vector3D(double x1, double y1, double z1, double x2, double y2, double z2)
{
ex1 = x1;
ey1 = y1;
ez1 = z1;
ex2 = x2;
ey2 = y2;
ez2 = z2;
};
public double Magnitude()
{
double mag;
mag = Math.sqrt((ex2-ex1)*(ex2-ex1) + (ey2-ey1)*(ey2-ey1) + (ez2-ez1)*(ez2-ez1));
return mag;
};
public String ObjectType()
{
return "Vector3D";
}
};
public class InterfaceEx03 {
public static void main(String[] args) {
CommonInterfaces[] Objects = new CommonInterfaces[] {
new Vector2D(0.0,0.0,1.0,1.0),
new Vector3D(0.0,0.0,0.0,1.0,1.0,1.0)
};
for (int i = 0; i < Objects.length; i++)
{
System.out.println(Objects[i].ObjectType());
System.out.println(Objects[i].Magnitude());
}
}
}
|