Source of Thing.java


  2: /**
  3:  * A class that counts how many objects of its type have been created.
  4:  *
  5:  * @author Mark Young (A00000000)
  6:  */
  7: public class Thing {

  9:     // instance variables
 10:     public final int ID;
 11:     private String name;
 12:     private String location;

 14:     // counter for how many Things have been created
 15:     private static int numThings = 0;

 17:     /**
 18:      * Create a Thing with a name and location.
 19:      *
 20:      * @param reqName the name of the Thing.
 21:      * @param reqLocn the location of the Thing.
 22:      */
 23:     public Thing(String reqName, String reqLocn) {
 24:         // brand new thing being created
 25:         ++numThings;

 27:         // ID for this Thing is its position in the order Things have been
 28:         // created -- i.e. 1 for the first Thing, 2 for the second, and so on.
 29:         ID = numThings;
 30:         name = reqName;
 31:         location = reqLocn;
 32:     }

 34:     /**
 35:      * Create a Thing with a name but no location.
 36:      *
 37:      * @param reqName the name of the Thing.
 38:      */
 39:     public Thing(String reqName) {
 40:         this(reqName, null);
 41:     }

 43:     /**
 44:      * Create a Thing with no name or location.
 45:      */
 46:     public Thing() {
 47:         this(null, null);
 48:     }

 50:     /**
 51:      * Get this Thing's name.
 52:      *
 53:      * @return this Thing's name, if it has one (null if not).
 54:      */
 55:     public String getName() {
 56:         return name;
 57:     }

 59:     /**
 60:      * Get this Thing's location.
 61:      *
 62:      * @return this Thing's location, if it has one (null if not).
 63:      */
 64:     public String getLocation() {
 65:         return location;
 66:     }

 68:     /**
 69:      * Make a String to represent this Thing, using the name and location if
 70:      * possible; ID if not.
 71:      */
 72:     @Override
 73:     public String toString() {
 74:         if (location != null) {
 75:             return "The " + name + " in the " + location;
 76:         } else if (name != null) {
 77:             return name;
 78:         } else {
 79:             return "Thing #" + ID;
 80:         }
 81:     }

 83: }