Activecode Exercises¶
Answer the following Activecode questions to assess what you have learned in this chapter.
Vacation time! But before you go, you need to convert your currency.
Let’s write the code for the dollarToYen
function. dollarToYen
takes dollar as a parameter and returns the equivalent amount of Japanese yen.
The conversion rate is 1 USD equals 105.42 Japanese yen.
Write the code that performs this conversion.
Below is one way to write the function. You need to make sure your function’s output accounts for decimal values.
When you buy something, you also need to pay sales tax. For example,
a nice shirt could be labeled with a price of exactly $20, but when
you pay, you actually need to pay $21.20 in a state with 6% sales tax.
However, different states have different tax rates. Write the function
priceWithTax
, which takes price and percentTax as parameters.
priceWithTax calculates the price after tax and returns it.
For example, priceWithTax(20,6) returns 21.2.
Below is one way to write the function. You need to make sure your function returns cent values
Most assignments and tests are graded as a percentage, but final
grades are letters. Let’s write the code for the percentToLetter
function.
percentToLetter takes a percentage and returns the corresponding
letter grade. A 90 and above is an ‘A’, an 80 and above is a ‘B’, a 70 and above
is a ‘C’, and anything under a 70 is an ‘F’. Write the necessary code to
convert a grade percentage to a letter grade.
Below is one way to write the function. Your syntax for the letter returns much match the return variable type of the function
Let’s write the code for the triangleArea
function. triangleArea
takes two parameters, base and height. It returns the
area of the triangle using the formula 1/2 * base * height.
Write the necessary code to find the area of a triangle.
Below is one way to write the function. Your function must take in more than integer base and height values and return more than integer area values.
Let’s write the code for the cylinderVolume
function. cylinderVolume
takes two parameters, radius and height. It returns the
volume of the cylinder using the formula pi * radius * radius * height.
Write the necessary code to find the volume of a cylinder.
Below is one way to write the function. Your function should incorporate the value for pi.
On a distant planet, depending on the characteristics of an egg, a kenchic,
an ooseg, or a guinpen might hatch from it. Let’s write the function
birdType
which returns an int corresponding to each type of bird
(1 for kenchic, 2 for ooseg, and 3 for guinpen). If the egg is round, then it is a
guinpen. Otherwise, if the egg is round and it isn’t gray, then it is a kenchic. If
it isn’t a guinpen and it isn’t a kenchic, then it’s an ooseg. Write the necessary
code to classify these eggs.
Below is one way to write the function.
Let’s write the code for the isDoubleDigit
function. isDoubleDigit
takes num as a parameter. isDoubleDigit returns true if
num is a double digit number and returns false otherwise.
Write the necessary code to determine if a number is a double digit number.
Below is one way to write the function. Your function must account for numbers that are greater than 100.
Let’s write the code for the Compare
function. Compare
takes two integers a, b. Compare returns 1 if
a is greater than b, -1 if a is less than b and 0 if they are equal.
Write the necessary code to compare two integers.
Below is one way to write the function. Your function must account for equal integers.
Let’s write the code for the isFactor
function. isFactor
takes two parameters, num and factor.
isFactor returns true if factor is a factor of num
and returns false otherwise. Write the necessary code to deternube is a number
is a factor of another.
Below is one way to write the function. The modulo (%) operator performs the necessary calculation.
Let’s write the code for the isPerfectSquare
function. isPerfectSquare
takes input as a parameter and returns true if input is a
perfect square and returns false otherwise. Write the necessary code
to determine if a number is a perfect square.
Below is one way to write the function.
Most bacteria cultures grow exponentially. For this problem,
assume the number of cells in a bacterial culture doubles every hour.
Let’s write the code for the countBacteria
function. countBacteria
takes hour as a parameter and returns the number of bacteria cells
after hour hours. Assume when hour is 0, there is one cell. When
hour is one, the number of cells doubles to two. When hour is two,
the number of cells doubles to four. Use recursion. Write the
necesary code to count the bacteria.
Below is one way to write the function.