Activecode Exercises¶
Answer the following Activecode questions to assess what you have learned in this chapter.
Let’s write the struct definition for Song
. Song should have
instance variables title, artist, and numLikes.
Below is one way to define the Song
struct.
Let’s make an album! Write the struct definition for
Album
, which should have instance variables name, year and
a vector of Songs.
Below is one way to define the Album
struct.
Two Songs are equal if the title and artist of the Songs are equal.
Write the function songEqual
, which takes two Songs as parameters
and returns true if they are equal.
Below is one way to write the songEqual
function.
What if we’d like to search an album for our favorite song?
Write the Album
member function searchAlbum which takes a
Song as a parameter and returns the location of the Song in
the album. If the song isn’t found, return -1. Use the
songEqual function we defined earlier!
Below is one way to write the Album
member function.
What’s the most popular Song within an Album? Let’s write
the Album
member function mostLikedSong, which prints out
the information of the most liked Song in the format “The most
liked song is title by artist with numLikes likes.”
Below is one way to write the Album
member function.
Let’s write the struct definition for Product
. Product
should have
instance variables name and price.
Below is one way to define the Product
struct.
Let’s make a shopping list! Write the struct definition for
List
, which should have instance variables type and
a vector of Products.
Below is one way to define the List
struct.
Two Products are equal if the name and price of the Products are equal.
Write the function productEqual, which takes two Products as parameters
and returns true if they are equal. What if we want to check to see if
we have bananas in our shopping list? Write the List member function
searchList
, which takes a Product as a parameter and returns the location
of the Product in the List. Return -1 if it’s not in the List.
Below is one way to write the searchList
member function.
Time to checkout! Write the List member function totalPrice
which calculates and returns the total price of all the Products.
Below is one way to write the totalPrice
member function.
Oops! We made a mistake and grabbed pineapple pizza.
What if we want to remove an Product from our List?
Write the List member function removeProduct
, which takes
an index as a parameter and removes it. Then it fills
the gap with the last product in the List.
Below is one way to write the removeProduct
member function.