Write a method that takes two doubles and returns the number
which is halfway between them.
Write a method that takes two ints, a number of students and
a number of pencils needed per student, and returns the total number of
pencils needed as an int.
Write a method that takes two ints, representing the number
of answers correct on an exam and the total number of questions, and
returns a double representing the percentage correct as a
number from 0.0 to 100.0.
Write a method that takes two doubles. The first is a
proportion (between 0 and 1) of 3-point questions a student got correct
and the second the proportion of 4-point questions correct. The method
should return a score between 0.0 and 4.0 which is calculated as three
times the proportion of 3-point questions correct plus the proportion of
4-point questions correct. (Thus a student who got 100% of the 3-point
questions correct but no 4-point questions would get a 3.0 and a student
who got all the questions correct would get 4.0.)
Write a method that takes a single double argument, the
radius of a circle, and returns the circle’s circumference. Recall that
the circumference is computed as 2πr where r is the radius of the
circle.
Write a method that takes a single int argument and returns
the boolean value true if the number is even
and false otherwise.
Write a method that takes a single int argument and returns
the boolean value true if the number is odd and
false otherwise.
Write a method that takes two ints and returns the sum of
their squares. For example, given 3 and 4, it should return 25 (since 3²
+ 4² = 9 + 16 = 25).
Write a method that takes three doubles representing the
length, width, and height of a rectangular box and returns its volume.
Write a method that takes a temperature in Fahrenheit (a
double) and returns the equivalent temperature in Celsius.
The formula is (F − 32) × 5/9.
Write a method that takes two doubles representing the base
and height of a triangle and returns its area. Remember that the area of
a triangle is (base × height) / 2.
Write a method that takes an int number of minutes (e.g.,
145) and returns the equivalent in hours as a double,
possibly including a fractional part (e.g., about 2.42 for 145 minutes).
You need quarters for the laundry machine. You’re buying a snack and want
to get your change in quarters. You’ve got a twenty dollar bill and a
pocket full of small change. Write a method that takes the price of an
item in cents as an int (e.g. 542 for $5.42) and returns the
number of extra cents you should pay in addition to your twenty dollar
bill so you get all your change in quarters.
Write a method that takes three ints (each between 1 and 6)
representing dice rolls and returns their sum, but with a bonus of 10
extra points if all three numbers are the same.
Write a method that takes two doubles representing the
lengths of the two shorter sides of a right triangle and returns the
length of the hypotenuse. Use the Pythagorean theorem: a² + b² = c².
Write a method that takes three doubles representing test
scores and three more doubles representing the weight of
each test and returns the weighted average of the scores. A weighted
average is computed by multiplying each score by its corresponding
weight, summing those values, and then dividing by the sum of all the
weights. For example, given scores 90, 85, 88 and weights 50, 30, 20, it
should return 88.1.
Write a method that takes a loan amount (a double), an
annual interest rate as a percentage (a double), and a
number of years (an int), and returns the monthly payment
required to pay off the loan. The formula is: P = L[i(1 + i)ⁿ]/[(1 + i)ⁿ
− 1], where L is the loan amount, i is the monthly interest rate (annual
rate / 1200), and n is the total number of months.
More practice writing methods that compute things with numbers. As always, write your methods in the editor to the left and hit the button above the editor to run the tests. Click a button in the test panel to see the specification for each method.
Pay attention to the types! Each specification says whether the arguments
and return value are ints or doubles (or, in one
case, a boolean). Remember that dividing one int
by another throws away the fractional part, so when a method takes
ints but needs to return a double you may need to
be careful about how you divide. And remember you can use
Math.sqrt,
Math.pow,
and Math.PI.
Pro tip: when you click one of the method buttons, the name of the method is copied to your clipboard so you can paste it into your code, saving typing and avoiding spelling errors.