Question

In: Computer Science

Part 1 (Objective C++ and please have output screenshot) The purpose of this part of the...

Part 1 (Objective C++ and please have output screenshot)

The purpose of this part of the assignment is to give you practice in creating a class. You will develop a program that creates a class for a book. The main program will simply test this class.

The class will have the following data members:

  1. A string for the name of the author
  2. A string for the book title
  3. A long integer for the ISBN

The class will have the following member functions (details about each one are below:)

  1. A default constructor
  2. A Print function which prints out all the information about the book.
  3. A GetData function which reads information from a file into the data members.
  4. A function GetISBN that returns the integer containing the ISBN. (This will be needed in Part 2).

You must create your program using the following three files:

book.h – used for declaring your class. In this header file, the declarations of the class and its members (both data and functions) will be done without the definitions. The definitions should be done in the book.cpp file.

book.cpp – contains the definitions of the member functions:

  1. The default constructor will initialize the author's name to “No name”, the title to "Unknown title", and the ISBN to 0.
  2. The Print function will display all of the information about a book in a clear format. This will be a const function because it does not change the data members. For formatting purposes, you may assume that no name will have more than 20 characters and that no book title will have more than 50 characters.
  3. The GetData function will have the input file as a parameter, will read information from the file and put appropriate values into the data members. (See below for the format of the data file.)
  4. The GetISBN function will simply return the long integer containing the ISBN. It also will be a const function.

Mp8bookDriver.cpp – should contain the main program to test the class.

It should declare two book objects (book1 and book2) using the default constructor. Call the print function for book1 (to show that the default constructor is correct). Open the input file and call the GetData function for book2 and then print its information. Finally, test the GetISBN function for book2 and output the result returned from the function.

Format of Data file
The name of the data file is mp7book.txt
It has data for one book arranged as follows:

  • The name is on one line by itself (hint: use getline).
  • The title is on a line by itself.
  • The ISBN is on the third line.

mp7book.txt

Jane Smith
History Of This World
12349876

Get this part of the program working and save all the files before starting on Part 2. The output should be similar to the following:

Testing the book class by (your name)

The information for book 1 is:
No name Unknown title 0
The information for book 2 is:
Jane Smith History Of The World 12349876
book2 has ISBN 12349876
Press any key to continue

Part 2

Now you will use the book class to create an array of books for a small library. Note that the book.h and book.cpp files should not have to be changed at all - you just have to change the main program in the Mp8bookDriver.cpp file.

There is a new data file, mp7bookarray.txt. It contains the information for the books in the library using the same format as described above for each book. There will be exactly 10 books in the file.

Declare an array of books that could hold 10 book objects. Open the new data file and use a loop to call the GetData function to read the information about the books into the objects in the array. Print out the list of books in the library in a nice format. Notice that the books are arranged in order by ISBN in the data file.

Now imagine customers coming into the library who want to know whether a particular book is in the collection. Each customer knows the ISBN of the book. Open the third data file (mp8bookISBN.txt) which contains ISBN's, read each one, and use a binary search to find out whether the book is in the array. If it is found, print out all the information about the book, if not, print an appropriate message. Then repeat the process for each of the ISBN's until you get to the end of the file.

mp8bookarray.txt

H. M. Deitel
C++ How to Program
130895717
Judy Bishop
Java Gently
201593998
Jeff Salvage
The C++ Coach
201702894
Thomas Wu
Object-Oriented Programming with Java
256254621
Cay Horstmann
Computing Concepts with C++
471164372
Gary Bronson
Program Development and Design
534371302
Joyce Farrell
Object-Oriented Programming
619033614
D. S. Malik
C++ Programming
619062134
James Roberge
Introduction to Programming in C++
669347183
Nell Dale
C++ Plus Data Structures
763714704

mp8bokkISBN.txt
201593998
888899999
763714704
111122222
256254621
130895717
488881111
534371302
619033614

Solutions

Expert Solution

Part1:

book.h File--------:

class Book
{
   private :
  
   public :
   string name;
   string title;
   int ISBN;
   Book();
   void Print();
   void GetData(char* str);
   int GetISBN();
};

book.cpp-----------:

include "book.h"

Book::Book()
{
   name = "No Name";
   title = "Unknown title";
   ISBN = 0;
}
void Book::Print()
{
   cout<<"\n"<<name<<" "<<title
   <<" "<<ISBN<<endl;
}
void Book::GetData(char* file)
{
   ifstream ifs(file);
   string line;
   int i = -1, j = 1;
   if(ifs)
   {
       while(getline(ifs,line) && fileCounter == j++)
       {
           if(++i== 0)
               name = line;
           else if(++i == 1)
               title = line;
           else
               ISBN = atoi(line.c_str());
           fileCounter++;
       }
   }
}
int Book::GetISBN()
{
   return ISBN;
}

Mp8bookDriver.cpp----:

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int fileCounter = 1;

#include "book.h"

//using namespace std;
int main()
{
       Book book1 = Book();
       Book book2 = Book();
       cout<<"Testing the book class by(ABC)"<<endl;
       cout<<"The information for book1 is:"<<endl;
       book1.Print();
       char* filePath = (char *)"mp7book.txt";
       book2.GetData(filePath);
       cout<<"The information for book2 is:"<<endl;
       book2.Print();
       int isbn = book2.GetISBN();
       cout<<"Book2 has ISBN :"<<isbn<<endl;
       cout<<"Press any key to continue"<<endl;
       getchar();
}

Part 2:-::

book.h

class Book
{
   private :
  
   public :
   string name;
   string title;
   int ISBN;
   Book();
   void Print();
   void GetData(char* str);
   int GetISBN();
};

book.cpp

include "book.h"

