216k views
4 votes
Use Else and IF

Create an object-oriented program that allows a user to keep track of the book they are currently reading. The program should allow the user to change the name of the book, change the number of pages in the book, and the current page they are currently on in the book. The number of pages in a book must be greater than 0 and also greater than or equal to the current page the reader is currently on. The current page must be greater than 0 and also less than or equal to number of pages in the book. Any time a user enters an invalid value that value will be ignored and will not be saved.

Since this program is object-oriented it must be developed as a class called Book. This class should store name of the book, the number of pages, and current page as instance variables. There should be an instance variable for each these. These instance variables should be made private to prevent invalid values from being stored in them, but there should be ways to access and mutate these instance variables using public non-static methods. There should be a separate method to access each instance variable. There should be a separate method to mutate each instance variable. This class should also have a default constructor which sets name instance variable to No Name Yet and sets number of pages instance variable to 0 and sets current page instance variable to 0.

The second part of this program should be developed in a class called BookDemo. This class will have a main method inside of it where an object of the Book class will be created. Then the user will be given options to update the name, number of pages, and the current page. There should be an option for each of these. Finally, there should be an option to look at all the information about the book. This main method should also have a loop to allow this program to continue running until the user chooses the last option which is to exit the program. If the user chooses an invalid option then an error message should be printed to the screen. The program should continue executing until the user chooses to exit the program.

User Wacko
by
8.2k points

1 Answer

4 votes

Final Answer:

Create a class named `Book` with private instance variables for the book's name, number of pages, and current page. Implement public non-static methods to access and mutate these variables. Include a default constructor setting the name to "No Name Yet," number of pages to 0, and current page to 0. Ensure the program validates and ignores invalid values for the number of pages and the current page. Develop a separate class named `BookDemo` with a main method creating a `Book` object. Provide options for the user to update the name, number of pages, and current page, with a loop to continue until the user chooses to exit.

Step-by-step explanation:

The `Book` class is designed to encapsulate the properties of a book and ensure data integrity through private instance variables and public methods for access and mutation. The default constructor sets initial values and prevents invalid inputs. The `BookDemo` class, with its main method, serves as the program interface. It creates an instance of the `Book` class and allows the user to interact with the book's information. The program runs in a loop until the user chooses to exit, handling invalid options with error messages.

The use of object-oriented principles enhances modularity, encapsulation, and reusability. The `Book` class encapsulates the book's properties, ensuring controlled access through methods. The `BookDemo` class orchestrates user interactions, promoting a clean and organized design. This structure facilitates code maintenance and readability, making it easy to expand or modify the program in the future. The loop ensures the program's continuous execution until the user decides to exit, providing a user-friendly experience.

User Ragesh Chakkadath
by
7.8k points