The upper density of an automatic set is rational

Given a natural number $k\ge 2$ and a $k$-automatic set $S$ of natural numbers, we show that the lower density and upper density of $S$ are recursively computable rational numbers and we provide an algorithm for computing these quantities. In addition, we show that for every natural number $k\ge 2$ and every pair of rational numbers $(\alpha,\beta)$ with $0<\alpha<\beta<1$ or with $(\alpha,\beta)\in \{(0,0),(1,1)\}$ there is a $k$-automatic subset of the natural numbers whose lower density and upper density are $\alpha$ and $\beta$ respectively, and we show that these are precisely the values that can occur as the lower and upper densities of an automatic set.