/** * Class Biscuit is an example of an object * NB: Our American friends would call this a Cookie * * I'm using this as a teaching example so there are many redundant comments * Each student in the group is developing their own Class in parallel * * General hints and tips on style are given at the end of the Class * * @author Mr J * @version 1.0 */ public class Biscuit { // data members private char shape; // the biscuit shape - coded, eg R = round private String taste; // well, taste private double price; // um, the price private int unit; // the number of biscuits the price applies to private boolean myFavourite; // true if this is my favourite biscuit /** * "No argument" constructor sets the values arbitrarily */ public Biscuit() { // special biscuit with "no" properties shape = 'x'; taste = "None"; price = 0.0; unit = 0; myFavourite = false; } // Mutator methods, change a data member /** * This method sets the shape of the Biscuit * * @param the shape type of the biscuit (eg R = round, S = square) */ public void setShape(char shape) { // We'll make sure our shape is represented by a capital // letter. If the shape is not one of our "recognised" // acceptable shapes, we'll set it to "other" shape = Character.toUpperCase(shape); if ( (shape == 'R') || (shape == 'S') ) { // The this keyword is used to make sure we are referring to // the Class data member not a local variable of the same name. // It's not essential but just good practice. this.shape = shape; } else { // set it to "Other" this.shape = 'O'; } } /** * This method sets the taste of the Biscuit * * @param the taste description of the biscuit */ public void setTaste(String taste) { // It is not sensible to validate this property // of a biscuit. (Any String will be accepted) this.taste = taste; } /** * This method sets the price of the Biscuit * * @param the price of the biscuit ( > 0 and < 100) */ public void setPrice(double price) { // (A price between 0 and 99.99 seems sensible) if ( (price > 0.0) && (price < 100.0) ) { this.price = price; } else { // back to our "default value" this.price = 0.0; } } /** * This method sets the unit of the Biscuit * The unit is the base number the price applies to, eg * A pack of 24 biscuits. * * @param the unit of the biscuit ( single, or size of pack, for example) */ public void setUnit(int unit) { // (A unit between 1 and 100 seems sensible) if ( (unit > 0) && (unit <= 100) ) { this.unit = unit; } else { // back to our "default value" this.unit = 0; } } /** * This method defines whether this is my favourite biscuit * As you may know, I'm a Douglas Adams fan. Therefore, in * the interests of introducing a certain mild tension I * won't tell you what my favourite biscuit is just yet :-) * * @param whether this biscuit is my favourite. */ public void setMyFavourite(boolean iLikeThisOne) { // I've used a differently named parameter, just to show I can this.myFavourite = iLikeThisOne; } } /** * Elements of good style (recommended by the language designers): * * Please use an initial capital letter for the Class name. * Use lowercase letters for the data members and methods * Use internal capitals rather than underscores: myFavourite, not my_favourite * Use consistent indentation schemes * Always add comments * * Recommended by me: * * Keep the data members and methods in the same order, ie if shape is * your first data member then make setShape your first mutator method * Add comments as soon as you write the method - comments are for other * humans, not for yourself or the computer, so make them meaningful! * */