Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me solve this problem ASAP!!! and please make the required files as indicated in the end of the problem. Book Chapters Write a

Please help me solve this problem ASAP!!! and please make the required files as indicated in the end of the problem.

Book Chapters

Write a program that encapsulates a Book with its title and its Chapters name with the pages in each chapter.

Chapter

A Chapter object should get instantiated using the name of the Chapter and the number of pages. The name can be a maximum of 50 characters long and the pages of the chapters cannot be more than 200. If any of this information is not provided or if they are invalid, the object is created in an invalid state.

A Chapter is displayed as follows (if Valid):

  • Name in 50 spaces, filled with dots '.', left-justified
  • Number of pages in 3 spaces right-justified
  • Newline

If the chapter is invalid, it should just print: "invalid Chapter" and go to newline.

For example if the Chapter name is Java and it is 60 pages, it should print as follows:

Java.............................................. 60

Book

A Book is created using its name, number of chapters and an array of Chapters ( with "number of chapters" elements).

The length of the name or the number of chapters is unknown.

The following are the public functions required for the Class Book.

bool isEmpty()const;

returns true if the Book is empty or invalid, otherwise, it returns false;

Book& addChapter(const char* chapter_name, int noOfPages);

Adds a Chapter to a Book only if the name and noOfPages are valid values (to create a Chapter with).

void display()const;

Displays the Book as follows, if it is valid:

  • prints "Book Name: "
  • prints the book name value
  • NEWLINE
  • prints "No of chapters: "
  • prints the number of chapters
  • prints all the Chapters in individual lines If the book is invalid it will print:
  • otherwise, prints "Invalid book object";

Note

Make sure all the allocated memory is deleted when the object goes out of scope.

Tester program:

// Workshop #4: // Version: 0.9 // Date: 2021/05/01 // Author: Nargis Khan // Description: // This file tests the DIY section of your workshop // Revision: Fardad Soleimanloo // Date: 2021/10/01 /////////////////////////////////////////////////// #include #include #include"Chapter.h" #include"Book.h" using namespace std; using namespace sdds; void displayBooks(const Book* b, int num); void DMAtest(Book& B); int main() { Chapter cp1[] = { Chapter("C++",75), Chapter("Java",60), Chapter("JavaScript",80) , Chapter() }; Chapter cp2[] = { Chapter("C++",75), Chapter("Java",60), Chapter("JavaScript",80), Chapter("",100) }; Chapter cp3[] = { Chapter("C++",75), Chapter("Java",60), Chapter("JavaScript",80), Chapter("Bad1", 0) }; Chapter cp4[] = { Chapter("C++",75), Chapter("Java",60), Chapter("JavaScript",80), Chapter(nullptr, 40) }; Chapter cp[] = { Chapter("C++",75), Chapter("Java",60), Chapter("JavaScript",80) }; Book bo[] = { Book(nullptr,3,cp), Book("Bad1", 0,cp), Book("Bad2", 4,cp1), Book("Bad3", 4,cp2), Book("Bad4", 4,cp3), Book("Bad5", 4,cp4), Book("Object Oriented Programming Languages", 3, cp) }; cout << "Displaying invalid Book objects" << endl; cout << "-------------------------------------------------------" << endl; displayBooks(bo, 5); cout << "Displaying valid Book object with valid chapters" << endl; cout << "-------------------------------------------------------" << endl; displayBooks(&bo[6], 1); cout << "Adding a new Chapter to the Book" << endl; cout << "-------------------------------------------------------" << endl; bo[6].addChapter("Python", 100); bo[6].display(); cout << "-------------------------------------------------------" << endl; cout << "Testing DMA" << endl; cout << "-------------------------------------------------------" << endl; cout << "Press enter to continue: "; cin.ignore(); DMAtest(bo[6]); return 0; } void DMAtest(Book& B) { for (int i = 0; i < 2000; i++) B.addChapter("testing", i%202); B.display(); } void displayBooks(const Book* b, int num) { int i; for (i = 0; i < num; i++) { b[i].display(); } cout << "-------------------------------------------------------" << endl; } 

The output:

Displaying invalid Book objects ------------------------------------------------------- Invalid Book object Invalid Book object Invalid Book object Invalid Book object Invalid Book object ------------------------------------------------------- Displaying valid Book object with valid chapters ------------------------------------------------------- Book Name: Object Oriented Programming Languages No of Chapters: 3 Chapter name Pages C++............................................... 75 Java.............................................. 60 JavaScript........................................ 80 ------------------------------------------------------- Adding a new Chapter to the Book ------------------------------------------------------- Book Name: Object Oriented Programming Languages No of Chapters: 4 Chapter name Pages C++............................................... 75 Java.............................................. 60 JavaScript........................................ 80 Python............................................ 100 ------------------------------------------------------- Testing DMA ------------------------------------------------------- Press enter to continue: Book Name: Object Oriented Programming Languages No of Chapters: 1985 Chapter name Pages C++............................................... 75 Java.............................................. 60 JavaScript........................................ 80 Python............................................ 100 testing........................................... 1 testing........................................... 2 testing........................................... 3 testing........................................... 4 testing........................................... 5 testing........................................... 6 testing........................................... 7 testing........................................... 8 ... any many more lines will be printed 

Files To Be Made:

Book.h Book.cpp Chapter.h Chapter.cpp BookTester.cpp

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

3. Explain what a commodity arbitrager does.

Answered: 1 week ago