// your package // import any classes needed from the Java library /** * Documentation - Describe what the class represents and how * (what the attributes are and some methods that are provided) * */ public class ________________________________________________________ { // Declare instance variables and any needed constants. // Instance variables are almost always private; constants // can be (and often are) public and static. // If the class involves random number generation instantiate // a Random generator object (it should be private, static). // Write at least one Constructor to initialize an object - generally // this assigns values to the instance variables. Usually those // values are sent to the constructor through parameters but may // be calculated inside the constructor or set to default values. The // formal parameter list (inside the parentheses) lists the type // followed by the name of each formal parameter with commas // separating the parameters. public _____________________________________________________________ { } // Define each method needed for the class - the structure is // as follows. The returnType is either void (the method does // something but returns no value to the calling program) or // a data type (primitive or object). visibilityModifier returnType methodName (parameterType formalName,...) { // code to accomplish the task of the method - this code // uses the formal parameters (which are visible only inside this // method) and the instance variables. Local variables needed // just for calculations in the method can be declared (basically // a method is a "small" program so it can have own varibles). // Local variables and parameters are visible only inside the // method. // if the return type is NOT void the method must have a // return statement (which should be at the end of the method) return ______________________________________________ ; } } ======================================================================= Using the class in a client program (another class that either represents some other object or that is the main program): 1. Declare an object of the type defined by the class: ClassName objectName; 2. Instantiate the object: objectName = new ClassName ( ... actual parameters go here ...); 3. Invoke methods to operate on the object: objectName.methodName ( ... actual parameters go here ...) For all method calls (including the constructor) the actual parameters must match the formal parameters in type and order. The call DOES NOT include the type - it only includes the actual value of the parameter (a literal, a variable, or a more complex expression). If the method returns a value the call to the method typically is in a place where the value is saved (an assignment statement) or used (an expression or a print statement) If the method has void return type the call is on a line by itself.