We want to realize a program for collecting statistical data about apartments. For each apartment, the following information is stored:
Part 1.
Write a class Apartament
that implements the following
methods:
Apartament(String a, double s)
: constructor that creates an
object representing an appartment whose address is a
, whose
size is s
, and which is initially free;
String getAddress()
: that returns the address of the
apartment;
double getSize()
: that returns the size of the apartment;
String getPerson()
: that returns the name of the person
living in the apartment, if the apartment is occupied, and null
otherwise;
boolean isFree()
: that checks whether the aparment is free;
void occupy(String p)
: that changes the status of the
apartment to occupied and sets the name of the person living in the apartment
to p
; if the apartment is already occupied, the method should
do nothing;
void free()
: that changes the status of the apartment to
free; note that when the apartment is free, the name of the person living in
it is not significant;
String toString()
: that returns a string with all the
information about the apartment.
Example program:
TestAparment.Java
Part 2.
Write a class UseApartment
, client of Apartment
, that
contains the following public static method:
static double averageSizeFree(Apartment[] arc)
: that,
given an archive arc
of apartaments, represented as an array of
apartments, calculates the average size of the apartments that are free. If
there are no free apartments in the archive, the method should return 0.
Example program:
TestUseApartment.java
Solutions:
Apartment.java
,
UseApartment.java