Java Reference
In-Depth Information
cells, we display the array, and perform again a permutation to leave
the cells as before. Here, k = 0 and all values are successively put
in first position. Notice that when the mysterious function ends, the
array order is thus reset to the original order.
- In the function main , we now replace the function call mysterious(t, 0);
by mysteriousRecursive(t, 0); .
After having recompiled the program, what is the console result
obtained by launching java MysteriousProgram 3 ?
Describe precisely the program execution steps and the obtained result
in general case (any given n > 0 )
SOLUTION:
For n = 3, calling mysteriousRecursive(t, 0); yields the following
output:
123
132
213
231
321
312
The program displays all permutations of the array. Remark: The
recursion always terminates since the body of the for loop is executed
only for k > tab.length-1 . Proof goes by induction.
Solution 11.2 (Modeling molecules)
In this exercise, we are first concerned with modeling molecules as arrays
of atoms, where each atom is defined as a proper 3D sphere with a center
and a radius (the Van der Waals radius). We will then study how to
detect whether atoms and molecules collide or not.
- Design a class Point3D where each object is defined as a 3D point
with coordinates x , y and z , all of type double . Further, provide this
class with a constructor Point3D( double x0, double y0, double z0)
that allows us to initialize a Point3D object.
- Write a static function double distance(Point3D p, Point3D q) that
takes as arguments two points p and q , and returns the Euclidean
distance
between them. In order to compute the square func-
tion, we will use static double sqr( double x) { return x x; } ,whichis
also inserted inside the class Point3D .
q
p
To compute the square root, we'll use function static double
sqrt( double x) of the Math class.
 
Search WWH ::




Custom Search