model SphereCyl;

|**********************************************|
|RECOMMENDED WIDTH ********** RECOMMENDED WIDTH|

class Body
expose:
(* Parametric surface *)
u[min]; u[max]; v[min]; v[max ]; (*virtual*)
r[u_, v_]; (*virtual*)

(* Partial differentials of surface *)
ru[u_, v_] := D[r[u1,v1],u1]/. {u1->u,v1->v};
rv[u_, v_] := D[r[u1,v1],v1]/. {u1->u,v1->v};

(* Volume of body *)
V:= 1/3*Integrate[
r[u,v].Cross[ru[u,v], rv[u,v]],
{u, u[min], u[max]},
{v, v[min], v[max]}];

(* Plot the body *)
u[steps] := 15; v[steps] := 15;
Graphics :=
Block[{
p1 = r[u, v][[1]],
p2 = r[u, v][[2]],
p3 = r[u, v][[3]]},
Insert[
ParametricPlot3D[{p1, p2, p3},
{u, u[min], u[max]},
{v, v[min], v[max]},
{PlotPoints->{u[steps], v[steps]}
}]];
end;

class Ring inherits Body
expose:
(* Variables *)
L; (* Width *)

(* Definition of parametric surface *)
(* Look at mathematical syntax *)

u[min] := 0; u[max] := 4;
v[min] := 0; v[max] := 2*Pi;

x1[u_] := Rb + (Ra - Rb)*u;
x2[u_] := Ra;
x3[u_] := Rb + (Ra - Rb)*(3 - u);
x4[u_] := Rb;

x[u_] := x1[u] /; 0 <= u < 1;
x[u_] := x2[u] /; 1 <= u < 2;
x[u_] := x3[u] /; 2 <= u < 3;
x[u_] := x4[u] /; 3 <= u <= 4;

z1[u_] := 1 / 2*L;
z2[u_] := -L*(u - 3 / 2);
z3[u_] := - 1 / 2*L;
z4[u_] := L*(u - 7 / 2);

z[u_] := z1[u] /; 0 <= u < 1;
z[u_] := z2[u] /; 1 <= u < 2;
z[u_] := z3[u] /; 2 <= u < 3;
z[u_] := z4[u] /; 3 <= u <= 4;

(* The complete rotation surface *)
r[u_, v_] := { x[u]*Cos[v],
x[u]*Sin[v],
z[u] };
u[step] := 1; v[step] := Pi/15;
u[steps] := 4; v[steps] := 31;
end;

class Sphere inherits Body
expose:

(* Definition of parametric surface *)
u[min] := 0; u[max] := Pi;
v[min] := 0; v[max] := 2*Pi;
r[u_, v_] := R*{ Sin[u]*Cos[v],
Sin[u]*Sin[v],
Cos[u] };
u[step] := Pi/15; v[step] := Pi/15;
u[steps] := 16; v[steps] := 31;
end;

class Cylinder inherits Ring
expose:
Rb := 0;
u[max] := 3;
end;

instance Body1 inherits Sphere(C1, {Body2})
expose:
rho;(* Density *)
m := rho*V;(* Mass *)
end;

instance Body2 inherits Cylinder(C2, {Body1})
end;