Contents
- ./Chicken.java
- ./Equals.java
- ./NotTheChickenClass.java
./Chicken.java 1/3
[top][prev][next]
/**
* A Java class that represents a chicken. The state of the chicken is
* its name, height and weight.
*
* Added overriden methods: toString, equals
*
* @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;
/**
* 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 += .3;
height += 1;
}
//
// ------------- 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;
}
/**
* Returns a string representation of the chicken.
* Format:
* <br/>Chicken name: <name>
* <br/>weight: <weight> pounds
* <br/>height: <height> cm
* <p>Weight is displayed to one decimal place
*
* @return a string representation of this Chicken
*/
@Override
public String toString() {
// Use a StringBuilder: more efficient than concatenating strings.
StringBuilder rep = new StringBuilder("Chicken name: ");
rep.append(name);
rep.append("\nweight: ");
rep.append(String.format("%.1f", weight)); // guesses as to what this does?
rep.append(" pounds");
rep.append("\nheight: ");
rep.append(height);
rep.append(" cm");
return rep.toString();
}
/**
* Determines if the Object o is equivalent to this Chicken,
* based on their name, height, and weight.
*
* @param o the object to compare
* @return true if this object is the same as the o argument (by name, height, and wight). Otherwise, returns false.
*/
@Override
public boolean equals(Object o) {
// Follows the _Effective Java_ process
if( o == this ) {
return true;
}
if( ! ( o instanceof Chicken ) ) {
return false;
}
Chicken other = (Chicken) o;
if( ! other.getName().equals(this.getName() ) ) {
return false;
}
if( other.getHeight() != this.getHeight() ) {
return false;
}
if( Double.compare( other.getWeight(), this.getWeight()) == 0 ) {
return true;
}
// if the weight is close enough, let's consider them
// equivalent
double difference = this.getWeight() - other.getWeight();
if( difference > -.0001 && difference < .0001 ) {
return true;
}
return false;
}
/**
* Tests the Chicken class
* @param args the command-line arguments
*/
public static void main(String[] args) {
int fredHeight = 38;
Chicken chicken = new Chicken("Fred", fredHeight, 2.0);
System.out.println(chicken);
chicken.feed();
int newFredHeight = chicken.getHeight();
System.out.println();
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 and " + chicken.getWeight() + " pounds");
System.out.println("\nLook at this beautiful formatting: ");
System.out.println(chicken);
String expectedRep = "Chicken name: Fred\nweight: 2.6 pounds\nheight: 40 cm";
String actualRep = chicken.toString();
// Test the toString method
// Demonstrates trickiness with testing with doubles
if( ! actualRep.equals(expectedRep) ) {
System.err.println("Problem in toString");
System.err.println("\tActual: " + actualRep);
System.err.println("\tExpected: " + expectedRep);
}
Chicken trivialMatch = chicken;
// Test equals method
if( ! chicken.equals(trivialMatch) ) {
System.err.println("Problem in equals");
System.err.println("\tActual: " + chicken.equals(trivialMatch) );
System.err.println("\tExpected: " + true);
}
Chicken grownFred = new Chicken("Fred", 40, 2.6);
// Works because we have some error tolerance in weight comparison
if( ! chicken.equals(grownFred) ) {
System.err.println("Problem in equals");
System.err.println("\tActual: " + chicken.equals(grownFred) );
System.err.println("\tExpected: " + true);
}
// TODO: more testing of equals method. Only tested
// cases where expected true.
// ---- creating tests for chickens -----
String[] names = {"Rocky", "Baby Chicken"};
double[] weights = {4.0, .8};
int[] heights = {50, 4};
String[] newNames = {"Rocky II", "Chicken"};
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] + .3 ) {
System.err.println("Error in feed weight for Chicken " + i);
System.err.println("\tActual: " + thisChicken.getWeight());
System.err.println("\tExpected: " + (weights[i] + .3) );
}
if( thisChicken.getHeight() != heights[i] + 1 ) {
System.err.println("Error in feed height for Chicken " + i);
System.err.println("\tActual: " + thisChicken.getHeight());
System.err.println("\tExpected: " + (heights[i] + 1) );
}
// feed the chicken again and check the state
thisChicken.feed();
// NOTE: this test may fail, but tried to address by giving some
// error tolerance on the weight
double expectedWeight2 = weights[i] + 2 * .3;
int comparison = Double.compare(thisChicken.getWeight(), expectedWeight2 );
if( comparison != 0 ) {
if( thisChicken.getWeight() - expectedWeight2 > .0001 ) {
System.err.println("Error in second feed weight for Chicken " + i);
System.err.println("\tActual: " + thisChicken.getWeight());
System.err.println("\tExpected: " + (expectedWeight2 ));
}
}
if( thisChicken.getHeight() != heights[i] + 2 * 1 ) {
System.err.println("Error in second feed height for Chicken " + i);
System.err.println("\tActual: " + thisChicken.getHeight());
System.err.println("\tExpected: " + (heights[i] + 2 * 1 ));
}
thisChicken.setName(newNames[i]);
if( !thisChicken.getName().equals(newNames[i]) ) {
System.err.println("Problem likely in setName");
System.err.println("\tActual: " + thisChicken.getName());
System.err.println("\tExpected: " + newNames[i]);
}
}
}
}
./Equals.java 2/3
[top][prev][next]
/**
* Demonstrates different equals with Strings
* Run as
* java Equals <command-line argument>
* @author Sara Sprenkle
*/
public class Equals {
public static void main(String args[]) {
String string1 = "same";
String string2 = string1;
// The following statement doesn't create a _new_ String object/memory
// allocation Java memory optimization
String string3 = "same";
//String string4 = "same"; //for initial discussion
String string4 = args[0]; //enter "same" as a command-line argument
System.out.println("string1 == string2? " + (string1==string2));
System.out.println("string2 == string3? " + (string2==string3));
System.out.println("string1 == string4? " + (string1==string4));
// output should be
// true
// true
// false, regardless of what user enters
System.out.println("string1 equals string2? " + (string1.equals(string2)));
System.out.println("string2 equals string3? " + (string2.equals(string3)));
System.out.println("string1 equals string4? " + (string1.equals(string4)));
// output should be
// true
// true
// true (depending on what user enters)
}
}
./NotTheChickenClass.java 3/3
[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.
* (This class will NOT compile.)
*
* @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.5.90.