Hello everyone! I need help with a c++ program. There are two parts to it. Assig
ID: 3871244 • Letter: H
Question
Hello everyone! I need help with a c++ program. There are two parts to it.
Assignment Overview
In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of health care provider data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on.
Purpose
This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of an array of objects as a class data member, and introduces the concept of object serialization or "binary I/O".
Set Up
As in Assignment 1, you should create a subdirectory to hold your files for Assignment 2.
In that directory, make a symbolic link to the data file for this part of the assignment:
(Note that this is a platform-specific binary file. It will most likely not work anywhere other than our UNIX system.)
In this assignment, you will be creating several source code and header files, as described below. You can create each of these files separately using the nano editor, just as you did on Assignment 1.
This assignment description assumes that you are writing Assignment 2 from scratch. However, the Provider class used in this assignment is largely identical to the class from Assignment 1, so you should be able to reuse some of your code from that assignment.
Program
For this assignment, you will need to write three source code files as well as two header files. Each of these files is relatively short, but many inexperienced programmers are overwhelmed by the idea of writing a program as multiple files. "Where do I start?!!" is a common refrain. This assignment sheet attempts to walk you through the steps of writing a multi-file program.
The steps outlined below should not be thought of as a purely linear process, but rather an iterative one - For example, work a little on Step 1, then a little on Step 2, then test what you've written (Step 3).
Step 1: Write the Provider class declaration
The Provider class represents information about a health care Provider. The code for the Provider class will be placed in two separate files, which is the norm for non-template C++ classes.
The header file for a class contains the class declaration, including declarations of any data members and prototypes for the methods of the class. The name of the header file should be of the form ClassName.h (for example, Provider.h for the header file of the Provider class).
A skeleton for the Provider.h file is given below. As shown, a header file should begin and end with header guardsto prevent it from accidentally being #included more than once in the same source code file (which would produce duplicate symbol definition errors). The symbol name used in the header guards can be any valid C++ name that is not already in use in your program or the C/C++ libraries. Using a name of the format CLASSNAME_H (like PROVIDER_H in the code below) is recommended to avoid naming conflicts.
Data Members
The data members for the Provider class are the same as those used in Assignment 1:
Provider number (a character array with room for 6 characters PLUS the null character).
Specialty (a character array with room for 40 characters PLUS the null character).
Name (a character array with room for 40 characters PLUS the null character). The name will store a concatenated version of the provider's last name, first name, middle initial (if present), and title
First part of the provider address (a character array with room for 40 characters PLUS the null character)
Second part of the provider address (a character array with room for 30 characters PLUS the null character)
City (a character array with room for 20 characters PLUS the null character)
State (a character array with room for 2 characters PLUS the null character)
Zip code (a character array with room for 5 characters PLUS the null character)
Phone number (a character array with room for 14 characters PLUS the null character)
Note: Make sure you code your data members in THE EXACT ORDER LISTED ABOVE and with THE EXACT SAME DATA TYPES AND SIZES. The code for this class will be reused on the next assignment, and if you only make the name array 40 characters long instead of 41 or make the city array 31 characters instead of 21, your program for the next assignment will not work correctly.
Method Prototypes
The Provider class declaration should (eventually) contain public prototypes for all of the methods in the Provider.cpp source code file described in Step 2 below.
Step 2: Write the Provider class implementation
The source code file for a class contains the method definitions for the class. The name of the source code file should be of the form ClassName.cpp or ClassName.cc (for example, Provider.cpp for the source code file of the Provider class).
The Provider class implementation should (eventually) contain definitions for all of the methods described below. Make sure to #include "Provider.h" at the top of this file.
Default Constructor
The constructor you wrote for Assignment 1 should work with no modifications required.
print()
This method is the same as the one from Assignment 1, with one minor change: since the method will not change any data members of the object that calls it, the method should be declared as const.
To declare a method of a class to be const, put the keyword const at the end of the method's prototype and at the end of the first line of its definition. For example:
getProviderNumber()
This accessor method takes no parameters. It should return the provider number data member. In C++, the usual return data type specified when you are returning the name of a character array is const char* or "pointer to a character constant" (since returning an array's name will convert the name into a pointer to the first element of the array, which in this case is data type char. This method should be const, since it does not change any data members.
getSpecialty()
This accessor method takes no parameters. It will have a return data type of const char*. It should return the specialty data member. This method should be const, since it does not change any data members.
getName()
This accessor method takes no parameters. It will have a return data type of const char*. It should return the name data member. This method should be const, since it does not change any data members.
Step 3: Write the ProviderDB class declaration
The ProviderDB class represents a database of Provider objects. Like the Provider class, the code for this class will be placed in two separate files.
Place the class declaration in a header file called ProviderDB.h. Like the file Provider.h you wrote in Step 1, this file should begin and end with header guards to prevent it from accidentally being #included more than once in the same source code file.
After the header guard at the top of the file but before the class definition, make sure to #include "Provider.h".
Data Members
The ProviderDB class should have the following two private data members:
An array of exactly 40 Provider objects
An integer that specifies the number of providers actually stored in the array
Note: Once again, make sure you code your data members in THE EXACT ORDER LISTED ABOVE and with THE EXACT SAME DATA TYPES. If you make the array 41 elements long instead of 40, the input data will not be read correctly.
Method Prototypes
The ProviderDB class declaration should (eventually) contain public prototypes for all of the methods in the ProviderDB.cpp source code file described in Step 4 below.
Step 4: Write the ProviderDB class implementation
The ProviderDB class implementation should (eventually) contain definitions for all of the methods described below. Make sure to #include "ProviderDB.h" at the top of this file.
ProviderDB default constructor
This "default" constructor for the ProviderDB class takes no parameters. Like all C++ constructors, it does not have a return data type.
This constructor is called to create an empty database, so this method should set the number of providers data member to 0.
(If you initialize the number of providers data member to 0 when you declare it, this method's body can be empty. You still need to code the method with an empty body.)
Alternate ProviderDB constructor
Write a second constructor for the ProviderDB class that takes one parameter: a pointer to a constant character (which will point to an array of characters that contains the name of an existing database file). Like all C++ constructors, this constructor does not have a return data type.
This constructor should do the following:
Declare an input file stream variable (the code below assumes it is named inFile).
Open the file stream for binary input. The file to open is the one whose name was passed into the constructor as a C string variable. Check to make sure the file was opened successfully as usual.
Read the database file into your ProviderDB object. You can do this with a single statement:
Close the file stream.
print()
This method takes no parameters and returns nothing. It does not change any data members, so it should be declared as const
This method should first print a descriptive header line (e.g., "Health Care Provider Listing"). It should then loop through the array of Provider objects and print each of the elements that contain provider data. Here we see some of the power of object-oriented programming: since each element of the array is an object, we can call a method for that object. We've already written a print() method for the Provider class, so printing an element of the array is as easy as calling print() for the array element. The syntax for calling a method using an array element that is an object is pretty straightforward:
Step 5: Write the main program
The main() function should be placed in a file named assign2.cpp. Since most of the logic of the program is embedded in the two classes you wrote, the main() routine logic is extremely simple.
Create a ProviderDB object using the alternate constructor you wrote. Pass the filename string "providerdb"as an argument to the constructor.
Call the print() method for the ProviderDB object.
Part one output:
Part two:
Assignment Overview
In Part 2 of this assignment, you will add some functionality to the ProviderDB class and add some logic to your main() routine to test that functionality.
Purpose
This part of the assignment introduces the insertion sort algorithm.
Step 1: Add methods to ProviderDB
Add the following three methods to the ProviderDB class
sortByProviderNumber()
This method takes no parameters and returns nothing.
This method should sort the array of Provider objects in ascending order by provider number using the insertion sort algorithm.
The sort code linked to above sorts an array of integers called numbers of size size. You will need to make a substantial number of changes to that code to make it work in this program:
This will be a method, not a function. Change the parameters for the method to those described above.
In the method body, change the data type of bucket to Provider. This temporary storage will be used to swap elements of the array of Provider objects.
In the method body, change any occurrence of numbers to the name of your array of Provider objects and size to numProviders (or whatever you called the data member that tracks the number of valid Provider objects stored in the array).
The comparison of providerArray[j-1] and bucket will need to use the C string library function strcmp() to perform the comparison. Also, you'll need to use the getProviderNumber() method to access the providerNumber data member within each Provider object. The final version of the inner for loop should look something like this:
It is legal to assign one Provider object to another; you don't need to write code to copy individual data members.
sortBySpecialty()
This method takes no parameters and returns nothing.
This method should sort the array of Provider objects in descending order by specialty using the insertion sort algorithm.
sortByName()
This method takes no parameters and returns nothing.
This method should sort the array of Provider objects in ascending order by name using the insertion sort algorithm.
Step 2: Add some method calls to the main program
The main() routine logic will now look like this:
Create a ProviderDB object using the alternate constructor you wrote. Pass the filename string "providerdb"as an argument to the constructor.
Call the print() method for the ProviderDB object.
Call the sortByProviderNumber() method for the ProviderDB object.
Call the print() method for the ProviderDB object.
Call the sortBySpecialty() method for the ProviderDB object.
Call the print() method for the ProviderDB object.
Call the sortByName() method for the ProviderDB object.
Call the print() method for the ProviderDB object.
Thank you so much in advance, will upvote quickly!
Explanation / Answer
// As the question is a multi-part/step question, the first four parts/steps have been answered
#ifndef PROVIDER_H
#define PROVIDER_H
//*****************************************************************
// FILE: Provider.h
// AUTHOR: your name
// LOGON ID: your z-ID
// DUE DATE: due date of assignment
//
// PURPOSE: Declaration for the Provider class, which represents
// information about a health care provider.
//*****************************************************************
/// STEP 1
class Provider
{
// define the data members
char providerNumber[7];
//
char speciality[41];
//
char name[41];
//
char firstPart_provider_addr[41];
//
char secondPart_provider_add[31];
//
char city[21];
//
char state[3];
//
char zipcode[6];
//
char phone[15];
//
// prototype of the methods, all public
public:
//
Provider(); // default constructor, does not take any arguments
//
void print() const;
//
const char* getProviderNumber() const; // method returns the provider number which is a char array
//
const char* getSpeciality() const;
//
const char* getName() const;
//
};
#endif // PROVIDER_H
// STEP-2
//*****************************************************************
// FILE: Provider.cpp
// AUTHOR: your name
// LOGON ID: your z-ID
// DUE DATE: due date of assignment
//
// PURPOSE: Implementation of Provider class methods and constructors
//*****************************************************************
#include <iostream>
#include <cstdio>
#include "Provider.h"
using namespace std;
//
Provider::Provider()
{
// default constructor, empty
}
//
// question does not mention the logic for this function, so a logic based on the output shown has been written, may need slight modification
void Provider::print() const
{
//
cout<<"#"<<providerNumber<<endl;
//
cout<<name<<endl;
//
cout<<speciality<<endl;
//
cout<<firstPart_provider_addr<<endl;
//
cout<<secondPart_provider_add<<endl;
//
cout<<city<<","<<state<<zipcode<<endl;
//
cout<<phone<<endl;
}
//
// method returns the provider number
const char* Provider::getProviderNumber() const
{
//
return providerNumber;
}
//
// method returns provider specialty
const char* Provider::getSpeciality() const
{
//
return speciality;
}
//
// method returns provider name
const char* Provider::getName() const
{
//
return name;
}
//
// STEP-3
//*****************************************************************
// FILE: ProviderDB.h
// AUTHOR: your name
// LOGON ID: your z-ID
// DUE DATE: due date of assignment
//
// PURPOSE: Declaration for the ProviderDB class
//
//*****************************************************************
#include "Provider.h"
#ifndef PROVIDERDB_H
#define PROVIDERDB_H
class ProviderDB
{
//
Provider provider[40]; // array of 40 Provider class objects
//
int provider_count; // counter for actual number of objects stored
//
public:
//
// default constructor prototype
ProviderDB();
//
ProviderDB(const char*); // parameterized constructor
//
void print() const;
//
};
#endif
// STEP-4
//*****************************************************************
// FILE: ProviderDB.cpp
// AUTHOR: your name
// LOGON ID: your z-ID
// DUE DATE: due date of assignment
//
// PURPOSE: Implementation of ProviderDB methods and constructors
//*****************************************************************
#include <iostream>
#include <cstdio>
#include <fstream>
#include "ProviderDB.h"
using namespace std;
//
// implementation of default constructor
ProviderDB::ProviderDB()
{
provider_count = 0; // initialize the object counter
}
//
// implementation of the parameterized constructor
ProviderDB::ProviderDB(const char* fname)
{
// define an input file stream object that opens the input file in binary mode
ifstream inFile(fname, ios::in|ios::binary);
//
// check if the stream was created successfully
if(!inFile)
cout<<"Error creating input file stream"<<endl;
//
// read contents into the invoking providerDB object
inFile.read((char*) this, sizeof(ProviderDB));
//
inFile.close(); // close the file stream
}
//
void ProviderDB::print() const
{
//
int i;
//
cout<<"Health Care Provider Listing"<<endl;
//
for(i = 0; i < provider_count; i++)
{
//
provider[i].print(); // call the print() method for each provider object in the array
}
}
//
STEP-5
//#include <iostream>
#include "Provider.h"
#include "ProviderDB.h"
using namespace std;
int main()
{
//cout << "Hello world!" << endl;
//
ProviderDB provider_db("providerdb"); // create a providerDB object
//
provider_db.print(); // call print method for the providerDB object
//
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.