// Stick.java //Chapter 8 OBP material crammed into here. import java.awt.*; //class encapsulates (wraps together) data (attributes) and methods (behaviors) //public so is usable by any other class. //class can be either public or blank (meaning package level access). //If no extends, then implicitly extends Object. public class Stick { //fields: static (class variable) or non-static (instance variable) //static field is per class. one copy shared by all objects private static int sticks=0; //number of instantiated Sticks public static final int STICK_MAX_LENGTH=200; //sort of global constant //fields are the hidden implementation. Per object. private int length; private int width; private Point start; private double angle; //typically, fields are private and methods are public. //Within the class, any field or method can be accessed by name. private final int INCREMENT = 5; //unmodifiable instance variable //overloaded constructors to ensure objects start in consistent state //i.e. are initialized correctly. //no return type. name is same as class. normally are public. //automatically called when object is instantiated/created with new. //can not be explicitly called by other methods. //constructor with no args: public Stick() { //use of this to invoke another constructor: this( 100, 10, new Point(0,0), 90 ); //calls constructor of these args //can only be first statement of a constructor } public Stick( int l, int w, Point s, double ang ) { length = l; width = w; start = s; angle = ang; sticks++; } //C++: copy constructor public Stick( Stick otherStick ) { //object can access private members of another object of same class this( otherStick.length, otherStick.width, otherStick.start, otherStick.angle ); //warning. both sticks now share the same Point object. (shallow copy) //if one changes it, that effects the other } //an "accessor" method. "getter" public int getLength() { return length; } //a "mutator" method. "setter" public void setLength( int newLength ) { //some kind of validity check done to ensure object stays correct if (newLength>0 && newLength