/* Point.java */ /* simple class definition--we try to model the idea of a geometric point; the class contains two data members (corresponding to the x and y coordinates of the point; these are defined in terms of the primitive Java type "int" (analogous to int in C) and a set of methods that set and retrieve the values of the data members; the data members can only be accessed by methods in this class, the methods can be executed anywhere; this separate of "implementation" and "interface" is commonly seen; so too are these sorts of "accessor" or "getter" and "setter" methods; this class isn't an executable program in its own right */ /* class definitions begin "[access-specifier--commonly 'public', as in this case] class [classname]", followed by the body of the class definition within curly braces */ public class Point { /* the data members; declarations take the form "[access-specifier] [data-type] name"; we'll discuss access-specifiers at length later on, they include public (accessible without restriction), private (accessible only from within this class), protected, and there's also a default access rule associated with the absence of an explicit specifier */ private int x, y; /* and then here are the methods; all methods in Java have the form "[access-specifier] [return-type] [method-name]([parameter list])"; the set* methods all accept input parameters and use them to set the values of the data members ("=", as in C, is the assignment operator in Java); when a method doesn't compute and return a specific value, the return type is marked as "void" in the method definition; the parameter list for a method consists of zero or more comma-separated pairs, the first term of which gives the type of a parameter and the second a local name for it */ public void setPoint(int xVal, int yVal) { x = xVal; y = yVal; } public void setX(int n) { x = n; } public void setY(int n) { y = n; } /* to return a value from a method you say "return [expression]"; the value of the expression must be of the same type that's given as the return type of the method in its signature */ public int getX() { return x; } public int getY() { return y; } }