class Building implements IsMetersTall {
// Static one-of-a-kind Building objects.
public static final Building EiffleTower = new Building(324.0);
public static final Building EmpireStateBuilding = new Building(1454);
public static final Building StatueOfLiberty = new Building(93.0);
// Private field.
private double metersTall;
// Public constructors.
public Building(int heightInFeet) {
metersTall = heightInFeet * 0.3048;
}
public Building(double heightInMeters) {
metersTall = heightInMeters;
}
// Public method.
public double getMetersTall() {
return metersTall;
}
}
class Person implements IsMetersTall {
// Static one-of-a-kind Person objects.
public static final Person AbrahamLincoln = new Person(6, 4);
public static final Person TomCruise = new Person(5, 7);
// Private field.
private double metersTall;
// Public constructors.
public Person(int fullFeetTall, int fullInchesTall) {
metersTall = (fullFeetTall + fullInchesTall / 12.0) * 0.3048;
}
public Person(double metersTall) {
this.metersTall = metersTall;
}
// Public method.
public double getMetersTall() {
return metersTall;
}
}
class Gnat implements IsMetersTall {
// Gnat has no fields. (All gnats are alike.)
// Since no other constructors are explicitly defined, Gnat has a
// no-argument, default constructor that is implicitly defined.
// See: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
// Public method.
public double getMetersTall() {
return 0.001;
}
}
// See: https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html
interface IsMetersTall {
double getMetersTall();
}
class Comparator {
public static double compareHeights(IsMetersTall a, IsMetersTall b) {
return a.getMetersTall() - b.getMetersTall();
}
}
public class TallObjects {
public static void main(String args[]) {
Gnat[] swarmOfGnats = new Gnat[100];
for (int i = 0; i < swarmOfGnats.length; i++) {
swarmOfGnats[i] = new Gnat();
}
Building myHouse = new Building(7.5);
double difference = Comparator.compareHeights(
swarmOfGnats[0], myHouse
);
System.out.println(difference);
}
}