- Create a new class named MethodComposition and add a method named calculateArea and a method named distance.
public class MethodComposition { public static double calculateArea(double radius){ double result = Math.PI * radius * radius; return result; } public static double distance(double x1, double y1, double x2, double y2){ double dx = x2-x1; double dy = y2-y1; double dsquared = dx * dx + dy * dy; double result = Math.sqrt(dsquared); return result; } }
- Add a method named circleArea.
public static double circleArea(double xc, double yc, double xp, double yp){ double radius = distance(xc,yc,xp,yp); double area = calculateArea(radius); return area; }
- Add a main method and have it call circleArea.
public static void main(String[] args){ double theArea = MethodComposition.circleArea(2.3,9.5,9.8,88.54); System.out.println(theArea); }
The complete class should match the following.
public class MethodComposition { public static double calculateArea(double radius){ double result = Math.PI * radius * radius; return result; } public static double circleArea(double xc, double yc, double xp, double yp){ double radius = distance(xc,yc,xp,yp); double area = calculateArea(radius); return area; } public static void main(String[] args){ double theArea = MethodComposition.circleArea(2.3,9.5,9.8,88.54); System.out.println(theArea); } }
- Compile the program and run it.
$ javac MethodComposition.java $ java MethodComposition 19803.25422993726