- Create a new class named Overloading.
- Create two methods named calculateArea, where one method takes four doubles and the other takes one.
- Implement one of the methods as determining the area of a polynomial while the other as the area of a circle. Also create PI with the value of 3.14 as a constant.
public class Overloading{ public static double PI = 3.14; public static double calculateArea(double a, double b, double c, double d){ return a * b * c * d; } public static double calculateArea(double r){ return PI*r*r; } }
- Add a main method that calls both methods.
public static void main(String[] args){ Overloading myOverloading = new Overloading(); double areaOne = myOverloading.calculateArea(22.33,33.22,12.12,41.78); double areaTwo = myOverloading.calculateArea(16.55); System.out.println("area: " + areaOne + " area: " + areaTwo); }
- Compile and run the program.
$ javac Overloading.java $ java Overloading area: 375629.2530513599 area: 860.0538500000001
The complete class appears as follows.
public class Overloading{ public static double PI = 3.14; public static double calculateArea(double a, double b, double c, double d){ return a * b * c * d; } public static double calculateArea(double r){ return PI*r*r; } public static void main(String[] args){ double areaOne = Overloading.calculateArea(22.33,33.22,12.12,41.78); double areaTwo = Overloading.calculateArea(16.55); System.out.println("area: " + areaOne + " area: " + areaTwo); } }