In: Computer Science
Write a java program that contains 3 overloaded static methods for calculating area of a circle, area of a cylinder and volume of a cylinder. Also create an output method which uses JOptionPaneto display instance field(s) and the result of the computing. Then code a driver class which will run and test calling each of these overloaded methods with hard-coded data and display the data and the result of the calculation by calling output method.
Thanks!!
import javax.swing.JOptionPane;
public class AreaForAll
{
public static void area(double w)
{
//Area of Circle
double area = 3.14*(w*w);
JOptionPane.showMessageDialog(null,"Area of Circle is " + area+"
with radius = "+w);
}
public static void area(int w,int h)
{
//Area of Cylinder
double area = 2*3.14*w*(w+h);
JOptionPane.showMessageDialog(null,"Area of Cylinder is " + area+"
with radius = "+w+" and height = "+h);
}
public static void area(double r, double h)
{
//Volume of a Cylinder
double area = Math.PI * (r*r) *h;
JOptionPane.showMessageDialog(null,"Volume of Cylinder
is "+area+" with radius = "+r+" and height = "+h);
}
public static void main(String[] args)
{
String input;
int radius;
int radius1;
int height1;
double radius2;
double height2;
AreaForAll obj = new AreaForAll();
input = JOptionPane.showInputDialog("Enter Radius For Circle To
Calculate Area of Circle");
radius = Integer.parseInt(input);
obj.area(radius);
input = JOptionPane.showInputDialog("Enter Radius For Cylinder To
Calculate Area of Cylinder");
radius1 = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Enter Height For
Cylinder To Calculate Area of Cylinder");
height1 = Integer.parseInt(input);
obj.area(radius1,height1);
input = JOptionPane.showInputDialog("Enter Radius For Cylinder To Calculate Volume of Cylinder");
radius2 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter Height For
Cylinder To Calculate Volume of Cylinder");
height2 = Double.parseDouble(input);
obj.area(radius2,height2);
}
}