The perimeter of a square is four times the length of one side. Write a method that takes the length of the side of a square as its argument and returns the perimeter of the square.
Write a method that takes two numbers and returns the midpoint between them. For instance the midpoint between 6.0 and 10.0, the midpoint is 8.0.
If you remember the Pythagorean theorem, you know that the length of the hypotenuse of a right triangle is the square root of the sum of the squares of the two other sides. Write a method that takes the lengths of the two legs of a right triangle and returns the length of its hypotenuse.
The perimeter of a rectangle is two times the sum of its width and height. Write a method that takes the width and height of a rectangle as its arguments and returns its perimeter of the rectangle.
The area of a square is the square of the length of one side. Write a
method that takes the length of one side of a square as its argument and
returns the area of the square. (A square is also just a rectangle whose
width and height are the same, so you can implement this by
calling areaOfRectangle if you like — but computing it
directly is fine too.)
The area of a rectangle is its width times its height. Write a method that takes the width and height of a rectangle as arguments and returns the area of the rectangle.
The area of a triangle is half the base time the height. Write a method that takes the length of the base and the height of a triangle as arguments and return the area of the triangle.
The area of a trapezoid is half the sum of the bases (the two parallel sides) times the height. Write a method that takes the lengths of the two bases of a trapezoid and its height as arguments and returns the area of the trapezoid.
The area of a circle is π times the square of the circle’s radius. Write a method that takes the radius of a circle as an argument and returns the area of the circle.
The surface area of a sphere is four times the area of a circle with the same radius as the sphere. Write a method that takes the radius of a sphere as its argument and returns the surface area of the sphere.
The volume of a sphere is 4/3 times π times the radius cubed. Write a method that takes the radius of a sphere as its argument and returns the volume of the sphere.
The circumference of a circle is 2Ď€r. Write a a method that takes the radius of a circle as its argument and returns the circumference of the circle.
The curved surface area of a cylinder (i.e. the outside excluding the top and bottom) is the circumference of its base circle times its height. Write a method takes the radius and height of a cylinder as arguments and returns the curved surface area of the cylinder.
The total surface area of a cylinder is the curved surface area plus the areas of the top and bottom. Write a method that takes the radius and height of a cylinder as arguments and returns the total surface area of the cylinder.
The volume of a cylinder is the area of the base circle times the height. Write a method that takes the radius and height of a cylinder as arguments and returns the total volume of the cylinder.
The curved surface area of a cone (i.e. the outside excluding the circular base) is π times the radius of the base times the cone’s slant height. Write a method that takes the radius and slant height of a cone as arguments and returns the cone’s curved surface area.
The total surface area of a cone is its curved surface area plus the area of its circular base. Write a method that takes the radius and slant height of a cone as arguments and returns the cone’s total surface area.
The total surface area of a cone can also be computed from its base radius and height by computing its slant height using the Pythagorean theorem. (The slant height is the length of the hypotenuse of a right triangle formed by a radius of the cone’s base and its height.) Write a method that takes the radius and the height (not the slant height) of a cone as arguments and returns the cone’s total surface area.
The volume of cone is the area of its circular base time a third of its height. Write a method that takes the radius of a cone’s base and its height as arguments and returns the volume of the cone.
Write a method that takes an int array and returns the count
of even values in the array.
Write a method that takes an int array and returns the count
of odd values in the array.
Write a method that takes an int array and returns the sum of
the elements of the array.
Write a method that takes an int array and returns the
average of the elements of the array. (Note: if the array is of zero
length this method should return the special double
value NaN which you will get if you divide
a double value by zero.)
Write a method that takes an int array and returns the
average of the even values in the array. (Note: if there are no even
numbers to average this method should return the
special double value NaN which you will get if
you divide a double by zero.)
Write a method that takes an int array and returns the
average of the values at even positions in the array. (Note: if the array
is empty this method should return the special double
value NaN which you will get if you divide
a double by zero.)
Write a method that takes an int limit and
an int array and returns true if any value in
the array is bigger than the limit.
Write a method that takes an int limit and
an int array and returns true if every value in
the array is smaller than the limit.
Write a method that takes an int limit and
an int array and returns the first number in the array that
is bigger than the limit or -1 if no value is.
Write a method that takes an int limit and
an int array and returns the index of the first number in the
array that is bigger than the limit or -1 if no value is.
Write a method that takes a single int argument (which will
be a positive integer) and returns true if the number is prime and false
if it is not. A number is prime if it is greater than 1 and not divisible
by any number other than 1 and itself. Note that 1 is not prime.
Thus you can test whether a number greater than 1 is prime by checking
whether it is divisible by any smaller number greater than one. (As a
useful optimization, note that you only need to test numbers up to and
including the square root of the number you are checking. Remember you can
get the square root of a number with the Math.sqrt method.)
Write a method that takes a single int argument and returns
the number of primes less than that number.
Twin primes are pairs of primes that have only one number between them.
For instance 3 and 5 are twins. Write a method that takes a
single int argument and returns the number of twin prime
pairs with the first twin less than that number.
Super primes are prime numbers whose position in the list of all primes is also a prime number. For instance 3 is a super prime because it is the second prime number, i.e. it’s at position 2, and 2 is a prime number. Likewise, 5 is because it’s the third prime number. But 7 is not because it’s the fourth prime and 4 is not prime. (Note that the “positions” in the list of primes are normal human numbers that start at 1, not programmer numbers that start at 0, so 2 is at position 1, 3 is at position 2, and so on.
Write a method that takes a single int argument and returns
a boolean indicating whether the number is a super prime.
This speed run combines three earlier assignments into one set of methods.
The buttons below come in three groups: first the geometry formulas
(everything from perimeterOfSquare through
volumeOfCone), then the array loop idioms
(countEvens through indexOfFirstBiggerThan), and
finally the primes methods (isPrime through
isSuperPrime).
For the geometry methods, all the arguments and return types
are double and you may want the Math methods such
as Math.sqrt
and Math.pow,
plus the
variable Math.PI
for a good approximation of π. And remember that just like you can
call Math.sqrt you can also call methods you’ve already
written. Whenever a geometry formula can be expressed in terms of a formula
you have already implemented, your method should call the already-defined
method rather than repeating the math — except where a problem says
otherwise, the tests check for this, not just for the right answer. The
same idea applies to the primes methods, though there it is just good
advice.
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. If you get stuck on one method, move on and come back to it.