/* This program serves as a demonstration for methods and method overloading. * Method overloading is the process of having many methods with the same name, that differ in aspects like return type and number, * type and order of arguments. */ import java.util.Scanner; class Overloading { //This method returns the area of a circle, given its radius. static double circleArea (double radius) { //We are not required to store the result of the calculation in a variable. We can just return it directly. //In this case, the value is calculated first, and then returned. return Math.PI*radius*radius; } //This method returns the volume of a sphere, given its radius static double volumeOfSphere (double r) { return 4.0/3.0*Math.PI*Math.pow(r,3); } //This method returns the surface area of a solid hemisphere. static double surfaceArea (double rad) { return 3*Math.PI*rad*rad; } /* This method prints the calculated information. * We read in the radius in this method, instead of passing it in from a calling method. * This is a print method, so the return type is void. It also doesn't require any parameters, since we accept input here. */ static void print() { // We need to declare a new Scanner for every method that does user input. Scanner input = new Scanner (System.in); System.out.print("Enter the radius: "); double radius = input.nextDouble(); System.out.println("The area os the circle is "+circleArea(radius) + "\nThe voulme of the sphere is "+volumeOfSphere(radius) + "\nThe surface area of the hemisphere is "+surfaceArea(radius)); } /* This method demonstrates both method overloading and a method with default parameters. * This method is also called "print" and prints a triangle. * Since this is also a print method, its return type is the same as the previous method. * We differentiate it from the previous method by having it accept a parameters */ static void print (char symbol, int numLines, boolean isHorizontal ) { if(isHorizontal)// This prints a horizontal triangle, that points to the right. { if(numLines%2==0) { System.out.println("The horizontal triangle cannot have even number of lines. Reducing line count by 1."); numLines--; } for(int i=0;i<=numLines/2;i++) { for(int j =0;j<=i;j++) System.out.print(symbol+" "); System.out.println(); } for(int i=0;i