Found a typo? Is this solution unclear? Let us know here. Please rate my easiness! It's quick and helps everyone guide their studies.
We are on a finite domain, so we anticipate that separation of variables is a reasonable approach to this problem. With this in mind, we attempt a solution of the form $u(x,t)=\psi (x)\phi (t).$ Upon applying the boundary conditions at $x=0$, we get,
$u(0,t)=\psi (0)\phi (t)=0$
which implies for nonzero $\phi (t)$ that we require, $\psi (0)=0$. Similarly at the other endpoint, $x=\pi$, we conclude that $\psi (\pi )=0$. For the homogeneous initial condition on the derivative of $u$,
$u_{t}(x,0)=\psi (x)\phi _{t}(0)=0$
we get that for nonzero $\psi (x)$ that $\phi _{t}(0)=0$. Recall that we can only analyze homogeneous boundary conditions with the technique of separation of variables because they imply conditions on $\psi (x)$ and $\phi (t)$ to be nontrivial. We will deal with the last condition $u(x,0)=f(x)$ at the end. Now, substitute our form for $u(x,t)$ into the PDE, $u_{tt}=u_{xx}$ to get
${\begin{aligned}\psi (x)\phi _{tt}(t)&=\phi (t)\psi _{xx}(x)\\{\frac {\phi _{tt}(t)}{\phi (t)}}&={\frac {\psi _{xx}}{\psi }}=\lambda \end{aligned}}$
with $\lambda$ a constant. This constant comes from the fact that on the left side we have a function only containing the time variable $t$ and on the right side, a function only containing the space variable $x$. Therefore, the only way a function of $t$ can equal a function of $x$ is if they are a constant. The choice of $\lambda$ being negative is in general, arbitrary, but chosen for a reason we will see shortly. We now have the following ODE's to solve:
${\begin{aligned}\psi _{xx}(x)&=\lambda \psi (x),\qquad {}\psi (0)=\psi (\pi )=0\\\phi _{tt}(t)&=\lambda \phi (t),\qquad {}\phi _{t}(0)=0.\end{aligned}}$
We will start by solving the equation for $\psi (x)$ because it has the two boundary conditions required for a complete solution. We start by analyzing different possible values of $\lambda$.
Case 1:$\lambda <0$.
If $\lambda <0$ then
$\psi _{xx}(x)=\lambda \psi (x)$
has solution $\psi (x)=a_{1}\exp({\sqrt {\lambda }}x)+b_{1}\exp({\sqrt {\lambda }}x)$ which after applying the conditions $\psi (0)=\psi (\pi )=0$ results in $a_{1}=b_{1}=0$. Therefore $\lambda <0$ is not permitted if we want to have nontrivial solutions.
Case 2:$\lambda =0$
If $\lambda =0$ then
$\psi _{xx}(x)=\lambda \psi (x)$
has solution $\psi (x)=a_{2}x+b_{2}$ which after applying the conditions $\psi (0)=\psi (\pi )=0$ results in $a_{2}=b_{2}=0$. Therefore $\lambda =0$ is not permitted if we want to have nontrivial solutions.
Case 3:$\lambda >0$
If $\lambda >0$ then
$\psi _{xx}(x)=\lambda \psi (x)$
has solution $\psi (x)=a_{3}\sin({\sqrt {\lambda }}x)+b_{3}\cos({\sqrt {\lambda }}x)$. Applying the condition at $x=0$,
$\psi (0)=a_{2}\sin(0)+b_{3}\cos(0)=b_{3}=0$
and so we conclude that $b_{3}=0$. At the other endpoint, $x=\pi$,
$\psi (\pi )=a_{2}\sin({\sqrt {\lambda }}\pi )=0.$
In order for this condition to be satisfied we require that ${\sqrt {\lambda }}\pi =n\pi$ or that $\lambda =n^{2}$ with $n=1,2,3\dots$, an integer. We must start at $n=1$ because we already concluded that $\lambda =0$ is invalid. Therefore, we conclude (ignoring the constant out front) that,
$\psi (x)=\sin(nx).$
Before, we continue, note that had I taken $\lambda$ instead of $\lambda$ for the original constant, I would have done the same analysis and recovered the same function, only with $\lambda <0$ being the requirement. We choose the constant to be $\lambda$ from the start so that we have positive values of $\lambda$ but this is really just a matter of style. Now for the other differential equation, knowing $\lambda =n^{2}$ we have
$\phi _{tt}=n\phi (t)$
which, recalling $n>0$, has solution, $\phi (t)=a_{4}\cos(nt)+b_{4}\sin(nt)$. Applying the condition that $\phi _{t}(0)=0$ we conclude that $b_{4}=0$ so that,
$\phi (t)=\cos(nt)$
where we once again haven't included the constant out front of the solution. Finally, combining all this information we get that $u(x,t)=A_{n}\sin(nx)\cos(nt)$ but this must hold for all $n$ and therefore, by the principle of superposition,
$u(x,t)=\sum _{n=1}^{\infty }{}A_{n}\sin(nx)\cos(nt).$
Now we know that $u(x,0)=\sum _{n=1}^{\infty }{}A_{n}\sin(nx)=f(x)$ and so we could apply the usual Fourier series orthogonality to get that,
$A_{n}={\frac {2}{\pi }}\int _{0}^{\pi }{}f(x)\sin(nx){\textrm {d}}x$
which will work just fine. However, in this case, $f(x)=2\sin(2x)$ is special because it is one of the eigenfunctions (i.e. it is of the form, $\sin(nx)$ with $n=2$). Therefore, $\sin(2x)$ is orthogonal to every eigenfunction of the form $\sin(nx)$ except for when $n=2$. Therefore, only the coefficient $A_{2}$ will be nonzero and is given by,
$A_{2}={\frac {2}{\pi }}\int _{0}^{\pi }{}2\sin(2x)^{2}{\textrm {d}}x=2.$
If you are unconvinced of how this works, try getting the other coefficients using the formula above and you will quickly see that they are zero! Therefore, we conclude that the solution $u(x,t)$ to the PDE, $u_{tt}=u_{xx}$ is
$\displaystyle {}u(x,t)=2\sin(2x)\cos(2t)$
