Contents
- ./Chicken.java
- ./NotTheChickenClass.java
./Chicken.java 1/2
[top][prev][next]
/**
* A Java class that represents a Chicken. The state of the chicken is
* its name, height and weight. There is also a static variable
* representing the name of the farm that the Chickens are on.
*
* @author Sara Sprenkle
*/
public class Chicken {
// ------------ INSTANCE VARIABLES -------------------
/** the name of the chicken */
private String name;
/** the height of the chicken in centimeters */
private int height;
/** the weight of the chicken in pounds */
private double weight;
// ------------ CLASS VARIABLES ------------------
/** the name of the farm the chickens are on */
public static String FARM = "McDonald";
/** the amount of weight the chicken gains during feeding */
private static double WEIGHT_GAIN = .3;
/** the amount of height the chicken gains during feeding */
private static int HEIGHT_GAIN = 1;
/**
* Create a new Chicken object with the charactistics as specified by the
* parameters.
* @param name the name of the chicken
* @param height the height of the chicken in centimeters
* @param weight the weight of the chicken in pounds
*/
public Chicken(String name, int height, double weight) {
this.name = name;
this.height = height;
this.weight = weight;
}
/**
* Default name: "Bubba"; height and weight specified by parameters
* @param height the height of the chicken in centimeters
* @param weight the weight of the chicken in pounds
*/
public Chicken(int height, double weight) {
// if the user doesn't specify a name, let's make it Bubba
this("Bubba", height, weight);
}
//
// ----------- GETTER METHODS ------------
// (also Accessor methods)
/**
* Returns the chicken's height, in centimeters
*
* @return the height of the chicken, in centimeters
*/
public int getHeight() {
return this.height;
}
/**
* Returns the chicken's weight, in pounds
*
* @return the weight of the chicken, in pounds
*/
public double getWeight() {
return weight;
}
/**
* Returns the chicken's name
*
* @return the name of the chicken
*/
public String getName() {
return name;
}
//
// ------------- MUTATORS -----------
//
/**
* Feeds the chicken, increasing the chicken's weight and height
*/
public void feed() {
weight += WEIGHT_GAIN;
height += HEIGHT_GAIN;
}
//
// ------------- SETTERS ----------
//
/**
* Sets the name of the chicken
*
* @param n the name of the chicken
*/
public void setName(String n) {
name = n;
}
/**
* Sets the height of the chicken, in cm
*
* @param h the height of the chicken, in cm
*/
public void setHeight(int h) {
height = h;
}
/**
* Sets the weight of the chicken, in pounds
*
* @param w the weight of the chicken, in pounds
*/
public void setWeight(double w) {
weight = w;
}
/**
* This method is private. It is accessible by the Chicken class
* but NOT by other classes.
*/
private void privateMethod() {
System.out.println("I am private!");
}
/**
* @param args
* the command-line arguments
*/
public static void main(String[] args) {
// "Fred", weight: 2.0, height: 38
int fredHeight = 38;
Chicken chicken = new Chicken("Fred", fredHeight, 2.0);
// Note: this will look like gibberish to us at this point
System.out.println(chicken);
if( chicken.getHeight() != fredHeight ) {
System.err.println("Problem likely in constructor setting height");
}
if( !chicken.getName().equals("Fred") ) {
System.err.println("Problem likely in constructor setting name");
}
chicken.feed();
int newFredHeight = chicken.getHeight();
System.out.println(chicken.getName() + " is now " + newFredHeight +
" cm tall.");
chicken.feed();
System.out.println("He's a growing boy at " + chicken.getHeight() + " cm tall.");
// ---- creating tests for chickens -----
String[] names = {"Rocky", "Baby Chicken"};
double[] weights = {4.0, .8};
int[] heights = {50, 4};
for( int i=0; i < names.length; i++ ) {
Chicken thisChicken = new Chicken( names[i], heights[i], weights[i] );
if( !thisChicken.getName().equals(names[i]) ) {
System.err.println("Problem likely in constructor setting name");
System.err.println("\tActual: " + thisChicken.getName());
System.err.println("\tExpected: " + names[i]);
}
if( thisChicken.getWeight() != weights[i] ) {
System.err.println("\tError in getWeight for Chicken " + i );
System.err.println("\tActual: " + thisChicken.getWeight());
System.err.println("\tExpected: " + weights[i] );
}
// feed the chicken and check the state
thisChicken.feed();
if( thisChicken.getWeight() != weights[i] + WEIGHT_GAIN ) {
System.err.println("Error in feed weight for Chicken " + i);
System.err.println("\tActual: " + thisChicken.getWeight());
System.err.println("\tExpected: " + (weights[i] + WEIGHT_GAIN) );
}
if( thisChicken.getHeight() != heights[i] + HEIGHT_GAIN ) {
System.err.println("Error in feed height for Chicken " + i);
System.err.println("\tActual: " + thisChicken.getHeight());
System.err.println("\tExpected: " + (heights[i] + HEIGHT_GAIN) );
}
// feed the chicken again and check the state
thisChicken.feed();
// NOTE: this test may fail; look at the output to see why
// We will have better ways to test later in the term.
if( thisChicken.getWeight() != weights[i] + 2 * WEIGHT_GAIN ) {
System.err.println("Error in second feed weight for Chicken " + i);
System.err.println("\tActual: " + thisChicken.getWeight());
System.err.println("\tExpected: " + (weights[i] + 2 * WEIGHT_GAIN ));
}
if( thisChicken.getHeight() != heights[i] + 2 * HEIGHT_GAIN ) {
System.err.println("Error in second feed height for Chicken " + i);
System.err.println("\tActual: " + thisChicken.getHeight());
System.err.println("\tExpected: " + (heights[i] + 2 * HEIGHT_GAIN ));
}
// TODO: test setName method
}
}
}
./NotTheChickenClass.java 2/2
[top][prev][next]
/**
* Demonstrates how we can create Chicken objects
* and call its public methods, but
* we can't call the Chicken's private methods.
* @author Sara Sprenkle
*/
public class NotTheChickenClass {
public static void main(String[] args) {
Chicken myChicken = new Chicken("Fred", 10, 2);
myChicken.feed();
// this will result in a compiler error:
myChicken.privateMethod();
}
}
Generated by GNU Enscript 1.6.6.