Question

In: Computer Science

1. Define a class counterType to implement a counter. Your class must have a private data...

1. Define a class counterType to implement a counter. Your class must have a private data member counter of type int and functions to set counter to the value specified by the user, initialize counter to 0, retrieve the value of counter, and increment and decrement counter by one. The value of counter must be nonnegative.


2. Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication. Design a class bookType that defines the book as an ADT.

Each object of the class bookType can hold the following information about a book: title, up to four authors, publisher, ISBN, price, and number of copies in stock. To keep track of the number of authors, add another member variable.

Include the member functions to perform the various operations on objects of type bookType. For example, the usual operations that can be performed on the title are to show the title, set the title, and check whether a title is the same as the actual title of the book. Similarly, the typical operations that can be performed on the number of copies in stock are to show the number of copies in stock, set the number of copies in stock, update the number of copies in stock, and return the number of copies in stock. Add similar operations for the publisher, ISBN, book price, and authors. Add the appropriate constructors and a destructor (if one is needed).

Write the definitions of the member functions of the class bookType.

Write a program that uses the class bookType and tests various operations on the objects of the class bookType. Declare an array of 100 components of type bookType. Some of the operations that you should perform are to search for a book by its title, search by ISBN, and update the number of copies of a book.

Solutions

Expert Solution

1. the code snippet for the first part:-

#include <iostream>

using namespace std;

/**

class definition for CounterType

*/

class CounterType {

private:

// private variables

int count;

int counter = 0;

public:

// default constructor

CounterType() { count = 0; }

// constructor which accept count value and

// set the value to count

CounterType(int c) { count = c; }

// incrementing counter

void increseCounter() { counter++; }

// decreasing the count

int decreaseCount() {

// if count reach to then returning false

if (count == 0)

return 0;

count--;

return 1;

}

// methods for get count and get counter

int getCount() { return count; }

int getCounter() { return counter; }

// method for printing the info

void print_c(ostream &cout) {

cout << "Count : " << getCount() << "\n";

cout << "Counter : " << getCounter() << "\n\n";

}

};

// test method

int main(int argc, char const *argv[]) {

int value;

cout << "Please enter a COUNT value: ";

cin >> value;

CounterType c(value);

// using while loop printing

// count and counter

while (1) {

c.print_c(cout);

c.increseCounter();

int t = c.decreaseCount();

// if count reach to 0 then nothing program will exit

if (!t) {

break;

}

}

return 0;

}// end of program

2. Code for the second part:-

#include<iostream>
#include<string>

usingnamespace std;


classbookType//class to store information about the book
{
string title; //stories the title of an oject
string Authors[4]; //stores the Authors names
int n_Author; //stores number of Authors
string publishr; //Stores the name of Publisher
string ISBN;//store ISBN NUmber
double price; //Stores the price
int copies;
public:
bookType(); //constructor
void setTitle(string); //function declaration to set the title
string r_title(); //function declaration to return the title
void showTitle(); //function declaration to show the title

void SetAuthors(int); //function declaration to set the Author(s)
void ShowAuthors()const; //function declaration to show the Autho(s)

void setPublishr(string); //function declaration to set the Publisher name
void showPublishr(); //function declaration to show the Publisher name

void setIBSN(string); //function declaration to set the IBSN No.
string r_IBSN(); //function declaration to return the IBSN No.
void showIBSN(); //function declaration to show the IBSN No.

void setPrice(double); //function declaration to set the price
double R_price(); //function declaration to return the price
void showPrice(); //function declaration to show the price

void setCopies(int); //function declaration to set the No.of Copies
int r_copies(); //function declaration to return the No.of Copies
void showCopies(); //function declaration to show the No.of Copies
void u_Copies(int); //function declaration to update the No.of Copies

void print(); //function declaration to print the all data about the book

};
void search();
bookType A[3]; //creating object using Aray


