You have to use Quantiles for this.

Calculate the 50th percentile.

We have 40 students, and we want the 50th percentile.

Now we have to look for the interval where the cumulative frequency is greater than or equal to 20.

That interval is the [21;30) interval.

The percentile formula for grouped data is:

And that is your median

============

Now for the mean:

( are the class midpoints.)