Description
-
Objectives
This homework aims to help you get familiar with the fundamental C++ programming concepts.
Keywords: Constructor/Copy Constructor/Destructor, Assignment/Move, Operator Overloading, Mem-ory Management
-
Problem De nition
TL;DR: Implement the methods in the given 4 classes (Laptime, Car, Race, Championship). Cars have Laptimes, Races have Cars, Championships have Races. The details are on their way.
Another TA who watched Drive to Survive
…
Oh God show mercy to this boring soul.
F1 needs a new clear backbone for their visualisation system. As a Computer Engineering student from METU, they trusted you with the task. Your task is to keep track of the laptimes, cars, races and tracks. As situation requested shiny features of the latest C++ is not available to you. Therefore, you have to be careful with your programs memory management (You do not want to leave a program that is leaking memory as your legacy).
1
-
Class De nitions
3.1 Laptime
Laptime is the most basic class in this homework. Basically, it’s the node in a linked-list which would keep the time information of the laps for each Car (not the best way to keep lap times of Cars, but let’s assume F1 is not paying you anything and you let this go).
c l a s s Laptime f
p r i v a t e :
i n t laptime ;
Laptime ∗next ;
-
DO NOT MODIFY THE UPPER PART
-
ADD OWN PRIVATE METHODS/PROPERTIES BELOW
p u b l i c :
-
∗ ∗
∗ C o n s t r u c t o r .
∗
∗ @param i n t v a l u e i n l a p t i m e .
∗/
Laptime( i n t laptime) ;
-
∗ ∗
∗ Copy C o n s t r u c t o r .
∗
∗ @param r h s The l a p t i m e t o be c o p i e d .
∗/
Laptime( c o n s t Laptime& rhs) ;
~Laptime ( ) ;
-
∗ ∗
∗ S e t s t h e next c h a i n f o r t h i s Laptime .
∗ You s h o u l d i n s e r t t h e g i v e n Laptime t o t h e o b j e c t which t h e method i s –
c a l l e d upon
∗
∗ @param next The next Laptime .
∗/
v o i d addLaptime(Laptime ∗next) ;
-
∗ ∗
∗ L e s s than o v e r l o a d .
∗
∗ True i f t h i s Laptime l e s s than t h e r h s Laptime .
∗
∗ @param r h s The Laptime t o compare .
∗ @return True i f t h i s l a p t i m e i s s m a l l e r , f a l s e o t h e r w i s e .
∗/
b o o l o p e r a t o r <( c o n s t Laptime& rhs) c o n s t ;
-
∗ ∗
∗ G r e a t e r than o v e r l o a d .
2
∗
∗ True i f t h i s Laptime g r e a t e r than t h e r h s Laptime .
∗
∗ @param r h s The Laptime t o compare .
∗ @return True i f t h i s l a p t i m e i s b i g g e r , f a l s e o t h e r w i s e .
∗/
b o o l o p e r a t o r >( c o n s t Laptime& rhs) c o n s t ;
-
∗ ∗
Plus o v e r l o a d
∗
Add two Laptime and r e t u r n t h e l h s Laptime
∗
∗ @param Laptime t o add
@returns Summation o f t h e two l a p t i m e
∗/
Laptime& o p e r a t o r +( c o n s t Laptime& rhs) ;
-
∗ ∗
∗ Stream o v e r l o a d .
∗
∗ What t o stream :
∗ minute : s ec o n d . m i l i s e c o n d s
∗
∗ Example : ∗ 1:19.125
-
∗
∗
@important Your l a p t i m e
v a r i a b l e
i s
r e p r e s e n t a t i o n
i n terms
o f m i l i s e c o n d s
∗
and you have t o t u r n
i t
t o d e s i r e d
outcome
type
∗
P r i n t t h e Laptime o f
t h e
o b j e c t
which t h e
method i s
c a l l e d
upon .
∗
@param o s Stream t o be used .
∗
@param l a p t i m e Laptime t o be streamed .
∗ @return The c u r r e n t Stream .
∗/
f r i e n d std : : ostream& o p e r a t o r <<(std : : ostream& os , c o n s t Laptime& laptime) ;
-
DO NOT MODIFY THE UPPER PART
-
ADD OWN PUBLIC METHODS/PROPERTIES BELOW
-
;
3.2 Cars
Cars are similar to Laptimes, but they contain the name of the driver which is up to you (LeClerc can be a good start. Bahrain 2019 :/ ). It is again a node in a linked-list, but every car contains the linked-list of Laptime class (Memory Problems 2019).
c l a s s Car f
p r i v a t e :
std : : string driver_name ;
d o u b l e performance ;
Laptime ∗head ;
3
Car ∗next ;
-
DO NOT MODIFY THE UPPER PART
-
ADD OWN PRIVATE METHODS/PROPERTIES BELOW
p u b l i c :
-
∗ ∗
∗ C o n s t r u c t o r .
∗
∗ @Important : s e t t h e p e r f o r m a n c e v a r i a b l e o f t h e c a r by u s i n g U t i l i z e r : : –
g e n e r a t e P e r f o r m a n c e ( )
∗
∗ @param s t d : : s t r i n g The Car ‘ s d r i v e r name .
∗/
Car(std : : string driver_name) ;
-
∗ ∗
∗ Copy C o n s t r u c t o r .
∗
∗ @param r h s The c a r t o be c o p i e d .
∗/
Car( c o n s t Car& rhs) ;
~Car ( ) ;
-
∗ ∗
∗ Gets t h e d r i v e r s name
∗
∗
∗ @returns : d r i v e r s name
∗/
std : : string getDriverName ( ) c o n s t ;
-
∗ ∗
∗
∗ Gets t h e p e r f o r m a n c e
∗
∗ @returns t h e p e r f o r m a n c e
∗
∗/
d o u b l e getPerformance ( ) c o n s t ;
-
∗ ∗
∗ S e t s t h e next c h a i n f o r t h i s Car .
∗ Adds a new c a r behind e x i s t i n g c a r
∗ You s h o u l d i n s e r t t h e g i v e n Car t o t h e o b j e c t which t h e method i s c a l l e d –
upon
∗ Important : Car d o e s NOT “own” next .
∗
∗ @param next The next Car .
∗/
v o i d addCar(Car ∗next) ;
-
∗ ∗
∗ L e s s than o v e r l o a d .
∗
4
∗ True i f t o t a l l a p t i m e o f t h i s Car i s l e s s than t h e r h s Car .
∗
∗ Important :
∗
∗ @param r h s The Car t o compare .
∗ @return True i f t h i s c a r ‘ s t o t a l l a p t i m e i s s m a l l e r , f a l s e o t h e r w i s e .
∗/
b o o l o p e r a t o r <( c o n s t Car& rhs) c o n s t ;
/∗ ∗
∗ G r e a t e r than o v e r l o a d .
∗
∗ True i f t o t a l l a p t i m e o f t h i s Car i s g r e a t e r than t h e r h s Car .
∗
∗ Important :
∗
∗ @param r h s The Car t o compare .
∗ @return True i f t h i s c a r ‘ s t o t a l l a p t i m e i s g r e a t e r , f a l s e o t h e r w i s e .
∗/
b o o l o p e r a t o r >( c o n s t Car& rhs) c o n s t ;
-
∗ ∗
∗ I n d e x i n g .
∗
-
∗
Find
t h e
l a p t i m e o f
t h e g i v e n
l a p .
∗
You
w i l l
u s e 0 based
i n d e x i n g .
∗
For example ,
a f t e r
20
l a p
your c a r
s h o u l d have
20 l a p t i m e s . To
g e t
15 th
–
l a p t i m e
you
w i l l
g i v e
14
a s
t h e i n p u t
parameter .
∗
∗
@return The Laptime
with t h e
g i v e n
l a p .
Laptime
with z e r o time
i f
g i v e n
–
l a p
d o e s
not
e x i s t s .
∗/
Laptime o p e r a t o r [ ] ( c o n s t i n t lap) c o n s t ;
-
∗ ∗
-
∗
Car
c o m p l e t e s one
l a p
and
r e c o r d s
i t s
l a p t i m e
∗
∗
@Important : Based
on
your
c a r s p e r f o r m a n c e c a l c u l a t e
some
v a r i a n c e
t o add –
a v e r a g e l a p t i m e
∗
Use
U t i l i z e r : : g e n e r a t e L a p t i m e V a r i a n c e ( p e r f o r m a n c e )
then
add i t t o
–
a v e r a g e l a p t i m e
∗
∗
@param : Car t a k e s
a v e r a g e
l a p t i m e
o f
t h e r a c e
∗
∗/
v o i d Lap( c o n s t Laptime& average_laptime) ;
-
∗ ∗
∗ Stream o v e r l o a d .
∗
∗ What t o stream :
F i r s t Three l e t t e r s o f t h e d r i v e r s surname ( C a p i t a l i z e d ) L a t e s t Laptime –
F a s t e s t Laptime T o t a l Laptime
∗ Example :
∗ For Lewis Hamilton
5
-
∗ HAM
1:19.235
1:18.832
90:03.312
∗
∗
@Important :
f o r
l a p
numbers s m a l l e r i n
s i z e
you have t o put z e r o s a s much –
a s n e c c a s a r y
∗
@Important :
you
can
u s e
Laptime ostream
when
n e c c e s s a r y
∗
∗
@param o s Stream t o be used .
∗
@param
c a r
Car
t o be
streamed .
∗ @return The c u r r e n t Stream .
∗/
f r i e n d std : : ostream& o p e r a t o r <<(std : : ostream& os , c o n s t Car& car) ;
-
DO NOT MODIFY THE UPPER PART
-
ADD OWN PUBLIC METHODS/PROPERTIES BELOW
-
;
3.3 Race
Race keeps a linked-list of Cars. You have to keep your cars in the order from fastest to slowest (Nobody wants to see some randomly ordered car info). The details are in the code itself below:
c l a s s Race f
p r i v a t e :
std : : string race_name ;
Laptime average_laptime ;
Car ∗head ;
-
DO NOT MODIFY THE UPPER PART
-
ADD OWN PRIVATE METHODS/PROPERTIES BELOW
p u b l i c :
-
∗ ∗
∗ C o n s t r u c t o r .
∗
∗ @Important : Generate a v e r a g e l a p t i m e by u s i n g U t i l i z e r : : –
g e n e r a t e A v e r a g e L a p t i m e ( )
∗
∗ @param s t d : : s t r i n g The name o f t h e Race .
∗/
Race(std : : string race_name) ;
-
∗ ∗
∗ Copy C o n s t r u c t o r .
∗
∗ @Important j u s t copy t h e names and p e r f o r m a n c e s o f t h e c a r s
∗ w i t h o u t any l a p t i m e .
∗ Cars s h o u l d be deep c o p i e d
∗
∗ @param r h s The r a c e t o be c o p i e d .
∗/
Race( c o n s t Race& rhs) ;
~Race ( ) ;
std : : string getRaceName ( ) c o n s t ;
6
-
∗ ∗
-
∗
Add a new c a r
t o r a c e .
∗
∗
@Important :
At
t h e
s t a r t o f
t h e
r a c e
t h e i r
o r d e r i n g
i s
not i m p or t a n t
∗
∗
No parameter
means
t h a t you
w i l l
g e n e r a t e
your
own
c a r
i n
∗
t h i s f u n c t i o n ( with
a random
name) and
add
i t
t o
your
Cars
∗/
v o i d addCartoRace ( ) ;
-
∗ ∗
-
∗
Add a new c a r t o r a c e .
∗
∗
@Important :
At
t h e s t a r t
o f
t h e r a c e t h e i r o r d e r i n g i s not i m p or t a n t
∗
∗
@param : c a r
Add
g i v e n Car
t o
o t h e r s
∗/
v o i d addCartoRace(Car& car) ;
-
∗ ∗
∗ I n f o r m a t i o n About how much c a r i s i n t h e r a c e
∗
∗ @returns number o f c a r s
∗
∗/
i n t getNumberOfCarsinRace ( ) ;
-
∗ ∗
-
∗
Return
s t a t e
o f e v e r y t h i n g t o
d e s i r e d l a p ‘ s
s t a t e
∗
∗
Your Race
w i l l
have
many
l a p s
i n
t h e
c o u r s e
o f
i t s
l i f e .
In
each
lap ,
your
–
c a r s
w i l l
have
new Laptimes
e t c .
∗
For example ,
i f you
have
20 Laps
i n
your
Race ,
but
l e t ‘ s
say
you
want
t o
–
r e d o
l a s t 10
l a p s
o f
i t .
By
u s i n g goBacktoLap ,
∗
you w i l l
t a k e
your
Race back
t o
10 th
Lap .
This
w i l l
a l s o
make c ha n g es
i n
–
your
Cars .
Your
c a r s
w i l l have
o n l y
t h e
i n f o r m a t i o n o f
t h e
f i r s t 10
l a p s .
∗
Lap numbers
s t a r t
from
z e r o .
I f
5 i s
g i v e n
a s
i n t lap , you w i l l
t a k e
Race
–
back
t o
6th
l a p .
∗
@Important :
t h i s
w i l l
a l s o apply
t o
c a r s
and l e a d e r b o a r d
t o o
∗
∗ @param l a p t o r e t u r n
∗
∗/
v o i d goBacktoLap( i n t lap) ;
-
∗ ∗
P r e f i x a d d i t i o n o v e r l o a d
∗
∗ add one more l a p t o a l l c a r s
∗
∗ @Important : Update t h e o r d e r o f t h e c a r s s o t h a t t h e f a s t e s t one s t a y s a t –
t h e f r o n t
∗
∗/
v o i d o p e r a t o r ++() ;
7
-
∗ ∗
P r e f i x decrement o v e r l o a d
∗
∗ remove one l a p from a l l c a r s
∗
∗ @Important : Update t h e o r d e r o f t h e c a r s s o t h a t t h e f a s t e s t one s t a y s a t –
t h e f r o n t
∗
∗/
v o i d o p e r a t o r () ;
-
∗ ∗
∗
-
∗ I n d e x i n g o v e r l o a d
∗
∗
This f u n c t i o n
w i l l
not
be t e s t e d
f o r t h e
c a s e s
i n which
Car i n g i v e n –
p o s i t i o n d o e s
not
e x i s t .
∗
∗
@param :
c a r
i n
p o s i t i o n
Car i n t h e g i v e n
p o s i t i o n
∗
@returns
t h e
c a r i n
t h e
d e s i r e d
p o s i t i o n
i n t h e
c u r r e n t
l a p
∗/
Car o p e r a t o r [ ] ( c o n s t i n t car_in_position) ;
-
∗ ∗
∗
∗ I n d e x i n g o v e r l o a d
∗
∗ This f u n c t i o n w i l l not be t e s t e d f o r t h e c a s e s i n which Car with g i v e n –
d r i v e r n a m e d o e s not e x i s t .
∗
∗ @param : d r i v e r n a m e d r i v e r ‘ s name o f t h e d e s i r e d c a r
∗ @returns t h e c a r whose d r i v e r named a s t h e g i v e n @param
∗/
Car o p e r a t o r [ ] ( std : : string driver_name) ;
-
∗ ∗
∗ Assignment
∗
∗ @param r h s The Race t o a s s i g n i n t o t h i s r a c e
∗ @return The a s s i g n e d Race
∗/
Race& o p e r a t o r =( c o n s t Race& rhs) ;
-
∗ ∗
∗ Stream o v e r l o a d .
∗
∗ What t o stream :
∗ P o s i t i o n ∗D r i v e r Name( l e a d e r o f t h e r a c e ) L a t e s t Laptime F a s t e s t –
Laptime o f t h e Dr ive r Sum o f Laptimes ( i n d i s p l a y Laptime format ) Po int s –
E xt r a Po i n t ( I f a p p l i c a b l e )
. . .
∗ P o s i t i o n ∗D r i v e r Name( l a s t p l a c e o f t h e r a c e ) L a t e s t Laptime F a s t e s t –
Laptime o f t h e Dr ive r Sum o f Laptimes ( i n d i s p l a y Laptime format ) Po int s –
E xt r a Po i n t ( I f a p p l i c a b l e )
∗
∗ Example :
8
-
∗
001
TUF
1:19.461
1:18.935
60:35.193
25
∗
002
UTA
1:19.335
1:18.335
60:37.321
18
1
∗
003
GRT
1:20.223
1:19.932
60:45.184
15
∗ …
-
∗
099
CEI
1:21.005
1:19.867
63:47.293
∗
100
ECH
1:23.213
1:21.331
64:00.123
∗
∗
@Important :
f o r
d r i v e r numbers
s m a l l e r i n
s i z e
you have
t o
put
z e r o s
a s
–
much
a s
n e c c a s a r y
t o t h e i r
b e g i n n i n g
∗
Example :
i f
t h e r e
i s
11 r a c e r s
f i r s t p o s i t i o n
s h o u l d
be
01
∗
@Important . you can u s e Laptime ostream when n e c c e s s a r y
∗
@Important : You s h o u l d o r d e r
t h e r a c e r s a c c o r d i n g t o
t h e i r
t o t a l l a p t i m e
∗
@Important :
There
a r e
two
d i f f e r e n t p o i n t
t y p e s f o r
F1
∗
F i r s t
one
i s
t h e F a s t e s t
Lap
p o i n t which
i s
1
p o i n t
and
i t
i s
g i v e n
t h e
–
f a s t e s t
c a r
i f
i t
i s
i n
top
10
∗
Other
one
i s
normal r a c i n g p o i n t s and they
a r e
25 18
15
12
10864
2 1
i n –
t h i s
o r d e r
∗
∗
@param o s Stream t o be used .
∗
@param
c a r Car
t o be
streamed .
∗ @return The c u r r e n t Stream .
∗/
f r i e n d std : : ostream& o p e r a t o r <<(std : : ostream& os , c o n s t Race& race) ;
-
DO NOT MODIFY THE UPPER PART
-
ADD OWN PUBLIC METHODS/PROPERTIES BELOW
-
;
3.4 Championship
Championship is the nal part of this homework. They keep races with di erent names. Championship.
c l a s s Championship f
p r i v a t e :
std : : vector< Race > races ;
-
DO NOT MODIFY THE UPPER PART
-
ADD OWN PRIVATE METHODS/PROPERTIES BELOW
p u b l i c :
-
∗ ∗
∗ C o n s t r u c t o r .
∗
∗/
Championship ( ) ;
-
∗ ∗
∗ Copy C o n s t r u c t o r .
∗
∗ @param r h s The Championship t o be c o p i e d .
∗/
Championship( c o n s t Championship& rhs) ;
~Championship ( ) ;
9
-
∗ ∗
∗ Add a new Race t o Championship .
∗
∗ @Important : Purpose o f t h i s f u n c t i o n i s t o add t h e f i r s t r a c e .
∗
∗ @param r a c e Race t o add
∗/
v o i d addNewRace(Race& race) ;
-
∗ ∗
∗ Add a new Race t o Championship .
∗
-
∗
@Important :
You w i l l u s e
getAverageLaptime
f u n c t i o n
f o r t h i s
∗
@Important :
You can u s e
copy
c o n s t r u c t o r
o f
t h e
Race
c l a s s i n o r d e r
∗
t o j u s t copy
d r i v e r
names
f o r
your
new Race
∗
@Important :
D r i v e r
names
s h o u l d be
same
f o r
each
r a c e
∗
∗
@param race name c r e a t e
a
new
r a c e
with
g i v e n name
∗/
v o i d addNewRace(std : : string race_name) ;
-
∗ ∗
∗ Remove r a c e from by u s i n g i t s name
∗
∗ @param race name remove r a c e from championship
∗
∗/
v o i d removeRace(std : : string race_name) ;
-
∗ ∗
∗
∗ Add l a p t o r a c e with t h e g i v e n name
∗
∗ @param race name you w i l l f i n d t h e r a c e with t h i s name and add a l a p t o i t
∗/
v o i d addLap(std : : string race_name) ;
-
∗ ∗
∗
∗ I n d e x i n g o v e r l o a d
∗
∗
∗ @param : r a c e name
∗ @returns t h e d e s i r e d Race
∗/
Race o p e r a t o r [ ] ( std : : string race_name) ;
-
∗ ∗
10
∗ Stream o v e r l o a d .
∗
∗ What t o stream :
∗ Championship R e s u l t s
∗ D r i v e r Surname f i r s t t h r e e l e t t e r s i n c a p i t a l ( from winner ) T o t a l P o i n t s
∗ …
∗ D r i v e r Surname f i r s t t h r e e l e t t e r s i n c a p i t a l ( t o l a s t p l a c e ) T o t a l P o i n t s ∗
∗ Example :
∗ Championship R e s u l t s
∗ 01 RAI 194
∗ 02 HAM 190
∗ 03 LEC 100
∗ 77 OCO 60
∗78RIC 1
∗79GRO 0
∗
-
∗
@Important :
f o r
d r i v e r p l a c e m e n t s numbers s m a l l e r i n
s i z e
you have t o put –
z e r o s a s
much
a s
n e c c a s a r y t o
t h e i r
s t a r t
∗
Example :
i f
t h e r e
i s
111 r a c e r s
f i r s t
p o s i t i o n
s h o u l d
be
001
∗
@Important :
You
w i l l
o r d e r d r i v e r s a c c o r d i n g t o
t h e i r
t o t a l p o i n t s
∗
∗
@param o s Stream t o be used .
∗
@param c a r
Car
t o
be
streamed .
∗ @return The c u r r e n t Stream .
∗/
f r i e n d std : : ostream& o p e r a t o r <<(std : : ostream& os , c o n s t Championship& – championship) ;
-
DO NOT MODIFY THE UPPER PART
-
ADD OWN PUBLIC METHODS/PROPERTIES BELOW
-
;
-
Extras
While generating Cars you need to produce random double value for its performance. To do this, you MUST use Utilizer class. It’ll return random small double numbers.
The implementation of the the Utilizer is already provided to you. Hence, you just need to do this:
d o u b l e performance = Utilizer : : generatePerformance ( ) ;
You also MUST use Utilizer::generateLaptimeVariance() for generating a di erence to average lap-time. After that you will add this to calculate laptime of the car (You will do this for every lap). For generating average laptimes of the Races you again MUST use Utilizer::generateAverageLaptime() func-tion which will return an integer value (You will turn it to Laptime class).
The summary of the memory ownership:
-
A Laptime WILL NOT own its nextLaptime.
-
A Car WILL NOT own its nextCar.
-
A Car WILL own the headLaptime when constructed with.
-
A Race WILL own the headCar.
-
CopyConstructers WILL not yield ownership of the old variables.
11
-
assignment operator “=” WILL yield ownership of the old variables.
-
Owning a Car/Laptime also means owning the nextCar/nextLaptime, and the nextCar/nextLaptim of the nextCar/nextLaptime, …
-
Grading
-
-
Full grade for Laptime class implementation 15 points.
-
-
-
Full grade for Car class implementation 30 points.
-
-
-
Full grade for Race class implementation 30 points.
-
-
-
Full grade for Championship class implementation 25 points.
-
In order to get full grade from each part your code should not have any memory leak. This will be checked with valgrind. While grading your classes will be used with the correct implementations, therefore they are expected to work as commented in the code.
-
Regulations
-
-
Programming Language: You must code your program in C++ (11). Your submission will be compiled with g++ with -std=c++11 ag on department lab machines.
-
-
-
Allowed Libraries: You may include and use C++ Standard Library. Use of any other library (especially the external libraries found on the internet) is forbidden.
-
-
-
Memory Management: When an instance of a class is destructed, the instance must free all of its owned/used heap memory. Any heap block, which is not freed at the end of the program will result in grade deduction. Please check your codes using valgrind {leak-check=full for memory-leaks.
-
-
-
Late Submission: You have a total of 10 days for late submission. You can spend this credit for any of the assignments or distribute it for all. For each assignment, you can use at most 3 days-late.
-
-
-
Cheating: In case of cheating, the university regulations will be applied.
-
-
-
Newsgroup: It’s your responsibility to follow the cengclass forums for discussions and possible updates on a daily basis.
-
-
Submission
Submission will be done via CengClass. Create a zip le named hw3.zip that contains:
-
Laptime.h
-
Laptime.cpp
-
Car.h
-
Car.cpp
-
Race.h
-
Race.cpp
12
-
Championship.h
-
Championship.cpp
Do not submit a le that contains a main function. Such a le will be provided and your code will be compiled with it. Also, do not submit a Make le.
Note: The submitted zip le should not contain any directories! The following command sequence is expected to run your program on a Linux system:
-
unzip hw3.zip
-
make clean
-
make all
-
make run
-
-optional- make valgrind
13