Suppose a solid body approaches a sphere of radius $R = 1$ and height $z$, how do I calculate the angle $\theta$ at which the body bounces off the sphere?

I am writing a java code where the user will input the height, z, at which a solid body approaches a sphere of radius $R$ and outputs the angle $\theta$ at which the body bounces off the sphere.
I know that I can break down the velocity vector of the solid body into two components where one is tangential to the sphere, another one points to the centre of the sphere and then use angle of incidence equals angle of reflection, but I do not know how to apply correctly here.
Assuming a perfectly elastic collision with a fixed (immovable) sphere, the angle ($\theta$) the body will bounce off the sphere is $2cos^{-1}(z)$.
Let $z$ be the $y$ coordinate and $x$ be the $x$ coordinate of a standard Cartesian plane.
$x^2+z^2=1$ describes the circle in question (I know you said sphere, but the diagram you gave is in two dimensions). WLoG have the circle centered at the origin, $(0,0)$.
The slope of the tangent to any point on the circle is $\frac{dz}{dx}=-\frac{x}{z}$.
The angle $\theta$ we are looking for is twice the angle of incidence since $\theta$ is comprised of the sum of the angle of reflection and the vertical angle of the angle angle of incidence, both of which have the same measure as the angle of incidence. (This can be easily seen by sketching the tangent line at the collision point.)
So what is the angle of incidence? Imagine a right triangle with one leg on the path of the incident object, one leg perpendicular to this path, and the hypotenuse on the tangent line at the point of collision.
Using the slope found above and letting the leg on the path of incidence have length $z$, we have the other leg length $x$ and the hypotenuse length $\sqrt{x^2+z^2}=1$. Thus, letting $\phi$ be the angle of incidence, we have $cos(\phi)=\frac{z}{1}=z$.
Finally, we have $\theta=2\phi=2cos^{-1}(z)$.