int main()
{
string ti;
int n,c=1;
double p;
char co;
for (int i = 0; i < 3; i++) //loop to take the data of multiple books
{
cout <<"BOOK No."<< i + 1 << endl;
cout <<"Enter the Title :";
getline(cin, ti); //Takes the title from the user
A[i].setTitle(ti); //set the title in private member of a class
cout <<"Enter the Numbers of Authors : ";
while (!(cin >> n) || n<0) { //validation checking of number of authors
cin.clear();
cin.ignore(999, '\n');
cout <<"Invalid data type! \nPlease Enter the Numbers of Authors again :";
}
if (n > 4 || n <= 0)
{
cout <<"Enter the Numbers of Authors in the range of 1~4 :";
cin >> n;
}
A[i].SetAuthors(n); //set the Authors NAme

cout <<"Enter the Name of Publisher :";
getline(cin, ti); //Takes the Nmae of the publisher from the user
A[i].setPublishr(ti); //sets the publisher name
cout <<"Enter the ISBN no : ";
getline(cin, ti); //Takes the ISBN no. from the user
A[i].setIBSN(ti); //ststhe ISBN name
cout <<"Enter the price : ";
while (!(cin >> p) || p<0) { //validation checking of price entered by the user
cin.clear();
cin.ignore(999, '\n');
cout <<"Invalid data type! \nPlease Enter the Price again :";
}
A[i].setPrice(p); //sets the price of the book
cout <<"Enter the number of copies :";
while (!(cin >> n) || n<0) { //validation checking of no of copies entered by the user
cin.clear();
cin.ignore(999, '\n');
cout <<"Invalid data type! \nPlease Enter the number of copies again :";
}
A[i].setCopies(n); //set the no. of copies
cout << endl;
cin.ignore();
}
for (int i = 0; i < 3; i++) //loop to diplay the data of multiple books
{
A[i].print();
cout << endl;
}
search(); //calling of search function
system("pause");
return 0;
}
voidbookType::setTitle(stringt) //Functon to set the title
{
title = t;
}
stringbookType::r_title() //Functon to return the title
{
return title;
}
voidbookType::showTitle() //Functon to show the title
{
cout <<"\tTitle : "<< r_title() << endl;
}

