Contents
- ./Chicken2.java
- ./Chicken.java
- ./DynamicDispatchExample.java
- ./Rooster2.java
- ./Rooster.java
./Chicken2.java 1/5
[top][prev][next]
/**
* A simple Java class that models a Chicken. The state of the chicken is its
* name, height, weight, and gender.
*
* This is the original Chicken class.
*
* @author Sara Sprenkle
*/
public class Chicken2 {
// ------------ INSTANCE VARIABLES -------------------
/** the chicken's name */
private String name;
/** the height of the chicken in centimeters */
private int height;
/** the weight of the chicken in pounds */
private double weight;
/** true iff the chicken is a female */
private boolean isFemale;
public static String DEFAULT_NAME = "BUBBA";
/**
* @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
* @param isFemale whether the chicken is female
*/
public Chicken2(String name, int height, double weight, boolean isFemale ) {
this.name = name;
this.height = height;
this.weight = weight;
this.isFemale = isFemale;
}
/**
* Assumes Chickens are females
* @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 Chicken2(String name, int height, double weight) {
this( name, height, weight, true );
}
/**
* Default name is Bubba (defined by DEFAULT_NAME)
* Assumes Chickens are female
* @param height the height of the chicken in centimeters
* @param weight the weight of the chicken in pounds
*/
public Chicken2( int height, double weight ) {
this( DEFAULT_NAME, height, weight, true);
}
/**
* Returns a string representation of the chicken.
* Format:
* <br/>Chicken name: <name>
* <br/>weight: <weight>
* <br/>height: <height>
* <br/>female/male
*/
@Override
public String toString() {
StringBuffer rep = new StringBuffer("Chicken name: ");
rep.append(name);
rep.append("\nweight: ");
rep.append(weight);
rep.append("\nheight: ");
rep.append(height);
rep.append("\n");
rep.append( isFemale? "female" : "male"); // Java ternary operator; also available in C
return rep.toString();
}
/**
* Determines if two Chickens are equivalent, based on their
* name, height, weight, and gender.
*/
@Override
public boolean equals(Object o) {
if( o == this ) {
return true;
}
Chicken2 other = (Chicken2) o;
if( ! other.getName().equals(this.getName() ) ) {
return false;
}
if( other.getHeight() != this.getHeight() ) {
return false;
}
if( other.getWeight() != this.getWeight() ) {
return false;
}
if( this.isFemale() ) {
return other.isFemale();
} else {
return ! other.isFemale();
}
}
//
// ----------- GETTER METHODS ------------
// (also Accessor methods)
/**
* @return the height of the chicken, in centimeters
*/
public int getHeight() {
return height;
}
/**
* @return the weight of the chicken, in pounds
*/
public double getWeight() {
return weight;
}
/**
* @return the name of the chicken
*/
public String getName() {
return name;
}
/**
* @return whether this chicken is female.
*/
public boolean isFemale() {
return isFemale;
}
//
// ------------- MUTATORS -----------
//
/**
* Feeds the chicken--making the chicken grow
*/
public void feed() {
weight += .2;
height += 1;
}
//
// ------------- SETTERS ----------
//
/**
* @param n
* the name of the chicken
*/
public void setName(String n) {
name = n;
}
/**
* @param h
* the height of the chicken, in cm
*/
public void setHeight(int h) {
height = h;
}
/**
* @param w
* the weight of the chicken, in pounds
*/
public void setWeight(double w) {
weight = w;
}
/**
* @param args
* the command-line arguments
*/
public static void main(String args[]) {
Chicken2 one = new Chicken2("Fred", 38, 2.0, false);
Chicken2 two = new Chicken2("Sallie Mae", 45, 3.0);
Chicken2 oneFemale = new Chicken2("Fred", 38, 2.0, true);
System.out.println(one.getName() + " weighs " + one.getWeight() );
one.feed();
System.out.println(one.getName() + " ate, so he now weighs " + one.getWeight() );
System.out.print(two.getName() + " is now ");
two.setName("The Divine Miss Sallie Mae");
System.out.println(two.getName());
// test toString method
System.out.println(one);
System.out.println( one + " equal to " + two + "? " + one.equals(two) );
System.out.println( one + " equal to " + oneFemale + "? " + one.equals(oneFemale) );
System.out.println( two + " equal to " + two + "? " + two.equals(two) );
}
}
./Chicken.java 2/5
[top][prev][next]
/**
* A simple Java class that models a Chicken. The state of the chicken is its
* name, height, weight, and gender.
*
* Keeps instance variables protected, rather than private
*
* @author Sara Sprenkle
*/
public class Chicken {
// ------------ INSTANCE VARIABLES -------------------
/** the chicken's name */
protected String name;
/** the height of the chicken in centimeters */
protected int height;
/** the weight of the chicken in pounds */
protected double weight;
/** true iff the chicken is a female */
protected boolean isFemale;
public static String DEFAULT_NAME = "BUBBA";
/**
* @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
* @param isFemale whether the chicken is female
*/
public Chicken(String name, int height, double weight, boolean isFemale ) {
this.name = name;
this.height = height;
this.weight = weight;
this.isFemale = isFemale;
}
/**
* Assumes Chickens are females
* @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, height, weight, true );
}
/**
* Default name is Bubba (defined by DEFAULT_NAME)
* Assumes Chickens are female
* @param height the height of the chicken in centimeters
* @param weight the weight of the chicken in pounds
*/
public Chicken( int height, double weight ) {
this( DEFAULT_NAME, height, weight, true);
}
/**
* Returns a string representation of the chicken.
* Format:
* <br/>Chicken name: <name>
* <br/>weight: <weight>
* <br/>height: <height>
* <br/>female/male
*/
@Override
public String toString() {
StringBuffer rep = new StringBuffer("Chicken name: ");
rep.append(name);
rep.append("\nweight: ");
rep.append(weight);
rep.append("\nheight: ");
rep.append(height);
rep.append("\n");
rep.append( isFemale? "female" : "male"); // Java ternary operator; also available in C
return rep.toString();
}
/**
* Determines if two Chickens are equivalent, based on their
* name, height, weight, and gender.
*/
@Override
public boolean equals(Object o) {
if( o == this ) {
return true;
}
Chicken other = (Chicken) o;
if( ! other.getName().equals(this.getName() ) ) {
return false;
}
if( other.getHeight() != this.getHeight() ) {
return false;
}
if( other.getWeight() != this.getWeight() ) {
return false;
}
if( this.isFemale() ) {
return other.isFemale();
} else {
return ! other.isFemale();
}
}
//
// ----------- GETTER METHODS ------------
// (also Accessor methods)
/**
* @return the height of the chicken, in centimeters
*/
public int getHeight() {
return height;
}
/**
* @return the weight of the chicken, in pounds
*/
public double getWeight() {
return weight;
}
/**
* @return the name of the chicken
*/
public String getName() {
return name;
}
/**
* @return whether this chicken is female.
*/
public boolean isFemale() {
return isFemale;
}
//
// ------------- MUTATORS -----------
//
/**
* Feeds the chicken--making the chicken grow
*/
public void feed() {
weight += .2;
height += 1;
}
//
// ------------- SETTERS ----------
//
/**
* @param n
* the name of the chicken
*/
public void setName(String n) {
name = n;
}
/**
* @param h
* the height of the chicken, in cm
*/
public void setHeight(int h) {
height = h;
}
/**
* @param w
* the weight of the chicken, in pounds
*/
public void setWeight(double w) {
weight = w;
}
/**
* @param args
* the command-line arguments
*/
public static void main(String args[]) {
Chicken one = new Chicken("Fred", 38, 2.0, false);
Chicken two = new Chicken("Sallie Mae", 45, 3.0);
Chicken oneFemale = new Chicken("Fred", 38, 2.0, true);
System.out.println(one.getName() + " weighs " + one.getWeight() );
one.feed();
System.out.println(one.getName() + " ate, so he now weighs " + one.getWeight() );
System.out.print(two.getName() + " is now ");
two.setName("The Divine Miss Sallie Mae");
System.out.println(two.getName());
// test toString method
System.out.println(one);
System.out.println( one + " equal to " + two + "? " + one.equals(two) );
System.out.println( one + " equal to " + oneFemale + "? " + one.equals(oneFemale) );
System.out.println( two + " equal to " + two + "? " + two.equals(two) );
}
}
./DynamicDispatchExample.java 3/5
[top][prev][next]
/**
* This class demonstrates using dynamic dispatch
*
* @author Sara Sprenkle
*
*/
public class DynamicDispatchExample {
public static void main(String[] args) {
Parent p = new Parent();
Child c = new Child();
p.method1();
System.out.println("");
c.method1();
System.out.println("");
p.method2();
System.out.println("");
c.method2();
System.out.println("");
}
}
class Parent {
public Parent() {
}
public void method1() {
System.out.println("Parent: method1");
}
public void method2() {
System.out.println("Parent: method2");
method1();
}
}
class Child extends Parent {
public Child() {
}
public void method1() {
System.out.println("Child: method1");
}
}
./Rooster2.java 4/5
[top][prev][next]
/**
* Class represents a rooster.
*
* Example of how to extend a class whose instance variables are *private*.
*
* @author Sara Sprenkle
*/
public class Rooster2 extends Chicken2 {
/**
* @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 Rooster2(String name, int height, double weight) {
// call to the super constructor must be the first line in constructor
super(name, height, weight, false);
// NOTE: reduces amount of code required in Rooster class,
// increases code reuse, decreases code duplication
}
// new functionality
public void crow() {
System.out.println("Cocka-Doodle-Doo!");
}
/**
* overrides superclass; greater gains
*/
@Override
public void feed() {
this.setWeight(this.getWeight() + .5);
this.setHeight(this.getHeight() + 2);
}
public static void main(String argv[]) {
Rooster2 leghorn = new Rooster2("Foghorn", 22, 10);
System.out.println(leghorn);
leghorn.crow();
leghorn.feed();
System.out.println(leghorn);
}
}
./Rooster.java 5/5
[top][prev][next]
/**
* Class represents a rooster.
*
* Example of how to extend a class whose instance variables are *protected*.
* @author Sara Sprenkle
*/
public class Rooster extends Chicken {
/**
* @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 Rooster( String name, int height, double weight ) {
// call to the super constructor must be the first line in constructor
super( name, height, weight, false);
// NOTE: reduces amount of code required in Rooster class,
// increases code reuse, decreases code duplication
}
// new functionality
public void crow() {
System.out.println("Cocka-Doodle-Doo!");
}
// overrides superclass; greater gains
@Override
public void feed() {
weight += .5;
height += 2;
}
public static void main(String argv[]) {
Rooster leghorn = new Rooster("Foghorn", 22, 10 );
System.out.println(leghorn);
leghorn.crow();
leghorn.feed();
System.out.println(leghorn);
}
}
Generated by GNU Enscript 1.6.6.