Well you should already know almost all you need to get this one working. You know to override the function? Fine.
1. Make the
parent class for classes rectangle and triangle. It should have two protected doubles. Now make the constructor which takes two double values and initializes these two variables. Fine. Now make two public functions: perimeter and area, which take no parameters but return double value. Make their bodies and let them return allways 0.
2. Now make two new public classes:
rectangle and
triangle, which inherit the class parent. In both classes you should implement:
- default constructor, which call only
super(0,0);
- constructor which takes two values e.g. a and b and call olny
super(a,b);.
- the function perimeter.
- the function area.
When you think you are done, make the main class with
public static void main function and run the following code
If it compiles and writes:
You've done it well.
After you've done it, you can try to use
abstract classes and methods.
Ok. For this one I'll give you how the start should look like.
Oh yes, I forgot: to get sqrt from some number, use
Math.sqrt.
And if you wonder what is
super, klick the link in this line.
Good luck.