w3resource

Creating a Java Book Class with Encapsulation and Discount Method

Java Encapsulatiion: Exercise-8 with Solution

Write a Java program to create a class called “Book” with private instance variables title, author, and price. Provide public getter and setter methods to access and modify these variables. Add a method called applyDiscount() that takes a percentage as a parameter and reduces the price by that percentage.

Sample Solution:

Java Code:

Book.java

// Define the Book class
public class Book {
    // Private instance variables
    private String title;
    private String author;
    private double price;

    // Public getter for the title variable
    public String getTitle() {
        return title;
    }

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

    // Public getter for the author variable
    public String getAuthor() {
        return author;
    }

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

    // Public getter for the price variable
    public double getPrice() {
        return price;
    }

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

    // Method to apply a discount to the price
    public void applyDiscount(double percentage) {
        if (percentage > 0 && percentage <= 100) {
            this.price -= this.price * (percentage / 100);
        }
    }
 }

Main.java

// Main method to test the Book class
 public class Main {
    public static void main(String[] args) {
        // Create a new Book object
        Book book = new Book();

        // Set the title, author, and price of the book
        book.setTitle("Java Programming");
        book.setAuthor("Imani Matey");
        book.setPrice(35.0);

        // Apply a 10% discount to the book price
        book.applyDiscount(10);

        // Print the details of the book
        System.out.println("Title: " + book.getTitle());
        System.out.println("Author: " + book.getAuthor());
        System.out.println("Price: $" + book.getPrice());
    }
 }

Output:

Title: Java Programming
Author: Imani Matey
Price: $31.5

Explanation:

  • Private Instance Variables: The title, author, and price variables are declared as private to ensure encapsulation.
  • Public Getters and Setters: These methods provide controlled access to the private variables.
    • getTitle(): Returns the title.
    • setTitle(String title): Sets the title.
    • getAuthor(): Returns the author.
    • setAuthor(String author): Sets the author.
    • getPrice(): Returns the price.
    • setPrice(double price): Sets the price.
  • applyDiscount Method: Takes a percentage and reduces the price by that percentage.
  • Main Method: Tests the functionality of the Book class by creating an instance, setting its properties, applying a discount, and printing the details.

Note on Encapsulation

Encapsulation works in the above exercise by:

  • Hiding Data: The private instance variables title, author, and price 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: Implementing a Student Class with Grade Validation.
Next: Creating a Java Smartphone Class with Encapsulation and Storage Increase.

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-8.php