A House class with instance variables erfNumber, location, noOfRooms, etc. • Mandatory properties: status(is the asset owned or sold), number of years owned, selling price, and buying price. • At least two overloaded constructors that can be used to instatiate House objects. • Getter and setter methods for all properties you have created. • A method checkProfitOrLoss() that returns the profit or loss that can be made if the property is sold (sellingPrice-buyingPrice). • A custom toString() method to print out all the properties of a House object. • A static counter to keep track of the number of each object created. • At least 5 more property or vehicle classes such as Farm, Flat, Bakkie, RentalCar, etc
The class House is given below and is followed by explanation :
class House{
private static int noOfObjects = 0;
private int erfNumber;
private String location;
private int noOfRooms;
private String status;
private int noOfYears;
private int sellingPrice;
private int buyingPrice;
private int noOfFloors;
private int noOfHalls;
private int area;
private int noOfBathrooms;
private int noOfKithens;
public House(){
noOfObjects++;
}
House(int erfNumber, String location, int
noOfRooms, String status){
this.erfNumber = erfNumber;
this.location = location;
this.noOfRooms = noOfRooms;
this.status = status;
noOfObjects++;
}
House(int erfNumber, String location, int
noOfRooms, String status, int noOfYears, int sellingPrice, int
buyingPrice){
this.erfNumber = erfNumber;
this.location = location;
this.noOfRooms = noOfRooms;
this.status = status;
this.noOfYears = noOfYears;
this.sellingPrice =
sellingPrice;
this.buyingPrice =
buyingPrice;
noOfObjects++;
}
public static int getNoOfObjects() {
return noOfObjects;
}
public int getErfNumber() {
return erfNumber;
}
public void setErfNumber(int erfNumber) {
this.erfNumber = erfNumber;
}
public String getLocation() {
return location;
}
public void setLocation(String location)
{
this.location = location;
}
public int getNoOfRooms() {
return noOfRooms;
}
public void setNoOfRooms(int noOfRooms) {
this.noOfRooms = noOfRooms;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getNoOfYears() {
return noOfYears;
}
public void setNoOfYears(int noOfYears) {
this.noOfYears = noOfYears;
}
public int getSellingPrice() {
return sellingPrice;
}
public void setSellingPrice(int sellingPrice)
{
this.sellingPrice =
sellingPrice;
}
public int getBuyingPrice() {
return buyingPrice;
}
public void setBuyingPrice(int buyingPrice)
{
this.buyingPrice =
buyingPrice;
}
public int getNoOfFloors() {
return noOfFloors;
}
public void setNoOfFloors(int noOfFloors)
{
this.noOfFloors = noOfFloors;
}
public int getNoOfHalls() {
return noOfHalls;
}
public void setNoOfHalls(int noOfHalls) {
this.noOfHalls = noOfHalls;
}
public int getArea() {
return area;
}
public void setArea(int area) {
this.area = area;
}
public int getNoOfBathrooms() {
return noOfBathrooms;
}
public void setNoOfBathrooms(int noOfBathrooms)
{
this.noOfBathrooms =
noOfBathrooms;
}
public int getNoOfKithens() {
return noOfKithens;
}
public void setNoOfKithens(int noOfKithens)
{
this.noOfKithens =
noOfKithens;
}
public static void setNoOfObjects(int
noOfObjects) {
House.noOfObjects =
noOfObjects;
}
int checkProfitOrLoss() {
return sellingPrice -
buyingPrice;
}
public String toString() {
return "House [erfNumber=" +
erfNumber + ", location=" + location + ", noOfRooms=" + noOfRooms +
", status="
+ status + ", noOfYears=" + noOfYears + ",
sellingPrice=" + sellingPrice + ", buyingPrice="
+ buyingPrice + ", noOfFloors=" + noOfFloors +
", noOfHalls=" + noOfHalls + ", area=" + area
+ ", noOfBathrooms=" + noOfBathrooms + ",
noOfKithens=" + noOfKithens + "]";
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
Instance Variable : Instance variables are non-static variables and are declared in a class outside any method, constructor or block. For ex.
class House{
private int erfNumber;
int sum(int a, int b){
return a+b;
}
Here variable erfNumber is an instance variable.
Construtor : A constructor in Java is a special method that is used to initialize objects.
Ex:
public House(){
noOfObjects++;
}
House(int erfNumber, String location, int
noOfRooms, String status){
this.erfNumber = erfNumber;
this.location = location;
this.noOfRooms = noOfRooms;
this.status = status;
noOfObjects++;
}
Here we have two overloaded constructor i.e One having parameters and other is not.
Custom toString : The custom toString() to print all properties of house is :
public String toString() {
return "House [erfNumber=" +
erfNumber + ", location=" + location + ", noOfRooms=" + noOfRooms +
", status="
+ status + ", noOfYears=" + noOfYears + ",
sellingPrice=" + sellingPrice + ", buyingPrice="
+ buyingPrice + ", noOfFloors=" + noOfFloors +
", noOfHalls=" + noOfHalls + ", area=" + area
+ ", noOfBathrooms=" + noOfBathrooms + ",
noOfKithens=" + noOfKithens + "]";
}
Static Counter : For this we use a static Variable. We can only have one copy of a static variable per class irrespective of how many objects we create, So it is created only once and its value is incremented each time object of house is created.
private static int noOfObjects = 0;
Get Answers For Free
Most questions answered within 1 hours.