Creating a class for a book with private properties

Report a typo

Create a default class named 'Book' that represents a book in a library. The class should contain three private properties: 'title' (String), 'author' (String), and 'numberOfPages' (int). The class should also contain public methods to set (mutator) and get (accessor) these properties. The mutator methods should not allow to set an empty string for 'title' and 'author,' or a negative or zero value for 'numberOfPages'. Instead, if such a value is attempted to be set, the methods should keep the properties unchanged. Once the class is created, create an object of this class, set the properties using the mutator methods and get values using the accessor methods. Take three lines of input, the first line is a string (title), the second line is also a string (author), and the third line is an integer (numberOfPages). The output should be three lines, printing the title, author, and the number of pages in the same order.

Sample Input 1:

The Great Gatsby
F. Scott Fitzgerald
180

Sample Output 1:

The Great Gatsby
F. Scott Fitzgerald
180

Sample Input 2:

To Kill a Mockingbird
Harper Lee
281

Sample Output 2:

To Kill a Mockingbird
Harper Lee
281
Write a program in Java 17
import java.util.Scanner;

// Creating class
class Book {
// set up three private properties

// getters and setters go here
// Remember:
// 1. They must not allow empty string for 'title' and 'author'.
// 2. They must not allow negative or zero value for 'numberOfPages'.
// 3. If such values are attempted to be set, the property should remain unchanged.
}

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Create an object of the Book class
Book book = new Book();

// Take Title, Author and numberOfPages as next inputs and set them using the mutator methods
// Your code here

// Then use the accessor methods to get and print these values.
// Your code here

scanner.close();
}
}

Create a free account to access the full topic