In: Computer Science
/**
* Chapter 6
* Programming Challenge 1: Area Class
* This program demonstrates the Area class.
*/
public class AreaDemo
{
public static void main(String[] args)
{
// Get the area of a circle with a radius of 20.0.
System.out.println("The area of a circle with a " +
"radius of 20.0 is " +
Area.getArea(20.0));
// Get the area of a rectangle with a length of 10
// and a width of 20.
System.out.println("The area of a rectangle with a " +
"length of 10 and a width of 20 is " +
Area.getArea(10, 20));
// Get the area of a cylinder with a radius of 10.0
// and a height of 15.0.
System.out.println("The area of a cylinder with a " +
"radius of 10.0 and a height" +
"of 15.0 is " +
Area.getArea(10.0, 15.0));
}
}
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly
indented for better understanding.
Area class
/*
This class demonstrates the concept of OVERLOADING methods.
*/
public class Area
{
// only one parameter, CIRCLE
public static double getArea(double radius)
{
return Math.PI * radius * radius;
}
// TWO parameters (int,int), it's a RECTANGLE
public static double getArea(int length, int width)
{
return length*width;
}
// TWO parameters (double,double), it's a CYLINDER
public static double getArea(double radius, double height)
{
return (2*Math.PI*radius) * (radius + height);
}
}
====================
AREA DEMO
/**
* Chapter 6
* Programming Challenge 1: Area Class
* This program demonstrates the Area class.
*/
public class AreaDemo
{
public static void main(String[] args)
{
// Get the area of a circle with a radius of 20.0.
System.out.println("The area of a circle with a " +
"radius of 20.0 is " +
Area.getArea(20.0));
// Get the area of a rectangle with a length of 10
// and a width of 20.
System.out.println("The area of a rectangle with a " +
"length of 10 and a width of 20 is " +
Area.getArea(10, 20));
// Get the area of a cylinder with a radius of 10.0
// and a height of 15.0.
System.out.println("The area of a cylinder with a " +
"radius of 10.0 and a height" +
"of 15.0 is " +
Area.getArea(10.0, 15.0));
}
}
===============
OUTPUT:

