Maximizing profit with uniformly distributed demand The question is as follows: 

Mike makes $40$ dollars for every apple he sells but incurs a net loss of $10$ dollars for every apple he doesn't sell. (If the demand meets or exceeds however many apples he has, he sells all of them.  If the demand is fewer than the number of apples he has, he incurs a loss.)  The random demand for apples, $X$, is uniformly distributed on $[0,5].$  How many apples should Mike make to maximize his expected profit?  

My initial thought was to create a profit function that would represent Mike's profit. Let $a$ be the number of apples that Mike makes.  Then $$f(x)=\begin{cases}
40x-10(a-x) & \text{ if } x < a\\ 
 40a& \text{ if } x \geq a
\end{cases}$$
describes the profit with demand $x.$ . The goal is to maximize $\mathbb{E}[X].$ I tried using the fact that 
$$\mathbb{E}[X]=\int xf(x) \ dx,$$ 
but I ran in to a few questions.  
$\textbf{1.}$ Can I even integrate the profit function I have?  That seems like the correct thing to do, however; $f(x)$ is not even a density.  
$\textbf{2.}$ What role does the random variable $X$ play in this question?  How would I incorporate it?  
Thanks in advance! 
 A: Reiterating my comment:
Indeed you have the right profit function but 1) you can integrate but its not an expectation, as you note no density is at play and 2) a change of notation will elucidate the role $X$: the profit is $Y=f(X,a)$ where $X\sim U(0,5)$ is the given demand and $f:[0,5]\times [0,\infty)\to \mathbb{R}$ is as you have defined (notice the emphasis on a as a variable). Then you want to maximize $\mathbb{E}(f(X,a))=\int_0^5 f(x,a)f_X(x)\mathrm{dx}$ by choosing $a$ appropriately where $f_X$ is the PDF of $X$.
Sketch of the general problem's solution:
Assume a selling price of $s$, a cost of $c$, so a per unit profit of $(s-c)$. We lose $c$ for each left over item unsold (your problem statement then gives $(s-c)=\$ 40$ and $c=\$10$ with $s$ implicitly determined). With demand $X \sim \mathcal{U}(0,b)$, the profit function is $f(X,a)=(s-c)a \mathbb{1}_{X\geq a}+(sX-ca)\mathbb{1}_{X<a}$. Check this is algebraically equivalent to yours and recall $\mathbb{1}_A:=\mathbb{1}_A(x)$ is the indicator function, the piecewise function that is $=0$ for $x\notin A$ and $=1$ for $x\in A$ for some given set $A$.
Now, we must take the expectation of the RV $Y=f(X,a)$, assuming $0\leq a \leq b$. With the PDF of $X$ known to be $f_X(x)=\frac{1}{b} \mathbb{1}_{0<x<b}$, the fact that $\mathbb{E}(k \mathbb{1}_A)=k\mathbb{P}(A)$ for an event $A$ and constant $k$, and finally linearity of expectation, we obtain
$$\mathbb{E}(Y)=(s-c)a\mathbb{P}(X\geq a)+\frac{1}{b}\int_0^a(sx-ca)\mathrm{dx},$$
(we needed $a\in(0,b)$ for the latter integral to be non-zero due to how $f_X$ is defined) which after basic integral calculations and recalling the CDF of a uniform RV, yields,
$$g(a):=\mathbb{E}(Y)=(s-c)a-\frac{s}{2b}a^2.$$
Upon maximizing $g(a)$ by solving $g'(a)=0$ with respect to $a$ and checking this is truly the maximum by your favorite method, we get the optimal stock $a^*=\frac{b(s-c)}{s}.$ In your case with given profit per unit sold $(s-c)=\$40$, cost per unsold unit $c=\$10$, so a selling price $s=\$50$ and the parameter $b=5$ for demand, we finally obtain $a^*=4$ apples as the optimal stock. This yields a max expected profit of $\$80$.
All mistakes are mine own and I invite you to run through these computations yourself as it is always good to get your hands dirty. 
