Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (anonymous):

(Java) Specify, design, and implement a class that can be used to keep track of the position of a location in three-dimensional space.

OpenStudy (anonymous):

Let's start with a few hints: for 3-dimensional space, how many coordinates would you need to describe the position of a point? Each of those would take the form of a scalar. For such a class, you'll likely want to be able to read and write the respective coordinates - update them when the position of the point changes, read them to find out where it is...

OpenStudy (anonymous):

You would need 3 coordinates, xyz.

OpenStudy (anonymous):

Exactly! So, those would be data members of your class. Then you'll need methods to access and modify them.

OpenStudy (anonymous):

This would be a basic class to specify a point. Though it might be useful to make the members public and not use the getter setter methods. class threedcoord { private int x = 0; private int y = 0; private int z = 0; int getx() { return x; } int gety() { return y; } int getz() { return z; } void setx(int inp) { x = inp; } void sety(int inp) { y = inp; } void setz(int inp) { z = inp; } }

OpenStudy (anonymous):

while it might seem useful, and may be ok for a small class like this, you should be extremely careful with making data members public. if you ever need to add functionality like logging everytime the position is changed, you would have to change the class and *every instance in your code where you access one of the public members*. once you've written a couple of thousand lines of code using your class in various places, this can become difficult. public data members should only be used in very specific circumstances - some say, data members should never be public.

OpenStudy (anonymous):

Oh I see now. Thanks for the responses!

OpenStudy (llib_xoc):

I absolutely agree with opiesche. What if you write the Point class, refer to the coordinates in lots of places, the decide that the coords need to be *float* numbers? You'll have to change all the references! A great mantra for programming is to organize your code so that you rarely need to change anything in more than one place.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!