Contents
- ./DynamicDispatchExample.java
- ./PrivateChicken.java
- ./PrivateRooster.java
- ./ProtectedChicken.java
- ./ProtectedRooster.java
./DynamicDispatchExample.java 1/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() {
}
@Override
public void method1() {
System.out.println("Child: method1");
}
}
./PrivateChicken.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 sex.
*
* This is similar to the original Chicken class, where the
* instance variables are private. See the impact of that design decision
* in the child class PrivateRooster.
*
* @author Sara Sprenkle
*/
public class PrivateChicken {
// ------------ 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;
// ------------ CLASS VARIABLES ---------------------
/** default name for chickens */
public static final String DEFAULT_NAME = "BUBBA";
/** the name of the farm the chickens are on */
private static String FARM = "McDonald";
/** the amount of weight the chicken gains during feeding */
private static final double WEIGHT_GAIN = .3;
/** the amount of height the chicken gains during feeding */
private static final int HEIGHT_GAIN = 1;
/** the amount of weight difference we are okay with */
private static final double ERROR_TOLERANCE = .0001;
/**
* Constructs a chicken with the given characteristics.
*
* @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 PrivateChicken(String name, int height, double weight, boolean isFemale ) {
this.name = name;
this.height = height;
this.weight = weight;
this.isFemale = isFemale;
}
/**
* Constructs a chicken with the given characteristics and assumes
* the chicken is female.
*
* @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 PrivateChicken(String name, int height, double weight) {
this( name, height, weight, true );
}
/**
* Constructs a chicken with the given characteristics, assumes
* the chicken's name is Bubba (defined by DEFAULT_NAME) and that
* it is female.
* @param height the height of the chicken in centimeters
* @param weight the weight of the chicken in pounds
*/
public PrivateChicken( 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> pounds
* <br/>height: <height> cm
* <br/>female/male
* <p>Weight is displayed to one decimal place
* @return a string representation of this Chicken
*/
@Override
public String toString() {
StringBuffer rep = new StringBuffer("Chicken name: ");
rep.append(name);
rep.append("\nweight: ");
rep.append(String.format("%.1f", weight));
rep.append(" pounds\nheight: ");
rep.append(height);
rep.append(" cm\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 sex.
*/
@Override
public boolean equals(Object o) {
if( o == this ) {
return true;
}
PrivateChicken other = (PrivateChicken) o;
if( ! other.getName().equals(this.getName() ) ) {
return false;
}
if( other.getHeight() != this.getHeight() ) {
return false;
}
double difference = this.getWeight() - other.getWeight();
if( difference > ERROR_TOLERANCE || difference < -ERROR_TOLERANCE) {
return false;
}
return this.isFemale() == other.isFemale();
}
//
// ----------- GETTER METHODS ------------
// (also Accessor methods)
/**
* Returns the height of the chicken in centimeters
* @return the height of the chicken, in centimeters
*/
public int getHeight() {
return height;
}
/**
* Returns the weight of the chicken 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;
}
/**
* Returns whether this chicken is female.
*
* @return true if the chicken is female and false otherwise.
*/
public boolean isFemale() {
return isFemale;
}
//
// ------------- MUTATORS -----------
//
/**
* Feeds the chicken--making the chicken grow
*/
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
*
* @param h
* the height of the chicken, in cm
*/
public void setHeight(int h) {
height = h;
}
/**
* Sets the weight of the chicken
*
* @param weight
* the weight of the chicken, in pounds
*/
public void setWeight(double weight) {
this.weight = weight;
}
/**
* @param args
* the command-line arguments
*/
public static void main(String args[]) {
int fredHeight = 38;
PrivateChicken chicken = new PrivateChicken("Fred", fredHeight, 2.0, false);
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 and " + chicken.getWeight() + " pounds");
String expectedRep = "Chicken name: Fred\nweight: 2.6 pounds\nheight: 40 cm\nmale";
String actualRep = chicken.toString();
if( ! actualRep.equals(expectedRep) ) {
System.err.println("Problem in toString");
System.err.println("\tActual: " + actualRep);
System.err.println("\tExpected: " + expectedRep);
}
PrivateChicken trivialMatch = chicken;
if( ! chicken.equals(trivialMatch) ) {
System.err.println("Problem in equals");
System.err.println("\tActual: " + chicken.equals(trivialMatch) );
System.err.println("\tExpected: " + true);
}
PrivateChicken grownFred = new PrivateChicken("Fred", 40, 2.6, false);
if( ! chicken.equals(grownFred) ) {
System.err.println("Problem in equals");
System.err.println("\tActual: " + chicken.equals(grownFred) );
System.err.println("\tExpected: " + true);
}
// ---- 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++ ) {
PrivateChicken thisChicken = new PrivateChicken( 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, but tried to address by giving some
// error tolerance on the weight
double expectedWeight2 = weights[i] + 2 * WEIGHT_GAIN;
int comparison = Double.compare(thisChicken.getWeight(), expectedWeight2 );
if( comparison != 0 ) {
double difference = thisChicken.getWeight() - expectedWeight2;
if( difference > ERROR_TOLERANCE || difference < -ERROR_TOLERANCE) {
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 * 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
}
}
}
./PrivateRooster.java 3/5
[top][prev][next]
/**
* Class represents a rooster, which is a specialized Chicken.
*
* Example of how to extend a class whose instance variables are *private*.
*
* @author Sara Sprenkle
*/
public class PrivateRooster extends PrivateChicken {
// ------------ CLASS VARIABLES ---------------------
/** the amount of weight the rooster gains during feeding */
private static double WEIGHT_GAIN = .5;
/** the amount of height the rooster gains during feeding */
private static int HEIGHT_GAIN = 2;
/**
* Construct the Rooster with the given characteristics.
*
* @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 PrivateRooster(String name, int height, double weight) {
// call to the super constructor must be the first statement in constructor
super(name, height, weight, false);
// NOTE: call to super's constructor reduces amount of code required
// in PrivateRooster class, increases code reuse, decreases code duplication
}
// new functionality
/**
* Displays the Rooster's signature phrase
*/
public void crow() {
System.out.println("Cocka-Doodle-Doo!");
}
/**
* Feed the rooster. The rooster gains weight and its height increases.
*/
@Override
public void feed() {
// overrides superclass; greater gains by rooster
this.setWeight(this.getWeight() + WEIGHT_GAIN);
this.setHeight(this.getHeight() + HEIGHT_GAIN);
}
public static void main(String argv[]) {
PrivateRooster leghorn = new PrivateRooster("Foghorn", 22, 10);
System.out.println(leghorn);
leghorn.crow();
leghorn.feed();
System.out.println(leghorn);
PrivateRooster fedLeghorn = new PrivateRooster("Foghorn", 24, 10.5);
if( ! leghorn.equals(fedLeghorn) ) {
System.out.println("Error in feeding rooster");
}
/** TODO: MORE TESTING! **/
}
}
./ProtectedChicken.java 4/5
[top][prev][next]
/**
* A simple Java class that models a Chicken. The state of the chicken is its
* name, height, weight, and sex.
*
* Instance variables are _protected_, rather than private, to
* demonstrate a different way to write the class. Effect of making the
* instance variables protected is seen in the child class ProtectedRooster.
*
* @author Sara Sprenkle
*/
public class ProtectedChicken {
// ------------ 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;
// ------------- CLASS VARIABLES --------------------
/** default name for chickens */
public static final String DEFAULT_NAME = "BUBBA";
/** the name of the farm the chickens are on */
private static String FARM = "McDonald";
/** the amount of weight the chicken gains during feeding */
private static final double WEIGHT_GAIN = .3;
/** the amount of height the chicken gains during feeding */
private static final int HEIGHT_GAIN = 1;
/** the amount of weight difference we are okay with */
private static final double ERROR_TOLERANCE = .0001;
/**
* Constructs a chicken with the given characteristics
*
* @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 ProtectedChicken(String name, int height, double weight, boolean isFemale ) {
this.name = name;
this.height = height;
this.weight = weight;
this.isFemale = isFemale;
}
/**
* Constructs a chicken with the given characteristics and assumes
* the chicken is female
* @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 ProtectedChicken(String name, int height, double weight) {
this( name, height, weight, true );
}
/**
* Constructs a chicken with the given characteristics, assumes
* the chicken is female, and the name is Bubba (defined by
* DEFAULT_NAME).
* @param height the height of the chicken in centimeters
* @param weight the weight of the chicken in pounds
*/
public ProtectedChicken( 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> pounds
* <br/>height: <height> cm
* <br/>female/male
* <p>Weight is displayed to one decimal place
* @return a string representation of this Chicken
*/
@Override
public String toString() {
StringBuffer rep = new StringBuffer("Chicken name: ");
rep.append(name);
rep.append("\nweight: ");
rep.append(String.format("%.1f", weight));
rep.append(" pounds\nheight: ");
rep.append(height);
rep.append(" cm\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 sex.
*/
@Override
public boolean equals(Object o) {
if( o == this ) {
return true;
}
if( ! (o instanceof ProtectedChicken) ) {
return false;
}
ProtectedChicken other = (ProtectedChicken) o;
if( ! other.getName().equals(this.getName() ) ) {
return false;
}
if( other.getHeight() != this.getHeight() ) {
return false;
}
double difference = this.getWeight() - other.getWeight();
if( difference > ERROR_TOLERANCE || difference < -ERROR_TOLERANCE) {
return false;
}
return this.isFemale() == other.isFemale();
}
// ----------- GETTER METHODS ------------
// (also Accessor methods)
/**
* Returns the height of the chicken in centimeters
*
* @return the height of the chicken, in centimeters
*/
public int getHeight() {
return height;
}
/**
* Returns the weight of the chicken in pounds
*
* @return the weight of the chicken, in pounds
*/
public double getWeight() {
return weight;
}
/**
* Returns the name of the chicken
*
* @return the name of the chicken
*/
public String getName() {
return name;
}
/**
* Returns whether this chicken is female.
*
* @return true if the chicken is female and false otherwise.
*/
public boolean isFemale() {
return isFemale;
}
//
// ------------- MUTATORS -----------
//
/**
* Feeds the chicken--making the chicken grow
*/
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
*
* @param h
* the height of the chicken, in cm
*/
public void setHeight(int h) {
height = h;
}
/**
* Sets the weight of the chicken
*
* @param weight
* the weight of the chicken, in pounds
*/
public void setWeight(double weight) {
this.weight = weight;
}
/**
* @param args
* the command-line arguments
*/
public static void main(String args[]) {
int fredHeight = 38;
ProtectedChicken chicken = new ProtectedChicken("Fred", fredHeight, 2.0, false);
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 and " + chicken.getWeight() + " pounds");
String expectedRep = "Chicken name: Fred\nweight: 2.6 pounds\nheight: 40 cm\nmale";
String actualRep = chicken.toString();
if( ! actualRep.equals(expectedRep) ) {
System.err.println("Problem in toString");
System.err.println("\tActual: " + actualRep);
System.err.println("\tExpected: " + expectedRep);
}
ProtectedChicken trivialMatch = chicken;
if( ! chicken.equals(trivialMatch) ) {
System.err.println("Problem in equals");
System.err.println("\tActual: " + chicken.equals(trivialMatch) );
System.err.println("\tExpected: " + true);
}
ProtectedChicken grownFred = new ProtectedChicken("Fred", 40, 2.6, false);
if( ! chicken.equals(grownFred) ) {
System.err.println("Problem in equals");
System.err.println("\tActual: " + chicken.equals(grownFred) );
System.err.println("\tExpected: " + true);
}
// ---- 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++ ) {
ProtectedChicken thisChicken = new ProtectedChicken( 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, but tried to address by giving some
// error tolerance on the weight
double expectedWeight2 = weights[i] + 2 * WEIGHT_GAIN;
int comparison = Double.compare(thisChicken.getWeight(), expectedWeight2 );
if( comparison != 0 ) {
double difference = thisChicken.getWeight() - expectedWeight2;
if( difference > ERROR_TOLERANCE || difference < -ERROR_TOLERANCE) {
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 * 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
}
}
}
./ProtectedRooster.java 5/5
[top][prev][next]
/**
* Class represents a rooster, which is a specialized Chicken
*
* Example of how to extend a class whose instance variables are *protected*.
*
* @author Sara Sprenkle
*/
public class ProtectedRooster extends ProtectedChicken {
// ------------ CLASS VARIABLES ---------------------
/** the amount of weight the rooster gains during feeding */
private static double WEIGHT_GAIN = .5;
/** the amount of height the rooster gains during feeding */
private static int HEIGHT_GAIN = 2;
/**
* Construct the Rooster with the given characteristics.
*
* @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 ProtectedRooster( String name, int height, double weight ) {
// call to the super constructor must be the first statement in constructor
super( name, height, weight, false);
// NOTE: call to super's constructor reduces amount of code required
// in Rooster class, increases code reuse, decreases code duplication
}
// new functionality
/**
* Displays the Rooster's signature phrase
*/
public void crow() {
System.out.println("Cocka-Doodle-Doo!");
}
/**
* Feed the rooster. The rooster gains weight and its height increases.
*/
@Override
public void feed() {
//overrides superclass; greater gains by rooster
weight += WEIGHT_GAIN;
height += HEIGHT_GAIN;
}
public static void main(String argv[]) {
ProtectedRooster leghorn = new ProtectedRooster("Foghorn", 22, 10);
System.out.println(leghorn);
leghorn.crow();
leghorn.feed();
System.out.println(leghorn);
ProtectedRooster fedLeghorn = new ProtectedRooster("Foghorn", 24, 10.5);
if( ! leghorn.equals(fedLeghorn) ) {
System.out.println("Error in feeding rooster");
}
/** TODO: MORE TESTING **/
}
}
Generated by GNU Enscript 1.6.5.90.