Finding mean and variance from given density function Got this question and I don't know which path to take in answering the question.

$$f(x)=\begin{cases}k&,\text{ if }0<x<1\\\frac{1}{2}k(3-x)&,\text{ if }1<x<3\\0&,\text{ otherwise }\end{cases}$$
Find $k$ and the mean and variance of $X$.

My attempt:
I got $k$ as $1/2$ by integrating $k$ from $0$ to $1$ and then adding the integral of $\frac{1}{2}k(3-x)$ from $1$ to $3$ and equating it to $1$.
My issue is the mean and variance. Do I calculate separate mean and variances for the different functions for which X is defined for eg calculate mean and variance for $f(x)=k$ and then calculate mean and variance for $f(x)=\frac{1}{2}k(3-x)$?
 A: The solutions are
$$k^{-1} = \int_0^3 f(x) \text{d}x=k \int_0^1 f(x) \text{d}x + k \int_1^3 f(x) \text{d}x$$
$$\mathbb{E}[X]= k \int_0^3 xf(x) \text{d}x=k \int_0^1 xf(x) \text{d}x + k \int_1^3 xf(x) \text{d}x$$
$$\mathbb{E}[X^2]= k \int_0^3 x² f(x) \text{d}x= k \int_0^1 x^2 f(x) \text{d}x + k \int_1^3 x^2 f(x) \text{d}x$$
If you want to solve it the suggested way, you need to define
$$f_1(x)=\mathbb{I}_{(0,1)}(x)\qquad f_2(x)=\frac{3-x}{2}\mathbb{I}_{(1,3)}(x)$$
normalise each of these functions into densities
$$f_1(x)=k_1\mathbb{I}_{(0,1)}(x)\qquad f_2(x)=k_2\frac{3-x}{2}\mathbb{I}_{(1,3)}(x)$$
and compute
\begin{align*}
\mathbb{E}[X] &= \frac{k}{k_1} \mathbb{E}[X_1] + \frac{k}{k_2} \mathbb{E}[X_2] \\
\mathbb{E}[X^2] &= \frac{k}{k_1} \mathbb{E}[X_1^2] + \frac{k}{k_2} \mathbb{E}[X_2^2] 
\end{align*}
which is a fairly convoluted way to reach the conclusion.
A: $$
f(x) = \begin{cases}
k & 0<x<1 \\
{1 \over 2} k(3-x) & 1 < x< 3
\end{cases}
$$
By $\int_0^3 f(x)=1$ we get $k = {1 \over 2}$.
Then the mean 
$$
\begin{align}
\mathbb{E}(X) & = \int_0^3 xf(x)dx  \\
& = {1 \over 2} \int_0^1 xdx + {1 \over 4} \int_1^3 (3-x)xdx \\
& = {13 \over 12}
\end{align}
$$
And the variance 
$$
\begin{align}
\mathrm{Var}(X) & = \mathbb{E}[X^2] -\mathbb{E}^2[X] \\
& = {1 \over 2} \int_0^1 x^2 dx + {1\over 4} \int_1^3 (3-x)x^2 dx
-\mathbb{E}^2[X] \\
& = {71 \over 144}
\end{align}
$$
