Contents
- ./Car.java
- ./CarLot.java
- ./Chicken.java
- ./Hybrid.java
- ./Movable.java
- ./OverloadPlay.java
- ./Powered.java
./Car.java 1/7
[top][prev][next]
/**
* Demonstrates use of interface
*
*/
public class Car implements Powered {
private double mpg;
private double xcoord;
private double ycoord;
// Making tankSize a static variable is only for demonstration purposes.
// The tank size should probably be an instance variable, not a static
// variable.
public static int tankSize = 10; // 10 gallons
public Car(double mpg) {
this.mpg = mpg;
xcoord = 0;
ycoord = 0;
}
@Override
public String toString() {
return xcoord + ", " + ycoord;
}
public double milesPerGallon() {
return mpg;
}
public void move(double x, double y) {
xcoord += x;
ycoord += y;
}
public static int getTankSize() {
System.out.println("Car's getTankSize()");
return tankSize;
}
public static void main(String args[]) {
Car car = new Car(32);
car.move(1, 1);
System.out.println(car);
System.out.println(SPEED_LIMIT);
System.out.println(tankSize);
System.out.println(getTankSize());
}
}
./CarLot.java 2/7
[top][prev][next]
public class CarLot {
public static void main(String[] args) {
Car[] mycars = new Car[2];
Car c = new Car(35);
Hybrid h = new Hybrid();
mycars[0] = c;
mycars[1] = h;
// note that mycar[1] and h are two variables referencing
// the *same* object.
System.out.println(h.getTankSize());
System.out.println(mycars[1].getTankSize());
// Output:
// Hybrid's getTankSize
// 10
// Car's getTankSize()
// 10
}
}
./Chicken.java 3/7
[top][prev][next]
import java.util.Arrays;
/**
* A simple Java class that models a Chicken. The state of the chicken is its
* name, height, weight, and gender.
*
* Made several of the accessor methods final because child classes shouldn't need to change them.
*
* Implements the Comparable interface
*
* @author CSCI209
*/
public class Chicken implements Comparable {
// ------------ 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;
/** default name of a Chicken */
public static final String DEFAULT_NAME = "BUBBA";
/**
* Creates a fully-specified Chicken object
*
* @param name
* the name of the Chicken
* @param height
* the Chicken's height, in cm
* @param weight
* the Chicken's weight, in pounds
* @param isFemale
* if the Chicken is female (true/false)
*/
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 Chicken's height, in cm
* @param weight
* the Chicken's weight, in pounds
*/
public Chicken(String name, int height, double weight) {
this(name, height, weight, true);
}
/**
* Default name is Bubba; Assumes Chickens are female
*
* @param height
* the Chicken's height, in cm
* @param weight
* the Chicken's weight, 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.
*/
public boolean equals(Object o) {
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 (other.weight != this.weight) {
return false;
}
return this.isFemale == other.isFemale;
}
/*
* (non-Javadoc)
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
/**
* Compares the chickens by their height
*/
@Override
public int compareTo(Object o) {
// There is a better way to handle this; we will talk about it later.
if (!(o instanceof Chicken)) {
return 1;
}
Chicken otherChicken = (Chicken) o;
if (otherChicken.getHeight() > this.getHeight()) {
return -1;
} else if (otherChicken.getHeight() < this.getHeight()) {
return 1;
}
return 0;
// simpler: return height-other.getHeight()
}
//
// ----------- GETTER METHODS ------------
// (also Accessor methods)
/**
* @return the height of the chicken, in centimeters
*/
public final int getHeight() {
return height;
}
/**
*
* @return the weight of the chicken, in pounds
*/
public final double getWeight() {
return weight;
}
/**
*
* @return the name of the Chicken
*/
public final String getName() {
return name;
}
/**
*
* @return whether this chicken is a female
*/
public boolean isFemale() {
return isFemale;
}
//
// ------------- MUTATORS -----------
//
/**
* Feed the chicken. Side effects: the chicken grows in weight and height.
*/
public void feed() {
weight += .2;
height += 1;
}
//
// ------------- SETTERS ----------
//
/**
* Changes 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 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 female = new Chicken("Lucille", 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 " + female + "? "
+ one.equals(female));
System.out.println(two + " equal to " + two + "? " + two.equals(two));
Chicken[] chickens = new Chicken[3];
chickens[0] = one;
chickens[1] = two;
chickens[2] = female;
// sort the chickens
Arrays.sort(chickens);
// print the chickens in sorted order (by height)
for (Chicken c : chickens) {
System.out.println(c);
}
}
}
./Hybrid.java 4/7
[top][prev][next]
/**
* Demonstrates extension
*
*/
public class Hybrid extends Car {
public Hybrid() {
super(45);
}
public static int getTankSize() {
System.out.println("Hybrid's getTankSize");
return tankSize;
}
public static void main(String args[]) {
Hybrid car = new Hybrid();
car.move(1, 1);
System.out.println(car);
System.out.println(SPEED_LIMIT);
System.out.println(tankSize);
System.out.println(getTankSize());
}
}
./Movable.java 5/7
[top][prev][next]
public interface Movable {
void move(double x, double y);
}
./OverloadPlay.java 6/7
[top][prev][next]
class Parent {
public void method() {
System.out.println("Called parent's method");
}
}
class Child extends Parent {
public void method() {
System.out.println("Called child's method");
}
}
public class OverloadPlay {
public static void print(Parent parent) {
System.out.println("Static Parent");
}
public static void print(Child child) {
System.out.println("Static Child");
}
public void instancePrint(Parent parent) {
System.out.println("Parent");
parent.method();
}
public void instancePrint(Child child) {
System.out.println("Child");
child.method();
}
public static void main(String[] args) {
Child child = new Child();
Parent p = child;
print(p);
new OverloadPlay().instancePrint(p);
}
}
./Powered.java 7/7
[top][prev][next]
public interface Powered extends Movable {
double milesPerGallon();
double SPEED_LIMIT = 95;
}
Generated by GNU Enscript 1.6.6.