Question

A House class with instance variables erfNumber, location, noOfRooms, etc. • Mandatory properties: status(is the asset...

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

Homework Answers

Answer #1

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;

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
Java Programming COMP-228 LAB ASSIGNMENT #1 >> Apply the naming conventions for variables, methods, classes, and...
Java Programming COMP-228 LAB ASSIGNMENT #1 >> Apply the naming conventions for variables, methods, classes, and packages: - variable names start with a lowercase character for the first word and uppercase for every other word - classes start with an uppercase character of every word - packages use only lowercase characters - methods start with a lowercase character for the first word and uppercase for every other word Java Programming COMP-228 Exercise #1: [5 marks] Write a Java application using...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...