In: Computer Science
(In java language)
Write an abstract class called House. The class should have type (mobile, multi-level, cottage, etc.) and size.
Provide the following methods:
Then write 2 subclasses, one for mobile house and one for cottage. Add relevant attributes and implement appropriate methods for each class.
Note that mobile house uses 50% more in heating cost than regular houses (cottage or multi-level) because it doesn't have foundation and good insulation. Cottages use 30% more in heating cost due to low insulation. The heating cost is calculated based on the size of the house multiplied by .35 cents.
Write a tester to test all classes by creating multiple objects in an ArrayList of House. Do not create individual objects with specific object type. Print out the house information as well as its heating cost.
Kindly upvote if
this helped
CODE:
package search;
import java.util.ArrayList;
public abstract class House {
String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
int size;
public House() { // default constructor
type = "";
size = 0;
}
public House(String type ) { // constructor that accepts params
this.type = type;
this.size = 100;
}
public House(String type , int size) { // constructor that accepts params
this.type = type;
this.size = size;
}
public abstract double calculateHeatingCost();
public abstract String toString();
}
class MobileHouse extends House{
public MobileHouse(String type , int size) { // constructor that accepts params and sets values for size and type;
super(type , size);
}
@Override
public double calculateHeatingCost() {
return (super.getSize()*0.35) + (super.getSize()*0.35)/2; //used 50% more, so adding 50%
}
public String toString() {
return super.getType()+ " with size "+super.getSize()+" has a heating cost of "+calculateHeatingCost();
}
}
class CottageHouse extends House{
public CottageHouse(String type , int size) { // constructor that accepts params and sets values for size and type;
super(type , size);
}
@Override
public double calculateHeatingCost() {
return (super.getSize()*0.35) + ((super.getSize()*0.35) * 0.3); //used 30% more, so adding 30%
}
public String toString() {
return super.getType()+ " with size "+super.getSize()+" has a heating cost of "+calculateHeatingCost();
}
}
class TestingHouses{
public static void main(String[] args) {
ArrayList<House> arr = new ArrayList<House>();
arr.add(new CottageHouse("CottageTest1", 1100));
arr.add(new CottageHouse("CottageTest2", 1200));
arr.add(new CottageHouse("CottageTes3", 1030));
arr.add(new CottageHouse("MobileTest1", 1100));
arr.add(new CottageHouse("MobileTest2", 1600));
arr.add(new CottageHouse("MobileTest3", 1070));
for(int i=0;i<arr.size();i++) {
System.out.println("Type of house is "+arr.get(i).getType()+" and its heating cost is "+arr.get(i).calculateHeatingCost());
}
}
}
OUTPUT:
CODE snapshot for indent: