w3resource

Creating a Java House Class with Encapsulation and Price Calculation

Java Encapsulatiion: Exercise-11 with Solution

Write a Java program to create a class called House with private instance variables address, numberOfRooms, and area. Provide public getter and setter methods to access and modify these variables. Add a method called calculatePrice() that returns the price of the house based on its area and a price per square meter.

Sample Solution:

Java Code:

House.java

// Define the House class
public class House {
    // Private instance variables
    private String address;
    private int numberOfRooms;
    private double area; // area in square meters

    // Public getter for the address variable
    public String getAddress() {
        return address;
    }

    // Public setter for the address variable
    public void setAddress(String address) {
        this.address = address;
    }

    // Public getter for the numberOfRooms variable
    public int getNumberOfRooms() {
        return numberOfRooms;
    }

    // Public setter for the numberOfRooms variable
    public void setNumberOfRooms(int numberOfRooms) {
        this.numberOfRooms = numberOfRooms;
    }

    // Public getter for the area variable
    public double getArea() {
        return area;
    }

    // Public setter for the area variable
    public void setArea(double area) {
        this.area = area;
    }

    // Method to calculate the price of the house based on area and price per square meter
    public double calculatePrice(double pricePerSquareMeter) {
        return this.area * pricePerSquareMeter;
    }
}

Main.java

public class Main {
    // Public setter for the numberOfRooms variable
   public static void main(String[] args) {
        // Create a new House object
        House house = new House();

        // Set the address, number of rooms, and area of the house
        house.setAddress("ABC Main Rd.");
        house.setNumberOfRooms(5);
        house.setArea(130.5);

        // Calculate the price of the house with a given price per square meter
        double price = house.calculatePrice(2000);

        // Print the details of the house and its calculated price
        System.out.println("Address: " + house.getAddress());
        System.out.println("Number of Rooms: " + house.getNumberOfRooms());
        System.out.println("Area: " + house.getArea() + " sq meters");
        System.out.println("Price: $" + price);
    }
 }

Output:

Address: ABC Main Rd.
Number of Rooms: 5
Area: 130.5 sq meters
Price: $261000.0

Explanation:

  • Private Instance Variables: The address, numberOfRooms, and area variables are declared as private to ensure encapsulation.
  • Public Getters and Setters: These methods provide controlled access to the private variables.
    • getAddress(): Returns the address.
    • setAddress(String address): Sets the address.
    • getNumberOfRooms(): Returns the number of rooms.
    • setNumberOfRooms(int numberOfRooms): Sets the number of rooms.
    • getArea(): Returns the area.
    • setArea(double area): Sets the area.
  • calculatePrice Method: Takes a price per square meter and returns the total price of the house.
  • Main Method: Tests the functionality of the House class by creating an instance, setting its properties, calculating the price, and printing the details.

Note on Encapsulation

Encapsulation works in the above exercise by:

  • Hiding Data: The private instance variables address, numberOfRooms, and area are not accessible directly from outside the class.
  • Controlled Access: The public getter and setter methods provide controlled access to the private variables, allowing for validation and modification when necessary.
  • Data Integrity: Encapsulation helps maintain data integrity by ensuring that the internal state of the object can only be changed through well-defined methods.

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Creating a Java Desktop Class with Encapsulation and RAM Upgrade.
Next: Creating a Java Account Class with Encapsulation and Transaction Methods.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/encapsulation/java-encapsulation-exercise-11.php