Question

In: Computer Science

(In java language) Write an abstract class called House. The class should have type (mobile, multi-level,...

(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:

  • A no-arg/default constructor.
  • A constructor that accepts parameters.
  • A constructor that accepts the type of the house and sets the size to 100.
  • All other required methods.
  • An abstract method for calculating heating cost.
  • Come up with another abstract method of your own.

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.

Solutions

Expert Solution

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:


Related Solutions

Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
Write a java class called circle that represents a circle. It should have following three fields:...
Write a java class called circle that represents a circle. It should have following three fields: int x (x-coordinate), int y (y-coordinate), double radius (radius of the circle). Your circle object should have following methods: public int getRadius () public int getX () public int getY () public double area () public double perimeter() public String toString() write a client program called CircleClient that creates objects of the circle class called c1 and c2. Assign values to the fields when...
The language is java Write a class called Tablet that stores information about a tablet's age,...
The language is java Write a class called Tablet that stores information about a tablet's age, capacity (in GB), and current usage (in GB). You should not need to store any more information Write actuators and mutators for all instance data Write a toString method When you print a tablet, the info should be presented as such: This tablet is X years old with a capacity of Y gb and has Z gb used. There is A gb free on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT