This looks like an assignment to me.Your best interest here is to try to solve it yourself.When you've got a solution, you may post it here and ask for second opinion.The goal is that you learn the basics (these questions are really revolving around the basics). If you just copy someone else's solution and submit that in your assignment, then you will learn nothing and you'll just struggle with future assignments.So go ahead, do some research. Try to write some code.Good luck!
There are plenty of tutorial pages and reference pages for both C++ and Java. I'll include a pair. Feel free to use your preferred tutorial pages instead:
Also, in the solution of this problem I posted a simple C++ class so you can see an example there too.