voidbookType::SetAuthors(intn) //Functon to set the names of author(s)
{
cin.ignore();
for (int i = 0; i <n; i++)
{
cout <<"\tAuthor #"<< i + 1<<" : ";
getline(cin, Authors[i]);
}

}
voidbookType::ShowAuthors() const//Functon to display the names of Authors
{
cout <<"\tAuthor(s)";
if (Authors[0] != "")
cout << Authors[0];
if (Authors[1] != "")
cout <<", "<<Authors[1];
if (Authors[2] != "")
cout <<", "<< Authors[2];
if (Authors[3] != "")
cout <<", "<< Authors[3];
cout << endl;
}
voidbookType::setPublishr(stringp) //Functon to set the name of publisher
{
publishr = p;
}
voidbookType::showPublishr() //Functon to show the name of publisher
{
cout <<"\tPublisher : "<< publishr << endl;
}
voidbookType::setIBSN(stringi) //Functon to set ISBN no.
{
ISBN = i;
}
stringbookType::r_IBSN() //Functon to return ISBN no.
{
return ISBN;
}
voidbookType::showIBSN() //Functon to return ISBN no.
{
cout <<"\tISBN : "<< r_IBSN() << endl;
}
voidbookType::setPrice(doublep) //Functon to set the price of a book
{
price = p;
}
doublebookType::R_price() //Functon to return the price of a book
{
return price;
}
voidbookType::showPrice() //Functon to show the price of a book
{
cout <<"\tPrice : "<< R_price() << endl;
}
voidbookType::setCopies(intn) //Functon to set the no. of copies of a book
{
copies = n;
}
intbookType::r_copies() //Functon to return the no. of copies of a book
{
return copies;
}
voidbookType::u_Copies(intu) //Functon to update the no. of copies of a book
{
copies=u;
}
voidbookType::showCopies()
{
cout <<"\tCopies in Stock : "<< r_copies() << endl;
}
voidbookType::print() ////Functon to print the book information
{
showTitle();
ShowAuthors();
showPublishr();
showIBSN();
showPrice();
showCopies();
}
bookType::bookType() //constructor definition
{
title = "";
Authors[0] = "";
Authors[1] = "";
Authors[2] = "";
Authors[3] = "";
n_Author = 0;
ISBN = "0-00000-00";
price = 0.00;
copies = 0;
}
void search() //function to serach the book by ISBN no. or by title
{
int ch,c=1,n;
char co;
string ti;
cout <<"Enter 1 for to Search by 'Title' and '2' for to search by IBSN No. : ";
cin >> ch; //takes the choice
switch (ch)
{
case 1: //executes if user enters 1
{
cin.ignore();
cout <<"Enter the title :";
getline(cin, ti);
for (int i = 0; i < 3; i++)
{
string t = A[i].r_title();
if (ti == t)
{
c = 0;
A[i].print();
cout <<"Did you want of modify the no of copies (y/n) :";
cin >> co;
switch (co)
{
case'y':
{
cout <<"Enter the nmuber of Copies :";
cin >> n;
A[i].u_Copies(n);
cout << endl <<"Modified DATA "<< endl << endl;
A[i].print();
break;
}
case'n':
break;
default:
cout <<"Invliad Choice"<< endl;
}
}
if (c == 1 && i == 2)
cout <<"Not found a book with this title"<< endl;
}
break;
}
case 2: //executes if user enters 2
{
cin.ignore();
cout <<"Enter the IBSN :";
getline(cin, ti);
for (int i = 0; i < 3; i++)
{
string t = A[i].r_IBSN();
if (t == ti)
{
c = 1;
A[i].print();
cout <<"Did you want of modify the no of copies (y/n) :";
cin >> ch;
switch (ch)
{
case'y':
{
cout <<"Enter the nmuber of Copies :";
cin >> n;
A[i].u_Copies(n);
cout << endl <<"Modified DATA "<< endl << endl;
A[i].print();
break;
}
case'n':
break;
default:
cout <<"Invliad Choice"<< endl;
}
}
if (c = 1 && c == 2)
cout <<"Not found a book with this ISBN NO."<< endl;
}
break;
}
default: //exectes if user enter invalid choice
cout <<"Invliad choice"<< endl;
}

}


Related Solutions

A. Define and implement the class Alarm as follows: The class Alarm consists of two private...
A. Define and implement the class Alarm as follows: The class Alarm consists of two private member variables: description of type string and atime of type Time. The class Alarm also includes the following public member functions: 1. print to print out the alarm’s description and hour, minute, and second of the alarm time. 2. setDescription to accept a string argument and use it to set the description member variable. 3. setAtime to accept three integers (for hour, minute, and...
A. Define and implement the class Alarm as follows: The class Alarm consists of two private...
A. Define and implement the class Alarm as follows: The class Alarm consists of two private member variables: description of type string and atime of type Time. The class Alarm also includes the following public member functions: 1. print to print out the alarm’s description and hour, minute, and second of the alarm time. 2. setDescription to accept a string argument and use it to set the description member variable. 3. setAtime to accept three integers (for hour, minute, and...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max   // 2.) A constructor which takes initial values for // min and max // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is greater than...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max // // 2.) A constructor which takes initial values for // min and max // // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is...
Define a class called Counter whose internal "count" variable is a basic integer counter. This class...
Define a class called Counter whose internal "count" variable is a basic integer counter. This class track that count from its instantiation in the constructor (can be set to any positive integer, or 0). The count should never be allowed to be negative. Include methods that will set the counter to 0, increase the count by 1, and decrease the count by 1. Include an accessor method that returns the current count value (getCount()). There should be no input /...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
#data structure 1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named...
#data structure 1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named PriorityQueue The priority queue MUST be implemented using a linked list. 2 test program checks that a newly constructed priority queue is empty o checks that a queue with one item in it is not empty o checks that items are correctly entered that would go at the front of the queue o checks that items are correctly entered that would go at the...
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT