This is a program that I need to write in order to help me on my final exam for
ID: 3767728 • Letter: T
Question
This is a program that I need to write in order to help me on my final exam for an intermediate c++ course. I would love some guidance or a step by step walkthrough on how to do this because I'm very confused. Definitely would appreicate the help. In the drop box link I have the three files. Thanks!
https://www.dropbox.com/sh/4s8e0c33ndx74po/AADUggAVubPIn68zdXXiNocwa?dl=0
You work as a programmer for a startup company and your project manager has given you this legacy code (above) that was written by a programmer who left the company. Your project manager needs you to convert the sequence class to a template so the class can be used by multiple client programs, each possibly using different data types.
Additionally, you need to write a second client for this class called sequence_test2.cpp. It needs to be exactly the same as the sequence_test.cpp except that it must take in a different data type. You do not have to create a template for the client, just create a second .cpp and manually change the data type where necessary in that second .cpp file. When you complete this client, the project manager will be able to successfully use the sequence class with either the sequence_test.cpp or your new sequence_test2.cpp. Note: You cannot include both clients simultaneously in the same project in your IDE. Swap them out for testing or create two separate projects. Note: You will need to make some minor changes to the sequence_test.cpp file to make it work with your templated class.
Add some exception handling added to the clients as needed. An exception should be thrown and handled in a catch block in the following situations:
If the user types in an invalid option for the menu
If the user types in something that is not a number
Your program should meet the functional requirements and include a minimum of the following:
The complete sequence class converted to a template (10 points)
A client sequence_test2.cpp that accepts numbers from the user with exception handling as described above (10 points)
Either client (sequence_test.cpp OR sequence_test2.cpp) should successfully run with the templated sequence class. (5 points)
Explanation / Answer
//Creating a template class T
#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
namespace main_savitch_3
{
template<class T> // Mention template class
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef T value_type;
static const int CAPACITY = 30;
// CONSTRUCTOR
sequence( );
// MEMBER FUNCTIONS
void showSequence();
void attach(value_type entry);
value_type current( );
void remove_current( );
int size( );
private:
value_type data[CAPACITY];
int used;
};
}
#endif
The sequence.cp file will not change, and will remain as it is, and we have canged the header file for the same. sequence.h
#include <string>
#include <iostream>
#include "sequence1.h"
namespace main_savitch_3
{
sequence::sequence( )
{
used = 0;
}
void sequence::attach(value_type entry)
{
data[used++] = entry;
}
sequence::value_type sequence::current( )
//replace sequence::value_type with typename value_type for your template
{
return data[used-1];
}
void sequence::remove_current( )
{
used--;
}
int sequence::size( )
{
return used;
}
void sequence::showSequence()
{
std::string display = "";
for (int i = 0; i<used; i++)
std::cout << data[i] << " ";
}
}
This is sequence_test.cpp with Exception Handling
// FILE: sequence_test.cxx
// An interactive test program for the new sequence class
#include <cctype> // Provides toupper
#include <iostream> // Provides cout and cin
#include <cstdlib> // Provides EXIT_SUCCESS
#include "sequence1.h" // With value_type defined as double
using namespace std;
using namespace main_savitch_3;
// PROTOTYPES for functions used by this test program:
void print_menu( );
// Postcondition: A menu of choices for this program has been written to cout.
char get_user_command( );
// Postcondition: The user has been prompted to enter a one character command.
// The next character has been read (skipping blanks and newline characters),
// and this character has been returned.
double get_data( );
// Postcondition: The user has been prompted to enter a real number. The
// number has been read, echoed to the screen, and returned by the function.
int main( )
{
sequence test; // A sequence that we’ll perform tests on
char choice; // A command character entered by the user
cout << "I have initialized an empty sequence of real numbers." << endl;
do
{
print_menu( );
choice = toupper(get_user_command( ));
switch (choice)
{
case 'C': cout << "Current is " << test.current() << '.' << endl;
break;
case 'S': cout << "Size is " << test.size( ) << '.' << endl;
break;
case 'A': test.attach(get_data());
break;
case 'P': test.showSequence();
break;
case 'R': test.remove_current( );
cout << "The current item has been removed." << endl;
break;
default: cout << choice << " is invalid." << endl;
}
}
while ((choice != 'Q'));
return EXIT_SUCCESS;
}
void print_menu( )
// Library facilities used: iostream.h
{
cout << endl; // Print blank line before the menu
cout << "The following choices are available: " << endl;
cout << " C Print the result from the current( ) function" << endl;
cout << " P Print a copy of the entire sequence" << endl;
cout << " S Print the result from the size( ) function" << endl;
cout << " A Attach a new number with the attach(...) function" << endl;
cout << " R Activate the remove_current( ) function" << endl;
cout << " Q Quit this test program" << endl;
}
char get_user_command( )
// Library facilities used: iostream
{
char command;
cout << "Enter choice: ";
cin >> command; // Input of characters skips blanks and newline character
if(!command.compare("C") && !command.compare("P") && !command.compare("S") !command.compare("A") !command.compare("R") !command.compare("Q")
{
bool isNumber = true;
for(string::const_iterator k = command.begin(); k != command.end(); ++k)
isNumber &&= isdigit(*k);
if(!isNumber)
throw "Exception: User has entered an Invalid Option";
else throw "Exception: USer entered a number";
}
return command;
}
double get_data( )
// Library facilities used: iostream
{
double result;
cout << "Please enter a value for the sequence: ";
cin >> result;
cout << result << " has been read." << endl;
return result;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.