Java Reference
In-Depth Information
-
Give a static function
Point3D add(Point3D p, Point3D q)
that takes as
arguments two points
p
and
q
, and return a
new
point equal to
p
+
q
.
This function shall be inserted inside class
Point3D
.
-
Give a static function
void
scale(Point3D p,
double
k)
that multiplies
the coordinates of point
p
by scalar number
k
.Thatis,
p
becomes
k.p
.
This function shall be located inside class
Point3D
as well.
SOLUTION:
Program 11.1
The class
Point3D
public class
Point3D
{
double
x, y, z;
public
Point3D (
double
x0
,
double
y0
,
double
z0)
{
this
.x = x0;
this
.y = y0;
this
.z = z0;
static double
sqr (
double
x)
}
public static double
distance ( Point3D p, Point3D q)
{
return
Math . sqrt (sqr (q.x
{
return
x
∗
x;
−
p.x) + sqr (q.y
−
p.y)
+sqr(q.z
−
p.z));
}
public static
Point3D add ( Point3D p , Point3D q)
{
return new
Point3D (p.x + q.x, p.y + q.y, p.z + q.z);
}
public static void
scale ( Point3D p,
double
k)
{
p.x
∗
=k;
p.y
∗
=k;
p.z
∗
=k;
}
}
-
Give a class
Atom
that allows us to define atoms with
two
object
fields:
-
center
of type
Point3D
denoting the location (
x, y, z
) of the center of
this atom, and
-
radius
of type
double
that encodes the radius of this atom.
Provide a constructor
Atom(
double
x,
double
y,
double
z,
double
rad)
to this class that initialize objects of this type.
Further, add to this class two constants
H RADIUS = 1.2
and
O RADIUS = 1.5
that represent the radii in angstrom for the hydrogen
and oxygen atoms, respectively.
Search WWH ::
Custom Search