Activecode Exercises

Answer the following Activecode questions to assess what you have learned in this chapter.

Let’s write the code for the struct definition of Song. The Song structure will have the instance variables string title, string artist, string album, and int year in that order.

Below is one way to define the struct definition of Song.

In main, create a Song object called fly which holds the data for Frank Sinatra’s “Fly Me to the Moon” from his 1964 album “It Might as Well Be Swing”.

Below is one way to create the object in the main function.

Let’s write the code for the printSong function. printSong takes a Song as a parameter and prints out the instance variables in the following format: “title” by artist (album, year).

Below is one way to write the printSong function.

Let’s write the code for the struct definition of Unicorn. The Unicorn structure will have the instance variables name, age, hornLength, hairColor, and isSparkly in that order. A Unicorn’s horn length is measured to the nearest tenth of a unit.

Below is one way to define the struct Unicorn.

Let’s write the code for the convertToHumanAge function. convertToHumanAge takes a Unicorn as a parameter and returns the equivalent human age. If a unicorn is sparkly, then its equivalent human age is three times its age in unicorn years plus the length of its horn. If a unicorn is not sparkly, then its equivalent human age is four times its age in unicorn years plus twice the length of its horn.

Below is one way to write the convertToHumanAge function.

Let’s write the code for the unicornPower function. unicornPower takes a Unicorn as a parameter and sets isSparkly to true and changes the color to rainbow.

Below is one way to write the unicornPower function.

Let’s write the code for the struct definitions of Address and Employee. The Address structure will have the instance variables houseNumber, state (abbreviation), and postalAddress in that order. The Employee structure will be a nested structure with the instance variables name and Address address in that order.

Below is one way to define the Address and Employee structs.

Let’s write the code for the printAddress function. printAddress takes an Employee as a parameter and should print out the information of the employee in the following format: name (id) lives at houseNumber in state, postalAddress.

Below is one way to write the printAddress function

Sometimes employees will move around and thus we’ll need to update their addresses. Let’s write the code for the updateAddress function. updateAddress takes an Employee and a new Address as parameters and sets the employee’s address to the new address.

Below is one way to write the updateAddress function.

Let’s write the code for the storeEmployeeData function. storeEmployeeData doesn’t take any parameters and prompts the user for information regarding their name, house number, state, and postal code. It then returns an Employee object with the stored data. Declare all variables before prompting the user.

Below is one way to write the storeEmployeeData function.

You have attempted of activities on this page