4.18. Mixed Up Code Practice¶
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. Put the necessary blocks of code in the correct order.
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. Put the necessary blocks of code in the correct order.
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’. Put the necessary blocks of code in the correct order.
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. Put the necessary blocks of code in the correct order.
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. Put the necessary blocks of code in the correct order.
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. Put the necessary blocks of code in the correct order.
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. Put the necessary blocks of code in the correct order.
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. Put the necessary blocks of code in the correct order.
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. Put the necessary blocks of code in the correct order.
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. Put the necessary blocks of code in the correct order.
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. Put the necessary blocks of code in the correct order.