As you probably leaned in 9th grade physics, force equals mass time
acceleration, or the famous F = ma. Write a method that takes
two double arguments representing a mass in kg and and
acceleration in m/s2 and returns the force in Newtons (kg * m /
s2) as a double.
Einstein’s famous equation E = mc2 tells us that the theoretical
amount energy we can obtain from a given mass. Where m is a measure
of mass and c is the speed of light. Write a method that takes a
single double argument representing a mass in kg and returns
the equivalent energy as a double according to this equation.
You can use the variable C that has already been defined for
you as the value of the speed of light.
The distance between two numbers on the number line is the absolute value of
their difference. Write a method that takes two double
arguments and returns the distance between them as a double
The distance between two points in 2d-space is normally obtained with our
old friend the Pythagorean theorem. In other words it is sqare root of the
sum of the squares of distances between the two points on the x- and y-axes.
Write a method that takes four double arguments representing
two points in 2d space as the x and y of the first point followed by the x
and y of the second point and returns a double representing the
distance between the two points.
An alternate measure of distance between two point in 2d-space is defined as
the sum of the distance along the x-axis and the distance along the y-axis.
It is called the Manhattan Distance or Taxi Cab Distance because the streets
in Manhattan are famously laid out in a grid so you can’t move diagonally;
this measure describes the distance you would travel moving between two
points in Manhattan. Write a method that takes four double
arguments representing two points as in the previous distance2d
method and returns a double representing the Manhattan Distance
between the two points.
Write a method that takes a single String argument and returns
a new, more excited String consisting of the argument followed
by three exclamation points.
Write a method that takes a single String argument and returns
a new String consisting of the argument repeated with a space
between the two repetitions.
Write a method that takes a single String argument and returns
a new String consisting of the argument repeated with a space
between the two repetitions followed by three exclamation points.
Write a method that takes two double arguments representing the
length of a space (e.g. the length of a room) and the length of an item that
you want to place in the space (maybe a rug) and returns
a double represesting how far from one end of the space to
position the item so that its centered in the space, i.e. its two ends are
the same distance from the ends of the space. You can assume both arguments
are positive and the second argument is less than the first.
For instance, to center a six foot long rug in a ten foot long room, we need to position one edge two feet from one wall so the other edge will be two feet from the other wall. In other words, we want to know half of the space left over when we subtract the length of the item from length of the space.
Write a method that takes no arguments and returns a random value in the range from 0.0 to 2.0 (inclusive and exclusive). There are no tests for this method since its output is inherently unpredictable.
Write a method that takes three double arguments: a value in
the range 0.0-2.0 (i.e. like one returned by
the randomCoordinate method), a starting value, and a length
and returns a double value so the new value is in the range
from start to start + length. For example if the first argument is 0.0 then
this method should return start value; if the coordinate is 2.0 it should
return start + length; and if the coordinate is 1.0, it would return the
value that is the start + half the length.
This is a test of writing methods. You may need to use the methods
from Math that you learned about in this unit. Also for one
problem you will need to use the constant C which is defined in
the starter code and represents the speed of light in meters per second.
Because it’s in the same class as the rest of your code you can refer to it as
just C. When possible, you should write your methods in terms of
other method you have written here.
After you finish these methods, move to Part 2: Estimate 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. Finally, click the black triangle at the start of these instructions to collapse them and have more room to code.