Write a class called Dormitory that is a subclass of Building. In addition to na
ID: 3622828 • Letter: W
Question
Write a class called Dormitory that is a subclass of Building. In addition to name, a Dormitory has two other member variables:1. floors of type int that holds the number of floors in the building 2. hasCafeteria of type boolean (true means that dorm has a cafeteria, false means it does
not) Methods
1. A constructor that accepts the dormitory name, the number of floors, and a Boolean indicating whether it has a cafeteria. Make sure that you use super to pass the name parameter to the super class.
2. A toString that produces a String representation of the dorm as shown in the following example:
Building Name : Twin Towers Number of Floors: 15 Has Cafeteria : Yes
Be sure to call the super class toString method to assist in writing the toString for Dormitory
Explanation / Answer
please rate - thanks
public class Dormitory extends Building
{int floors;
boolean hasCafeteria;
Dormitory(String n,int f,boolean yn)
{super(n);
floors=f;
hasCafeteria=yn;
}
public String toString()
{String yesno;
if(hasCafeteria)
yesno="Yes";
else
yesno="No";
String out="Building name : "+name+" Number of Floors: "+floors+" Has Cafeteria : "+yesno;
return out;
}
}
----------------------------------------------
import java.util.*;
public class BuildingDriver {
public static void main (String[] args) {
Building b=new Building("Twin Towers");
System.out.println(b.toString());
Dormitory d=new Dormitory("Twin Towers",15,true);
System.out.println(d.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.