Create a Building class and two subclasses, House and School. The Building class
ID: 3685146 • 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 number of bedrooms and baths. The School class contains additional fields for number of classrooms and grade level (for example, elementary or junior high). All the classes contain appropriate get and set methods. Place the Building, House, and School classes in a package named com.course.buildings. Create an application that declares objects of each type and uses the package. Save the necessary files as Building.java, House.java, School.java, and CreateBuilding.java
Explanation / Answer
import java.util*;
public class Building
{
public Building(){}
int stories;
double square;
public Building(int s,double sq){
this.square=sq;
this.stories=s;
}
public int getStories(){
return stories;
}
public void setStories(int n){
this.stories=n;
}
public double getSquare(){
return square;
}
public void setSquare(double n){
this.square=n;
}
public String toString(Building b){
return " Number of Stories: "+b.getStories()+" Square footage: "+b.square;
}
}
public class House extends Building{
int bed;
int bath;
public House(int c, int l) {
this.bed=c;
this.bath=l;
}
public int getBed(){
return bed;
}
public void setBed(int n){
this.bed=n;
}
public int getBath(){
return bath;
}
public void setBath(int n){
this.bath=n;
}
public String toString(House b){
return " Number of Stories: "+b.getStories()+" Square footage: "+b.square+" Number of beds :"+b.getBed()+" Number of baths :"+b.getBath();
}
}
public class School extends Building{
int classroom;
String gradeL;
public School( int c, String l) {
this.classroom=c;
this.gradeL=l;
}
public int getC(){
return classroom;
}
public void setClass(int n){
this.classroom=n;
}
public String getS(){
return gradeL;
}
public void setStories(String n){
this.gradeL=n;
}
public String toString(School b){
return " Number of Stories: "+b.getStories()+" Square footage: "+b.square+" Number of classrooms :"+b.getC()+" Grade level of school :"+b.getS();
}
}
public class ChoiceQuestion {
public static void main(String[] args){
String opt="";
do{
System.out.print("Enter your option: 1.add a building 2.add a school 3.add a house any key to exit");
Scanner s=new Scanner(System.in);
opt =s.next();
if(opt.equals("1"))
{
System.out.println("Enter Number of stories of the building:");
int st = s.nextInt();
System.out.println("Enter Square footage of the building :");
double sf=s.nextDouble();
Building b=new Building(st,sf);
System.out.println(b.toString(b));
}else if(opt.equals("2")){
System.out.println("Enter Number of stories of the school:");
int st = s.nextInt();
System.out.println("Enter Square footage of the school:");
double sf=s.nextDouble();
System.out.println("Enter Number of classrooms of the school:");
int c = s.nextInt();
System.out.println("Enter grade level of the school elementary or junior high:");
String l=s.next();
School school=new School(c,l);
school.setStories(st);
school.setSquare(sf);
System.out.println(school.toString(school));
}else if(opt.equals("3")){
System.out.println("Enter Number of stories of the House:");
int st = s.nextInt();
System.out.println("Enter Square footage of the House:");
double sf=s.nextDouble();
System.out.println("Enter Number of bedrooms in the House:");
int c = s.nextInt();
System.out.println("Enter number of baths in the house:");
int l=s.nextInt();
House house=new House(c,l);
house.setStories(st);
house.setSquare(sf);
System.out.println(house.toString(house));
}else{
break;
}
}while(opt.equals("1")||opt.equals("2")||opt.equals("3"));
}
}