Book::Book()
{
   name = "No Name";
   title = "Unknown title";
   ISBN = 0;
}
void Book::Print()
{
   cout<<"\n"<<name<<" "<<title
   <<" "<<ISBN<<endl;
}
void Book::GetData(char* file)
{
   ifstream ifs(file);
   string line;
   int i = -1, j = 1;
   if(ifs)
   {
       while(getline(ifs,line) && fileCounter == j++)
       {
           if(++i== 0)
               name = line;
           else if(++i == 1)
               title = line;
           else
               ISBN = atoi(line.c_str());
           fileCounter++;
       }
   }
}
int Book::GetISBN()
{
   return ISBN;
}

Mp8bookDriver.cpp

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int fileCounter = 1;

#include "book.h"

int binarySearch(Book book[], int s, int e, int num)
{
if (s <= e) {
int mid = (s+e)/2;
if (book[mid].ISBN == num)   
return mid ;
if (book[mid].ISBN > num)
return binarySearch(book, s, mid-1, num);
if (book[mid].ISBN > num)
return binarySearch(book, mid+1, e, num);
}
return -1;
}
int main()
{
       Book book[10] = Book();      
       char* filePath = (char*)"mp8bookarray.txt";
       for(int i=0; i<10; i++)
           book[i].GetData(filePath);
       for(int i=0; i<10; i++)
           book[i].Print();
       ifstream ifs("mp8bookISBN.txt");
       string line = "";
       cout<<"Searchng by ISBN Number"<<endl;
       while(ifs>>line)
       {
           int id = binarySearch(book, 0, 9, atoi(line.c_str()));
           if(id>-1)          
               book[id].Print();
           else
               cout<<"No book found for "<<line<<" ISBN"<<endl;
                      
       }
       cout<<"Press any key to continue"<<endl;
       getchar();
}


Related Solutions

C++(screenshot output please) Part 2 - Tree programs Program 1 Implement a tree using an array...
C++(screenshot output please) Part 2 - Tree programs Program 1 Implement a tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template
Please in C++ thank you! Please also include the screenshot of the output. I have included...
Please in C++ thank you! Please also include the screenshot of the output. I have included everything that's needed for this. Pls and thank you! Write a simple class and use it in a vector. Begin by writing a Student class. The public section has the following methods: Student::Student() Constructor. Initializes all data elements: name to empty string(s), numeric variables to 0. bool Student::ReadData(istream& in) Data input. The istream should already be open. Reads the following data, in this order:...
PLEASE do the following problem in C++ with the screenshot of the output. THANKS! Assuming that...
PLEASE do the following problem in C++ with the screenshot of the output. THANKS! Assuming that a text file named FIRST.TXT contains some text written into it, write a class and a method named vowelwords(), that reads the file FIRST.TXT and creates a new file named SECOND.TXT, to contain only those words from the file FIRST.TXT which start with a lowercase vowel (i.e., with 'a', 'e', 'i', 'o', 'u'). For example, if the file FIRST.TXT contains Carry umbrella and overcoat...
This is C++, please insert screenshot of output please. Part1: Palindrome detector Write a program that...
This is C++, please insert screenshot of output please. Part1: Palindrome detector Write a program that will test if some string is a palindrome Ask the user to enter a string Pass the string to a Boolean function that uses recursion to determine if something is a palindrome or not. The function should return true if the string is a palindrome and false if the string is not a palindrome. Main displays the result ( if the string is a...
Programming Steps: (In Java) (Please screenshot your output) A. Files required or needed to be created:...
Programming Steps: (In Java) (Please screenshot your output) A. Files required or needed to be created:    1. Artist.java (Given Below)    2. p7artists.java (Input file to read from)    3. out1.txt (Output file to write to) B. Java programs needed to writeand create:    1. MyArtistList.java:    - Contains the following:        1. list - public, Arraylist of Artist.        This list will contain all entries from "p7artists.txt"        2. Constructor:        A constructor that accepts one...
3. Add mutex locks to tprog2.c to achieve synchronization, and screenshot the output tprog2.c #include <stdio.h>...
3. Add mutex locks to tprog2.c to achieve synchronization, and screenshot the output tprog2.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> void* print_i(void *ptr) { printf("1: I am \n"); sleep(1); printf("in i\n"); } void* print_j(void *ptr) { printf("2: I am \n"); sleep(1); printf("in j\n"); } int main() { pthread_t t1,t2; int rc1 = pthread_create(&t1, NULL, print_i, NULL); int rc2 = pthread_create(&t2, NULL, print_j, NULL); exit(0); }
Objective in JAVA (Please show output and have the avergae number of checks also.): Implement both...
Objective in JAVA (Please show output and have the avergae number of checks also.): Implement both linear search and binary search, and see which one performs better given an array 1,000 randomly generated whole numbers (between 0-999), a number picked to search that array at random, and conducting these tests 20 times.  Each time the search is conducted the number of checks (IE number of times the loop is ran or the number of times the recursive method is called) needs...
The Programming Language is C++ Objective: The purpose of this project is to expose you to:...
The Programming Language is C++ Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student...
The Programming Language is C++ Objective: The purpose of this project is to expose you to:...
The Programming Language is C++ Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student...
I HAVE ALREADY CORRECTLY COMPLETED PART A AND B PLEASE COMPLETE PART C ONLY Part A...
I HAVE ALREADY CORRECTLY COMPLETED PART A AND B PLEASE COMPLETE PART C ONLY Part A In late 2020, the Nicklaus Corporation was formed. The corporate charter authorizes the issuance of 6,000,000 shares of common stock carrying a $1 par value, and 2,000,000 shares of $5 par value, noncumulative, nonparticipating preferred stock. On January 2, 2021, 4,000,000 shares of the common stock are issued in exchange for cash at an average price of $10 per share. Also on January 2,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT