Question
/CarRadio.cpp #include #include #include #include CarRadio.h CarRadio::CarRadio() { for( int i = 0; i { button[i].AMButton = 530; } for( int j = 0; j
/CarRadio.cpp
#include
#include
#include
#include "CarRadio.h"
CarRadio::CarRadio()
{
for( int i = 0; i
{
button[i].AMButton = 530;
}
for( int j = 0; j
{
button[j].FMButton = 87.9;
}
current_station = 530;
strcpy(frequency, "AM");
volume = 0;
on = false;
}
void CarRadio::PowerToggle()
{
if( on == false )
{
on = true;
}
else
{
on = false;
}
}
bool CarRadio::IsRadioOn()
{
return on;
}
int CarRadio::SetVolume()
{
char buf[20] = "";
printf(" Enter the volume level (0 - 100). ");
fgets(buf, sizeof buf, stdin);
volume = atoi(buf);
if( volume
{
volume = 0;
return 0;
}
if( volume > 100 ) //if user enters volume greater than 100, volume = 100
{
volume = 100;
return 2;
}
return 1;
}
void CarRadio::ToggleFrequency()
{
if( strcmp(frequency, "AM") == 0 )
{
strcpy(frequency, "FM");
current_station = 87.9;
}
else
{
strcpy(frequency, "AM");
current_station = 530;
}
}
int CarRadio::SetButton(int button_num)
{
if( (button_num >= 0) && (button_num
{
if( strcmp("AM", frequency) == 0 )
{
button[button_num].AMButton = current_station;
}
else
{
button[button_num].FMButton = current_station;
}
return 1;
}
return 0;
}
int CarRadio::SelectCurrentStation(int button_num)
{
if( (button_num >= 0) && (button_num
{
if( strcmp("AM", frequency) == 0 )
{
current_station = button[button_num].AMButton;
}
else
{
current_station = button[button_num].FMButton;
}
return 1;
}
return 0;
}
void CarRadio::ShowCurrentSettings()
{
if( on == true )
{
printf(" Radio is on. ");
}
else
{
printf(" Radio is off. ");
}
printf(" Frequency: %s ", frequency);
printf("Volume: %d ", volume);
printf("Current Station: %.1f %s ", current_station, frequency);
printf("AM Button Settings: ");
for( int i = 0; i
{
printf("%d) %6d ", i + 1, button[i].AMButton);
}
printf(" FM Button Settings: ");
for( int j = 0; j
{
printf("%d) %6.1f ", j + 1, button[j].FMButton);
}
}
void CarRadio::ScanUp()
{
if( strcmp("AM", frequency) == 0 )
{
//if current_station is 1700, the current_station becomes 530
if( current_station == 1700 )
{
current_station = 530;
}
else
{
current_station = current_station + 10;
}
}
else
{
//if the current_station is 107.9, the current_station becomes 87.9
//Note: car radios jump .2 for the FM. That's how it's modeled here.
if( current_station >= 107.9 )
{
current_station = 87.9;
}
else
{
current_station = current_station + .2;
}
}
printf(" Current station: %f %s ", current_station, frequency);
}
//CarRadio.h
#ifndef _CARRADIO_H
#define _CARRADIO_H
struct Button
{
int AMButton;
float FMButton;
};
class CarRadio
{
private:
Button button[5];
double current_station;
char frequency[3];
int volume;
bool on;
public:
//sets the each button to the lowest frequency, sets the current station, sets the
//frequency to AM, sets the volume to 0 and sets on to false
CarRadio();
//sets on to true
void PowerToggle();
//returns a true if the radio is currently powered on, and false if the radio is in
//the off position
bool IsRadioOn();
//toggles frequency between AM and FM and sets current station
void ToggleFrequency();
//sets button with current station by being passed a button number
int SetButton(int button_num);
//sets current station by being passed a button number
int SelectCurrentStation(int button_num);
//sets volume
int SetVolume();
//shows volume, button settings, current station, AM or FM
void ShowCurrentSettings();
//changes frequency up in increments of .2 for FM, 10 for AM
void ScanUp();
};
#endif
//driver.cpp
#include
#include
#include "CarRadio.h"
#include
using namespace std;
enum menuItems
{
kMenuNothing = 0, // special constant for initialization only
kMenuTogglePower = 1, kMenuSetVolume, kMenuToggleAMFM, kMenuSetButton, kMenuSelectButton,
kMenuShowCurrentSettings, kMenuScanUp, kMenuQuit
};
int main()
{
int volume_OK = 0;
int button_OK = 0;
int button_num = 0;
menuItems choice = kMenuNothing;
char buf[20] = {0};
CarRadio jazzy; bool on = false; cout
}
if( (choice != kMenuShowCurrentSettings) && (choice != kMenuQuit) ) jazzy.ShowCurrentSettings();
}while( choice != kMenuQuit );
printf(" Goodbye! "); return 0;
}
modify the c program using the requirements in the picture.
.pdf a, onlinec Anybody can learn[C Learn l Code.org Learn to Code and H( Upgrading Your car Hadio Retrieve and use the fle from eConestoga as your starting polnt. The CarRadio example provided above is an okay.. but is significantly flawed In a lot of ways. Your task In this assignment general by improving It in the following ways: Generalizations: Change the names of the two class-related files to AmFmRadio. h and AnFnRadio. cpp. Change the name of the class to AmFmRadio (this class no longer models a radio within a car) Rename the struct Button to struct . Freqs, since we're going to use the struct for more than buttons. Change AMButton to AMFreq and FMButton to FMF req. o Additions & Replacements: Replace the existing constructor with two constructors that initialize all private data and that use the following parameters to initialize the appropriate data: one that takes a single bool parameter that indicates whether the radio should be on or not when instantiated. Give this parameter a default of false (as discussed in Module 1 (default parameters)). one that takes a single bool parameter that indicates whether the radio should be on or not when instantiated and an array of 5 struct Fregs that contains the initial button values o o Add a destructor that simply displays "Destroying AmFmRadio" Make sure that the instance of AmFmRodio created in the driver.cpp mainline is instantiated powered on. Add a method called ScanDown() that behaves similarly to Scanup(), except that the scanning is down. . When the AM and FM band reach the minimum frequency, they roll-over to the maximum frequency in the band Make menu entry #8 correspond to-scan Down". Make menu entry #9 correspond to "Quit the program o Donot otherwise changethe userinterface. I'm serious . Create a mutator for the current station data member Actually while you're at it, please make sure that all variables /elements holding an FM Frequency elsewhere in the code match the data type of current station starting code o I don't think that they did in the When switching from AM to FM (and vice versa), go immediately to the previous frequency tuned on that band. o e.g. if you're listening to 700 on AM and switch to FM and then back to AM, make sure that you go to 700, not S30 Hintit wuuld be a good idea tu use a new data nienbeul type struct Freys. a When turning the radio on after being off, go immediately to the previous band (AM or FM) and last frequency tuned on that band. . e.g. if you're listening to 92.9 on FM and turn the radio off, turning the radio on again should tune to 92.9 on FM. Hint: the previous hint works here too o When turning the radio off, turn the volume down to 0. When turning the radio back on, restore the previous volume level. . Turn the volume level to 0 when the radio is turned on for the first time. oStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started