Create a Building class and two subclasses, House and School. The Building class
ID: 668579 • Letter: C
Question
Create a Building class and two subclasses, House and School. The Building class contains fields for square footage and stories. The House class contains additional fields for the numbers of bedrooms and baths. The School class contains additional fields for the number of classrooms and grade level (e.g. elementary or junior high). All classes contain appropriate get and set methods and overloaded constructors allowing to accept and set the field data values.
Create an application driver class ShowBuildings, which creates the objects from the House andSchool classes, accepts the user’s input for the fields of proper object type and pass those to the object fields for storage.
Display the content of the every data field in the created object.
Explanation / Answer
class Building
{
double sqft;
int stories;
}
class House extends Building
{
int bedr;
int bathr;
void getd()
{
System.out.println("Square Footage" + sqft);
System.out.println("Number of Stories" + stories);
System.out.println("Number of Bedrooms" + bedr);
System.out.println("Number of Bathrooms" + bathr);
}
void setd(double sf, int s, int bed, int bath)
{
sqft=sf;
stories=s;
bedr=bed;
bathr=bath;
}
}
class School extends Building
{
int clsrm;
Sting grdlev;
void getd()
{
System.out.println("Square Footage" + sqft);
System.out.println("Number of Stories" + stories);
System.out.println("Number of Classrooms" + clsrm);
System.out.println("Grade Level" + grdlev);
}
void setd(double sf, int s, int c, String g)
{
sqft=sf;
stories=s;
clsrm=c;
grdlev=g;
}
}
class ShowBuilding
{
public static void main(String args[])
{
House o = new House();
School o1 = new School();
o.setd(1000,5,4,3);
o.getd();
o1.setd(2000,5,10,"elementry");
o1.getd();
}
}