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 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 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 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.
This is a test of writing methods. It may seem like a lot of math but remember that this is not about you doing math but you making the computer do it for you.
All the arguments and return types of these methods will
be double. You’ll probably want to use the Math
methods discussed in Section 3.3 such
as Math.sqrt
and Math.pow.
You can also use the
variable Math.PI
to get a good approximation of π.
One of the key elements of this assignment is that many of the methods you
will be asked to write require you to implement formulas expressed in terms of
other formulas you will have implemented. Remember that just like you can
call Math.sqrt you can also call a method that you’ve defined.
Whenever a 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.
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. Finally, click the black triangle at the start of these instructions to collapse them.