Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

record_player flashdrive in C++ Following the class diagram shown below, create

ID: 3598070 • Letter: R

Question

record_player flashdrive in C++

Following the class diagram shown below, create the class RecordPlayer. An RecordPlayer represents a stereocomponent that can play vinyl records.. Please review the class and the sample driver code. As explained in class, there is a specific order to the way the class methods should be called. In other words, you can't plop the Needle unless there actually is a record on the player and the recordplayer is turned on. You also can't return the Needle unless there is actually a record on the player and the recordplayer is turned on. Your class should enforce these rules and write errors to cout as they occur. A sample driver for this class is shown below. You should probably make a more thorough driver to test your class better.

Your submission should follow this organization:

Please show 3 files !!!!!

main.cpp this is your driver file

record_player.h

record_player.cpp

RecordPlayer

void turnOn( );
void turnOff( );
bool isPoweredOn( );
void affixPlatter( string record );
void plopNeedle( );
void returnNeedle( );

Sample Driver Code

RecordPlayer r;
r.turnOn();
r.affixPlatter( "Barry Manilow I" );
r.plopNeedle( );
r.returnNeedle( );
r.turnOff( );

cout << "--Test 2--" << endl;

RecordPlayer badr;
badr.plopNeedle( );

cout << "--Test 3--" << endl;

RecordPlayer badr1;
badr1.turnOn( );
badr1.plopNeedle( );

cout << "--Test 4--" << endl;

RecordPlayer badr2;
badr2.returnNeedle( );

cout << "--Test 5--" << endl;

RecordPlayer badr3;
badr3.turnOn( );
badr3.returnNeedle( );

Sample Driver Output

RecordPlayer

RecordPlayer( );

void turnOn( );
void turnOff( );
bool isPoweredOn( );
void affixPlatter( string record );
void plopNeedle( );
void returnNeedle( );

bool my_isOn;
string my_Record;
bool my_NeedleIsOnTheRecord;

Sample Driver Code

cout << "--Test 1--" << endl;

RecordPlayer r;
r.turnOn();
r.affixPlatter( "Barry Manilow I" );
r.plopNeedle( );
r.returnNeedle( );
r.turnOff( );

cout << "--Test 2--" << endl;

RecordPlayer badr;
badr.plopNeedle( );

cout << "--Test 3--" << endl;

RecordPlayer badr1;
badr1.turnOn( );
badr1.plopNeedle( );

cout << "--Test 4--" << endl;

RecordPlayer badr2;
badr2.returnNeedle( );

cout << "--Test 5--" << endl;

RecordPlayer badr3;
badr3.turnOn( );
badr3.returnNeedle( );

Sample Driver Output

--Test 1--
recordplayer is on...
recordplayer has album Barry Manilow I
recordplayer is playing album Barry Manilow I
recordplayer has stopped playing album
recordplayer is off...
--Test 2--
you can't plop the needle without being powered on!
--Test 3--
you can't plop the needle without an album!
--Test 4--
you can't return the needle without being powered on!
--Test 5--
you can't return the needle without an album!

Explanation / Answer


* main.cpp
* Corey Johns
* COP4530 Fall 2012
*
* Provides user interface.
*/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include "wordweights.h"
#define DEBUG 0
// Create the document map
std::map<std::string, WordWeights> docs;
// Function prototypes
void analyze(std::string);
void read_file(std::string&, std::ifstream&);
void query(std::string);
int main(int argc, char** argv){
if ( argc < 2 ){
std::cout << "Usage: Retrieve [FILE]... ";
return 1;
}
// Analyze files given as argument
for(int i = 1; i < argc; i++){
analyze(argv[i]);
}
// Enter user prompt
std::string input;
do {
getline(std::cin, input);
switch(input[0]){
case 'a':
if ( input.length() < 2 ){
std::cout << "Error: No files specified! ";
break;
}
analyze(input.substr(2, input.length() - 2));
break;
case 'q':
if ( input.length() < 2 ){
std::cout << "Error: Number of documents, word list not specified! ";
break;
}
query(input.substr(2, input.length() - 2));
break;
case 'x':
break;
default:
std::cout << "Error: Invalid command. ";
break;
}
} while (input[0] != 'x');
return 0;
}
void analyze(std::string s){
// First split up the parameters
std::string delimiters = " ";
std::size_t current;
std::size_t next = -1;
std::string filename;
do {
current = next + 1;
next = s.find_first_of(delimiters, current);
filename = s.substr(current, next-current);
// Check if the file exists
std::ifstream infile(filename.c_str());
if (!infile){
std::cout << "File " << filename << " does not exist ";
}
else {
// File exists, map the filename to a WordWeights obj and read in file
docs[filename] = WordWeights(); // Will replace the results if filename exists
read_file(filename, infile);
infile.close();
}
} while (next != std::string::npos);
}
/* read_file
*
* Given a filename and ifstream handle, read in file to a WordWeights object
*/
void read_file(std::string &filename, std::ifstream &infile){
std::string s, word;
std::string delimiters = " !()-:;",.?/ ";
while (infile.good()){
getline(infile, s);
std::size_t current;
std::size_t next = -1;
do {
current = next + 1;
next = s.find_first_of(delimiters, current);
word = s.substr(current, next-current);
docs[filename].Add(word);
} while (next != std::string::npos);
}
if (DEBUG)
docs[filename].Print();
}
void query(std::string s){
// Vector to hold parameters
std::vector<std::string> word_list;
// Split up the words
std::string delimiters = " ";
std::size_t current;
std::size_t next = -1;
int num_docs;
int count = 0;
do {
current = next + 1;
next = s.find_first_of(delimiters, current);
if (count == 0){ // First parameter is the number of documents to return
num_docs = std::atoi(s.substr(current, next-current).c_str() );
count++;
}
else
word_list.push_back( s.substr(current, next-current) );
} while (next != std::string::npos);
// Results map; document => sum of frequencies
std::map<std::string, double> results;
std::map<std::string, WordWeights>::iterator it;
for ( it=docs.begin(); it != docs.end(); it++){
// For each document
std::vector<std::string>::iterator w_it;
for ( w_it=word_list.begin(); w_it != word_list.end(); w_it++){
double curr_word_freq = it->second.WordFreq(*w_it);
if(curr_word_freq == 0){ // If any word is not in document, set freq to 0
results[it->first] = 0;
break;
}
results[it->first] += curr_word_freq;
// ... YIKES!
}
}
// Sort results by another map... There is probably a better way
// but maps by definition can't sort by value
std::map<double, std::string> sorted_results;
std::map<std::string, double>::const_iterator r_it;
int num_results = 0;
for ( r_it=results.begin(); r_it != results.end(); r_it++){
// Only put it in sorted_results if non-zero!
if(r_it->second != 0){
sorted_results[r_it->second] = r_it->first;
num_results++;
}
}
if(num_results == 0){
std::cout << "No matching document ";
return;
}
count = 0;
std::map<double, std::string>::const_reverse_iterator sr_it;
for ( sr_it=sorted_results.rbegin(); sr_it != sorted_results.rend(); sr_it++){
std::cout << sr_it->second << " " << sr_it->first << std::endl;
count++;
if(count == num_docs)
break;
}