By parts:
$u=t^2\,\,,\,u'=2t\;\;\;,\;\;v'=\cos n\pi t\,\,,\,v=\frac{1}{n\pi}\sin n\pi t\Longrightarrow$
$a_n=\int_{-1}^1 t^2\cos n\pi t\,dt=\left.\frac{t^2\sin n\pi t}{n\pi}\right|_{-1}^1-\frac{2}{n\pi}\int_{-1}^1\,t\sin n\pi t\,dt$
The first summand in the RHS is zero, and for the integral we do again parts:
$u=t\,\,,\,u'=1\;\;\;,\;\;v'=\sin n\pi t\,\,,\,v=-\frac{1}{n\pi}\cos n\pi t\Longrightarrow$
$-\frac{2}{n\pi}\int_{-1}^1\,t\sin n\pi t\,dt=\left.\frac{2}{n^2\pi^2}t\cos n\pi t\right|_{-1}^1-\frac{2}{n^2\pi^2}\int_{-1}^1\cos n\pi t\,dt$
Since now you'll get $\,\sin n\pi t\,$ in the solution of the second integral above, it's easy to see it equals zero, so we finally get (see my comment above):
$\left.\frac{2}{n^2\pi^2}t\cos n\pi t\right|_{-1}^1=\left\{\begin{array} \,\;\;\;\frac{4}{n^2\pi^2}&\,,\text{ if}\,\,n\,\,\text{ is even}\\{}\\-\frac{4}{n^2\pi^2}&\,,\text{ if}\,\,n\,\,\text{ is odd}\end{array}\right.$
Which is exactly what you have.