Write a class “Book” to model books. This book object will have two instance var
ID: 3699222 • Letter: W
Question
Write a class “Book” to model books. This book object will have two instance variables: a title (String) and authors (Array of Strings). Write a default constructor which sets title to “Test” and authors to null. Write a constructor receiving values for these fields. Lastly, implement getter and setter methods for your instance variables. In “Book”, write a method “bookToString”, which returns a string with the books information (in the format below. Use ” to have a quote character in a string. “The Lord of the Rings” by J.R.R. Tolkien “Nixonland” by Rick Perlstein “Berenstain Bears” by Stan Berenstain & Jan Berenstain Write a class “Bookshelf” to model a bookshelf. This bookshelf object will have two instance variables: size (int) and books (ArrayList of Books). Write a default constructor which sets the size to 2 and initializes the ArrayList. Write a parameterized constructor which receives a single parameter for size and initializes the ArrayList. Write a getter method for the size instance variable. (Don’t write a setter method, you can’t change the size of a bookshelf after it’s been created!) Write a getter method for the books instance variable. Rather than a single setter method, write three specific methods to modify the contents of the bookshelf: a public void method addBook which takes a single book parameter and adds it to the shelf variable if there is room in the shelf, a public void method removeBook which takes no parameters and removes the first book on the shelf (the book at position 0 in the books ArrayList), and a public void method emptyBookshelf which takes no parameters and removes all of the books from the shelf (empties the books ArrayList). Neither the “Book” class or the “Bookshelf” class can have a main method. If you want to test your “Book” and “Bookshelf” classes, you may make a separate class with a main method. You do not need to submit this separate class.
Explanation / Answer
#include <iostream>
#include <string.h>
using namespace std;
class book //Book class for single book information
{
string Title; //Title of the book
string Authors[5]; //Authors of the book
public:
void getter(); //To get a books information
void setter(string TitleInIt, string AuthorsInIt[]); //To set a books information
string bookToString();
};
void book::book() //Default constructor
{
Title = "Test";
for(int i=0; i<5; i++) Authors[i]="";
}
void book::book(string TitleInIt, string AuthorsInIt[]) //Parametric constructor
{
Title=TitleInIt;
for(int i=0; i<5; i++)Authors[i]=AuthorsInIt[i];
}
string book:: bookToString() //This will convert the book title to be printed with quotes
{
int i=0;
string s=Title;
while (s[i] != '')
{
++i;
}
for(int k=i+1; k>-1;)
{
s[k]=s[--k];
}
s[0]=""";
s[i]=""";
return s;
}
void book::getter() //Prints books information
{
string s;
s= bookToString();
cout<<"The book : "<<s<<endl;
cout<<"by Authors : "<<endl;
for(int i=0; i<5; i++)
cout<<"Author "<<i<<" : "<<Authors[i]<<endl;
}
void book::setter(string TitleInIt, string AuthorsInIt[]) //Sets books information
{
Title=TitleInIt;
for(int i=0; i<5; i++)Authors[i]=AuthorsInIt[i];
}
class bookshelf //class for many books in a set
{
int size; //size of bookshelf
book books[size]; //array of book object for storing information of many books
public:
bookshelf getter(); // print information of all books in the bookshelf
void addbook(book); // Add a book to shelf at the end
void removeBook(); //removes book from begining
void emptyBookshelf(); // empty the bookshelf
};
void bookshelf::bookshelf()
{
size=2;
for(int i=0; i<2; i++)
{
books[i].Title="";
for(int j=0; j<5; j++)
books[j].Authors ="";
}
}
void bookshelf::bookshelf(int s, book *b)
{
size=s;
for(int i=0; i<size; i++)
{
books[i].Title=b[i].Title;
for(int j=0; j<5; j++)
books[j].Authors =b[j].Authors;
}
}
bookshelf bookshelf:: getter()
{
cout<<"Books in this Shelf are: "<<endl;
for(int i=0; i<100; i++)
{
books[i].getter();
}
}
void bookshelf:: addbook(book b)
{
size++;
books[size].Title= b.Title;
for(int i=0; i<5; i++) books[size][i]=b.Authors;
}
void bookshelf:: removeBook()
{
books[0]="";
for(int i=0; i<size;)
{
books[i]=books[i++];
}
size--;
}
void bookshelf::emptyBookshelf()
{
for(int i=0; i<size; i++)
books[i]="";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.