Methods Practice
Back to the main excersizes page
1. Rectangular Shapes         Answers
Write a method called rectangleArea that takes two decimal parameters, base and
height and returns area of a rectangle of those dimensions.
Write a method called boxVolume that takes three decimal parameters, base, height,
and depth, and returns the volume of a box with those dimensions.
Write a method called boxSurfaceArea that takes three decimal parameters, base,
heigt, and depth, and returns the surface of a box having those dimensions.
2. Cylindrical shapes         Answers
Write a method called circleCircumference that takes a decimal radius and return the circumference
of a circle with that radius.
Write a method called circleArea that takes a decimal radius and returns the area of a circle
with that radius.
Write a method called cylinderVolume that takes two decimals, a radius and a height, and
returns the volume of a cylinder with those dimensions.
Write a method called cylinderSurfaceArea that takes two decimals, a radius and a height, and
returns the surface area of a cylinder with those dimensions.
3. Quadratic Roots         Answers
Write a method called quadraticRoot1 that takes three decimals, a, b, and c, and calcualtes one
of two quadratic roots of the equation ax² + bx + c = 0; Use the positive square root.
Write a method called quadraticRoot2 that takes three decimals, a, b, and c, and calcualtes one
of two quadratic roots of the equation ax² + bx + c = 0; Use the negative square root.
4. Simple Conversions         Answers
Write a method called seconds2minutes that takes an integer number of seconds and returns the
equivalent decimal number of minutes.
Write a method called minutes2hours that takes a decimal number of minutes and returns the equivalent
number of hours.
Write a method called hours2days that takes a decimal number of hours and returns the equivalent
number of days.
Write a method called days2years that takes a decimal number of days and returns the equivalent
number of years.
Write a method called seconds2years that takes an integer number of seconds and returns the equivalent
number of years.
5. Random Number Generator         Answers
Write a method called randomInteger that takes in two integers, a lower bound a an upper bound.
The method should return a number including or between the bounds at random.
6. The Point Class         Answers
These questions all pertain to the following class definition of some xy Point:
public class Point {
private int x;
private int y;
public Point(int x, int y) {
setX(x);
setY(y);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
Write a method called reflectAcrossX that takes in a Point and reflects it across
the X axis.
Write a method called reflectAcrossY that takes in a Point and reflects it across
the Y axis.
Write a method called reflectAcrossOrigin that takes in a Point and reflects it
across the origin.
Write a method called translateX that takes a Point and an integer amount and
translates the Point the specified amount in the x direction.
Write a method called translateY that takes a Point and an integer amount and
translates the Point the specified amount in the y direction.
Write a method called translate that takes a Point and two integers, and
translates the Point the specified amount in both the x and y directions.
Write a method called dotProduct that takes in two Points and returns their
dot product.
Write a method called magnitude that takes in a Point and returns the magnitude
of the point's vector (The Euclidean distance to the origin).
Write a method called angle that takes in a Point and returns the angle associated
with that point.
Write a method called copy which takes in a Point and returns a Point object at
exactly the same coordinates.
Write a method called double that takes in a Point and returns a second Point
object whose coordinates are twice the first.
Write a method called invert that takes a Point and returns a Point object whose
coordinates are reversed.
7. Physics         Answers
Write a method called acceleration that takes in a speed v and a radius r,
and return the centripetal acceleration. The formula is ac = v²/r. Use
decimals.
Write a method called force that takes in a mass m, a speed v, and a
radius r, and returns the centripetal force. The formula is Fc
= mv²/r. Use decimals.
Write a method called energy that takes a (linear) spring constant k, and a decmial
displacement x from the forces equilibrium position (the center) and returns the
potential energy of the spring. The formula is U = (1/2)kx². Use decimals.
Write a method called field that takes in a point charge q, and a radial
distance r, and returns the electrostatic field magnitude. The formula is E = kq/r²,
where k is Coublomb's constant, whose magnitude is ≈ 9 x 10^9. Use decimals.
Write a method called wireField that takes in a current i (or I),
and a radial distance r, and returns the magnitude of the magnetic field. The formula is B = μoI / 2πr, where μo is the permittivity of space, whose
magnitude is ≈ 8.85 x 10^-12. Use decimals.