Implement a client class to test the Apartment class as follows: 1. Create an array of Apartment objects of size 5. 2. Fill it with five Apartment objects according to your choice as a programmer (NOT from the user) 3. Print the description of all apartments in the array which are located in “Beirut” 4. Display the size assessment of each apartment in the array. 5. Display the city of all apartment objects that are formed of two stories
public class Appartments {
private String cityOfFlatLocation;
private double sizeOfFlat;
private int floors;
public Appartments() {
}
public Appartments(String cityOfFlatLocation, double sizeOfFlat, int floors) {
this.cityOfFlatLocation = cityOfFlatLocation;
this.sizeOfFlat = sizeOfFlat;
this.floors = floors;
}
public String getCityOfFlatLocation() {
return cityOfFlatLocation;
}
public void setCityOfFlatLocation(String cityOfFlatLocation) {
this.cityOfFlatLocation = cityOfFlatLocation;
}
public double getSizeOfFlat() {
return sizeOfFlat;
}
public void setSizeOfFlat(double sizeOfFlat) {
this.sizeOfFlat = sizeOfFlat;
}
public int getFloors() {
return floors;
}
public void setFloors(int floors) {
this.floors = floors;
}
@Override
public String toString() {
return "Appartments [cityOfFlatLocation=" + cityOfFlatLocation + ", sizeOfFlat=" + sizeOfFlat + ", floors="
+ floors + "]";
}
}
public class AppartmentsDemo {
public static void main(String[] args) {
// 1. Create an array of Apartment objects of size 5. 2. Fill it with five
// Apartment objects according to your choice as a programmer (NOT from the
// user)
Appartments[] flats = new Appartments[5];
flats[0] = new Appartments("Chicago", 62.7, 1);
flats[1] = new Appartments("Beirut", 218.2, 2);
flats[2] = new Appartments("London", 62.7, 1);
flats[3] = new Appartments("Beirut", 145.8, 2);
flats[4] = new Appartments("Paris", 108.1, 2);
// 3. Print the description of all apartments in the array which are located in
// “Beirut”
for (int i = 0; i < flats.length; i++) {
if (flats[i].getCityOfFlatLocation().equals("Beirut"))
System.out.println(flats[i]);
}
// 4.Display the size assessment of each apartment in the array
System.out.println();
for (int i = 0; i < flats.length; i++) {
System.out.println(flats[i].getSizeOfFlat());
}
// 5. Display the city of all apartment objects that are formed of two stories
System.out.println();
for (int i = 0; i < flats.length; i++) {
if (flats[i].getFloors() == 2)
System.out.println(flats[i]);
}
}
}
Comments
Leave a comment