posted 14 years ago
You need to decide where you do and where you don’t want constructors.Now you can set speed and direction. Note the actions taken to ensure both speed and direction are within certain ranges.
Yes, this is relevant, because you now have a superclass with two int fields. You can forget about the Exception and Math.abs and the 360°, since you have no limits to the values you permit.
Now rewrite the Vehicle class, miss out all the bits you don’t need, and compare it with your Figure class.
You want constructors which will set up those two fields, so you write a constructor which sets up those two fields. You don’t want default values, so you get rid of any other constructors. You may have to amend other code which uses those constructors, which you are getting rid of. You do not want a constructor like this, because you do not want to default to all your vehicles doing 97mph eastward:If you found a constructor like that in the Vehicle class, and called new Bus(); anywhere, you get the default values. So you don’t want either of those constructors. If you have a constructor which gives you defaults you don’t want, get rid of it. That’s the only way to ensure nobody calls it by mistake.
Now let’s look at a possible subclassNote there is no turn method, nor speedUp, nor getSpeed, nor getDirection. Those are inherited from the Vehicle class. The only place you use super. is in the toString method, so you are adding “A Bus travelling at 97mph in direction 90°” to “with 127 passengers.”
And yes, it will say Bus, not Vehicle.
So we have a Bus class which inherits those features from its superclass. Now see if you can’t turn my Bus class into your Circle class. Only give it the constructors you want. Then you must pass parameters to those constructors, defining the x, y locations