w3resource

Creating a Java Book Class with Multiple Constructors

Java Constructor: Exercise-3 with Solution

Constructor Overloading
Write a Java program to create a class called "Book" with instance variables title, author, and price. Implement a default constructor and two parameterized constructors:

  • One constructor takes title and author as parameters.
  • The other constructor takes title, author, and price as parameters.
  • Print the values of the variables for each constructor.

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;

    // Default constructor
    public Book() {
        // Initialize title to "Unknown"
        this.title = "Unknown";
        // Initialize author to "Unknown"
        this.author = "Unknown";
        // Initialize price to 0.0
        this.price = 0.0;
    }

    // Parameterized constructor (title, author)
    public Book(String title, String author) {
        // Initialize title with the provided parameter
        this.title = title;
        // Initialize author with the provided parameter
        this.author = author;
        // Initialize price to 0.0
        this.price = 0.0;
    }

    // Parameterized constructor (title, author, price)
    public Book(String title, String author, double price) {
        // Initialize title with the provided parameter
        this.title = title;
        // Initialize author with the provided parameter
        this.author = author;
        // Initialize price with the provided parameter
        this.price = price;
    }

    // Main method to test the Book class
    public static void main(String[] args) {
        // Create a new Book object using the default constructor
        Book book1 = new Book();
        // Print the values of the instance variables for book1
        System.out.println("Book1 Title: " + book1.title);
        System.out.println("Book1 Author: " + book1.author);
        System.out.println("Book1 Price: " + book1.price);

        // Create a new Book object using the parameterized constructor (title, author)
        Book book2 = new Book("Amatka", "Karin Tidbeck");
        // Print the values of the instance variables for book2
        System.out.println("Book2 Title: " + book2.title);
        System.out.println("Book2 Author: " + book2.author);
        System.out.println("Book2 Price: " + book2.price);

        // Create a new Book object using the parameterized constructor (title, author, price)
        Book book3 = new Book("Altered Carbon", "Richard K. Morgan", 18.99);
        // Print the values of the instance variables for book3
        System.out.println("Book3 Title: " + book3.title);
        System.out.println("Book3 Author: " + book3.author);
        System.out.println("Book3 Price: " + book3.price);
    }
}

Output:

Book1 Title: Unknown
Book1 Author: Unknown
Book1 Price: 0.0
Book2 Title: Amatka
Book2 Author: Karin Tidbeck
Book2 Price: 0.0
Book3 Title: Altered Carbon
Book3 Author: Richard K. Morgan
Book3 Price: 18.9

Explanation:

  • Define the Book class:
    • The Book class is defined with the keyword class.
  • Private instance variables:
    • The Book class has three private instance variables: title (a String), author (a String), and price (a double).
  • Default constructor:
    • The default constructor Book() is defined.
    • Inside the default constructor, title is initialized to "Unknown".
    • author is initialized to "Unknown".
    • price is initialized to 0.0.
  • Parameterized constructor (title, author):
    • A parameterized constructor Book(String title, String author) is defined.
    • Inside the constructor, this.title is initialized with the provided title parameter.
    • this.author is initialized with the provided author parameter.
    • price is initialized to 0.0.
  • Parameterized constructor (title, author, price):
    • Another parameterized constructor Book(String title, String author, double price) is defined.
    • Inside the constructor, this.title is initialized with the provided title parameter.
    • this.author is initialized with the provided author parameter.
    • this.price is initialized with the provided price parameter.
  • Main method:
    • The main method is defined to test the Book class.
    • Three Book objects (book1, book2, book3) are created using different constructors.
    • The values of the instance variables for each Book object are printed to the console.

Note on Constructors:

In the above exercise, the constructors for the Book class work by:

  • Default Constructor: When a new Book object is created without any arguments, the default constructor is called, initializing title to "Unknown", author to "Unknown", and price to 0.0.
  • Parameterized Constructors: When a new Book object is created with specific values for title and author, or title, author, and price, the respective parameterized constructor is called, initializing the instance variables with the provided values.
  • Encapsulation: By keeping the instance variables private and initializing them through constructors, the class ensures that the variables are set to meaningful values when an object is created, promoting controlled initialization and data integrity.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java Constructor Previous: Creating a Java Dog Class with Parameterized Constructor.
Java Constructor Next: Creating a Java Student Class with Constructor Chaining.

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/constructor/java-constructor-exercise-3.php