Programming Practice: Building a Book Structure

What are the main classes and methods involved in modeling a book, chapter, and page structure?

The given question describes a program that models a book, chapter, and page structure. Here is a breakdown of the main classes and their methods:

Answer:

1. The Page class: - The Page class has an ArrayList that stores each word on the page. - Methods: - public void addWords(String words): This method accepts a String and parses each word. - public String getWords(): This method returns all the words on the page as a single String. - public void clearWords(): This method removes all the words from the ArrayList. 2. The Chapter class: - The Chapter class allows users to add Page objects to the Chapter. - Methods: - public void addPage(Page myPage): This method adds a Page object to the Chapter. - public Page getPage(int pageNum): This method returns the page at the specified index. - public void removePage(int pageNum): This method removes the page at the specified index. - public void clearPages(): This method deletes all the pages in the chapter. - public int getNumPages(): This method returns the number of pages in the chapter. 3. The Book class: - The Book class allows the user to add Chapter objects to the book. - Methods: - public void addChapter(Chapter myChapter): This method adds a Chapter to the Book. - public Chapter getChapter(int chapterNum): This method returns the chapter at the specified index. - public void removeChapter(int chapterNum): This method removes the chapter at the specified index. - public void deleteChapters(): This method deletes all the chapters in the book. - public void setName(String name): This method sets the name of the book. - public String getName(): This method returns the name of the book. - public int getNumChapters(): This method returns the number of chapters in the book.

The program consists of three main classes (Page, Chapter, and Book) that model a book structure, allowing users to add pages, chapters, and manipulate the book's contents. The Tester class facilitates the creation and manipulation of the book. The program outputs information about the book, such as its title, number of chapters, and number of pages, before and after the deletion of a chapter.

The given question describes a program that models a book, chapter, and page structure. Here is a breakdown of the main classes and their methods:

1. The Page class: - The Page class has an ArrayList that stores each word on the page. - Methods: - public void addWords(String words): This method accepts a String and parses each word. Words are added to the ArrayList. - public String getWords(): This method returns all the words on the page as a single String, separated by spaces. - public void clearWords(): This method removes all the words from the ArrayList. 2. The Chapter class: - The Chapter class allows users to add Page objects to the Chapter. - The class has an ArrayList that holds Page objects. - It includes the following methods: - public void addPage(Page myPage): This method accepts a Page object and adds it to the ArrayList for the chapter. - public Page getPage(int pageNum): This method returns the page at the specified index. If the page number is out of range, it returns the first or last page, depending on whether the numbering system is 0 or 1 based. - public void removePage(int pageNum): This method removes the page at the specified index. It checks if the page number is in the correct range before removing the page. - public void clearPages(): This method deletes all the pages in this chapter. - public int getNumPages(): This method returns the number of pages in the chapter. 3. The Book class: - The Book class allows the user to add Chapter objects to the book. - It has an ArrayList that contains objects of the Chapter class and a String that represents the name of the book. - The class includes the following methods: - public void addChapter(Chapter myChapter): This method adds the supplied Chapter to the book object. - public Chapter getChapter(int chapterNum): This method returns the chapter at the specified index. If the chapter number is out of range, it returns the first or last chapter, depending on whether the numbering system is 0 or 1 based. - public void removeChapter(int chapterNum): This method removes the chapter at the specified index. It checks if the chapter number is in the correct range before removing the chapter. - public void deleteChapters(): This method deletes all the chapters in this book. - public void setName(String name): This method sets the name of the book. - public String getName(): This method returns the name of the book. - public int getNumChapters(): This method returns the number of chapters in the book.

← Creating classes in object oriented programming What workplace attitude does suzette fail to demonstrate →