Hi chaps, I am looking for a not too difficult exercise which involves superclasses and subclasses (the java book I am reading doesn't really have any good one). So I was thinking about something like this:
Create a superclass 2DimensionalShapes and 2 subclasses Rectangles and Triangles. I will then need to calculate the area and perimeter of a Rectangles object and a Triangles object.
I am thinking to have the sides declared in the superclass and perhaps the methods in the subclasses, not sure what's best. Do you guys have any suggestion at all as to whether this could be a good exercise and the best way to implement it?
thanks
Violet_82
89
Posting Whiz in Training
Recommended Answers
Jump to PostIt's a reasonable one to start with - small and easy. Just get stuck in and try a few things and see what works best.
Implementation: simple guideline: put everything you can that is used in >1 subclass in the superclass, so you only have to do it once. This …
Jump to PostTechnically, both the triangle and the rectangle can be specified by a point and two vectors ;)
Jump to Postarea and perimeter are ideal candidates for abstract methods in the (abstract) superclass with concrete implementations in each subclass - ie every Shape can calculate and return its area, but exactly how that happens is different in each subclass.
Sides are a bit more interesting... the two sides of a …
Jump to PostProblem there is that the "user" of this class has to know to call calculateArea before calling getArea, and do the same every time the dimensions of the Shape could change. It would be safer and easier to get rid of the area variable and the calculateArea method; just make …
All 16 Replies
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Violet_82
89
Posting Whiz in Training
pritaeas
2,194
¯\_(ツ)_/¯
Moderator
Featured Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
pritaeas
commented:
Good choice for the exercise.
+14
Violet_82
89
Posting Whiz in Training
Violet_82
89
Posting Whiz in Training
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Violet_82
89
Posting Whiz in Training
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Violet_82
89
Posting Whiz in Training
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Violet_82
89
Posting Whiz in Training
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Violet_82
89
Posting Whiz in Training
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Violet_82
89
Posting Whiz in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.