Language C++ A.) Add a method called findAlert to both AmberAlertQueue TrafficAl
ID: 3838127 • Letter: L
Question
Language C++
A.) Add a method called findAlert to both AmberAlertQueue TrafficAlertQueue classes which will look for a match in the container. The method needs to:
-Take a constant reference to a respective target alert.
-Return a boolean indicating if that alert was found or not
-The method should wrap a call to a Generic Algorithm called find. which has the signature below
template<class InputIterator, class T>
InputIterator find( InputIterator first, InputIterator last, const T& vai)
-Make sure to include the the appropriate library
Parameters first and last are iterators attached to the container at hand, and represent the range to be searched. Parameter vai represents the refrence to an object, which is the target of the search.
B.) Add a matching method findAlert to the AlertQueue class template parameterized on a generic Type T.
C.) The success of the Generic Algorithm find depends on what operation being present in the objects held in the container?
D.) Write code for the operation. The code can live in the base class Alert.
Sample Code:
#include <iostream>
#include <string>
#include <deque>
namespace csci1312
{
using std::cout;
using std::string;
using std::deque;
class BaseEvent
{
public:
BaseEvent(){};
BaseEvent(string id) {eventID = id;}
virtual void handleEvent() = 0;
private:
string eventID;
};
class AmberAlert : public BaseEvent
{
public:
AmberAlert() {};
AmberAlert(string id) : BaseEvent(id) {};
void handleEvent()
{
//some code
}
};
class TrafficAlert : public BaseEvent
{
//looks similar to AmberAlert class
};
class AmberAlertQueue
{
public:
void addAlert(AmberAlert a)
{
innerQueue.push_front(a);
}
void processAlert()
{
//Process alert from the front of the queue
currentAlert = innerQueue.front();
currentAlert.handleEvent();
innerQueue.pop_front();
}
bool findAlert(const AmberAlert& a)
{
//invoke generic algorithm find
}
private:
deque<AmberAlert> innerQueue;
AmberAlert currentAlert;
};
class TrafficAlertQueue
{
//looks identical to AmberAlertQueue
};
}
Explanation / Answer
PSEUDO CODE FOR TRAFFIC LIGHT ALERT:
a=Calculate(1)
b=Calculate(2)
X1=0
X2=0
max=60 sec
IF (a b) THEN
DO
TrafficLight_1(a,b, 1,2)
while(program is running)
ElSE
DO
TrafficLight_1(b,a,2,1)
while(program is running)
TrafficLight_1 FUNCTION:
TrafficLight_1(&n1,&n2, i, j)
{
timer=1
while(n1>OSi and timer !=max )
IF (TRj.GREEN EQUEL True)
TRj.YELLOW=True
TRj.RED=True
TRi.GREEN=True
timer++
IF (n1>0) AND (n2!=0)
TRi.YELLOW=True
TRi.RED=True
timer=1
IF (n2 != 0)
while(n2>OSj and timer !=max)
TRj.GREEN=True
timer++
n1=Calculate(i)
if (n1>0)
IF (TRj.GREEN EQUEL True)
TRj.YELLOW=True
TRj.RED=True
n2=Calculate(j)
}
Calculate FUNCTION:
Calculate(i)
{
n=ISi+(xi-OSi)
Xi=n
Return n
}
C++ CODE FOR FIND ALERT TRAFFIC:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
clrscr();
int gd = DETECT, gm, midx, midy;
initgraph(&gd, &gm, "C:\TC\BGI");
midx = getmaxx()/2;
midy = getmaxy()/2;
setcolor(CYAN);
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy-10, "Traffic Light Simulation");
outtextxy(midx, midy+10, "Press any key to start");
getch();
cleardevice();
setcolor(WHITE);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
rectangle(midx-30,midy-80,midx+30,midy+80);
circle(midx, midy-50, 22);
setfillstyle(SOLID_FILL,RED);
floodfill(midx, midy-50,WHITE);
setcolor(BLUE);
outtextxy(midx,midy-50,"STOP");
delay(2000);
graphdefaults();
cleardevice();
setcolor(WHITE);
rectangle(midx-30,midy-80,midx+30,midy+80);
circle(midx, midy, 20);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(midx, midy,WHITE);
setcolor(BLUE);
outtextxy(midx-18,midy-3,"READY");
delay(2000);
cleardevice();
setcolor(WHITE);
rectangle(midx-30,midy-80,midx+30,midy+80);
circle(midx, midy+50, 22);
setfillstyle(SOLID_FILL,GREEN);
floodfill(midx, midy+50,WHITE);
setcolor(BLUE);
outtextxy(midx-7,midy+48,"GO");
setcolor(RED);
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);
outtextxy(midx-150, midy+100, "Brrrrrmmmmmm... :)");
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.