COP 3337 SECTION U05 Spring 2017 THE APARTMENT COMPLEX ===================== I. The Apartment class: // the apartment class has the fields number, tenant, and value public class Apartment { // the fields private int number; private String tenant; private double value; // of the apartment // the constructor public Apartment( int aNumber, String aTenant, double aValue) { number = aNumber; tenant = aTenant; value = aValue; } // adjust the value public void adjustValue( double change) { value += change; } // return the value public double getValue() { return value; } // return the number public int getNumber() { return number; } // return the tenant's name public String getName() { return tenant; } } II. The ApartmentComplex class: public class ApartmentComplex extends Dwelling { // the field private Apartment[] units = null; // the constructor // the parameters are the address, the owner, and the array of apartments // the constructor computes the value of the complex as the sum of // the values of the apartments public ApartmentComplex( String anAddress, String anOwner, Apartment[] someUnits) { // form the super class super(anAddress, anOwner, 0); // compute the value of the complex double worth = 0; for (int i = 0; i < someUnits.length; i++) worth += someUnits[i].getValue(); // form the super class adjustValue(worth); units = someUnits; } // adjust the value // @param number is the unit number // @param change is the adjustment for that unit public void adjustValue( int number, double change) { // search for the apartment having that number int ind = findIndex(number); if ( ind >= 0) { // increase the value of the apartment and of the complex units[ind].adjustValue(change); adjustValue(change); } else // there is no apartment with that number throw new IllegalArgumentException("Wrong Number"); } // find the array index of a given apartment number // number is the number of the apartment private int findIndex(int number) { // search for the apartment having that number for (int i=0; i< units.length; i++) if ( units[i].getNumber() == number) return i; // the apartment was not found return -1; } } III. The Dwelling Class /** describes a dwelling */ public class Dwelling { // the fields private String address; private String owner; private double value; // in dollars // the constructor // form a dwelling with the given parameters // @param are the address, the owner, the value public Dwelling( String anAddress, String anOwner, double aValue) { address = anAddress; owner = anOwner; value = aValue; } // adjust the value of the dwelling // @param adjustment is the adjustment public void adjustValue(double adjustment) { value += adjustment; } // return the value public double getValue() { return value; } } IV. The Driver: //This program is a driver for Dwelling and ApartmentComplex public class Main { public static void main( String[] args) { System.out.println("Testing ApartmentComplex"); System.out.println("========================\n\n\n"); // form 3 apartments System.out.println("We form 3 apartments, " + "Apartment( 1, \"Papa\", 30000),\n " + "Apartment( 2, \"Mark\", 40000), " + "Apartment (21, \"Walid\", 20000)"); System.out.println("The first parameter is the apartment #, " + "\nthe second the tenant, and the third the value.\n"); Apartment papa = new Apartment( 1, "Papa", 30000); Apartment mark = new Apartment( 2, "Mark", 40000); Apartment walid = new Apartment (21, "Walid", 20000); // put them into an array Apartment[] slum = { papa, mark, walid}; // form an apartment complex System.out.println("We form an apartment complex."); ApartmentComplex jay = new ApartmentComplex( "2 Caca Street", "Jay", slum); System.out.println("The value of the complex is " + jay.getValue()); // adjust the value of apartment 21 by 10000 System.out.println("We increase the value of the apartment 21 by 10000" + " dollars."); jay.adjustValue(21,10000); // print the value of the complex and the value of apartment 21 System.out.println("The new value of the building is: " + jay.getValue()); System.out.println( walid.getName() + "'s aparment is worth " + walid.getValue()); } } V. The output: run: Testing ApartmentComplex ======================== We form 3 apartments, Apartment( 1, "Papa", 30000), Apartment( 2, "Mark", 40000), Apartment (21, "Walid", 20000) The first parameter is the apartment #, the second the tenant, and the third the value. We form an apartment complex. The value of the complex is 90000.0 We increase the value of the apartment 21 by 10000 dollars. The new value of the building is: 100000.0 Walid's aparment is worth 30000.0 BUILD SUCCESSFUL (total time: 4 seconds)