Question

In: Computer Science

Write a java program that contains 3 overloaded static methods for calculating area of a circle,...

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!!

Solutions

Expert Solution

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);

}
}


Related Solutions

Write a class that has three overloaded static methods for calculating the areas of the following...
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: - circles - rectangles - cylinders Here are the formulas for calculating the area of the shapes. Area of a circle: Area = π r2, where p is Math.PI and r is the circle's radius Area of a rectangle: Area = Width x Length Area of a cylinder: Area = π r2 h, where p is Math.PI, r is the radius of...
Write a class that has three overloaded static methods for calculating the areas of the following...
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: - circles - rectangles - cylinders Here are the formulas for calculating the area of the shapes. Area of a circle: Area = π r2, where p is Math.PI and r is the circle's radius Area of a rectangle: Area = Width x Length Area of a cylinder: Area = π r2 h, where p is Math.PI, r is the radius of...
write the program named Lab06.java that contains the following four static methods: • public static double...
write the program named Lab06.java that contains the following four static methods: • public static double max(double[] data) that returns the maximum value in the array data • public static double min(double[] data) that returns the minimum value in the array data • public static double sum(double[] data) that sums all items in the array and return the result • public static double ave(double[] data) that call the sum() method and then return the average. Once you have completed the...
JAVA program. Write two public classes (named exactly), TextBox and TextBoxTester.  TextBox contains the following overloaded static...
JAVA program. Write two public classes (named exactly), TextBox and TextBoxTester.  TextBox contains the following overloaded static methods called textBoxString. This method returns a String value. public static String textBoxString (int side) The returned String value, when printed, displays as the outline of a square of side characters. The character you use is up to you. Don't forget that '\n' will force a newline character into the returned String. For example, let's assume I want to use * as the character...
/* Writing a program that displays the following: calculating area of a circle, calculating area of...
/* Writing a program that displays the following: calculating area of a circle, calculating area of a rectangle, calculating area of a triangle, and quit. */ import java.text.DecimalFormat; import java.util.Scanner; public class GeoCalculator {    public static void main(String arg[]) { char selection; Scanner get = new Scanner(System.in); //having user input DecimalFormat twoDigits = new DecimalFormat("0.00"); //formatting area to two decimal places //display choice of selection System.out.println("Geometry Calculator"); System.out.println("Please select from the following menu:"); System.out.println("1. Calculate the Area of a...
In java processing 3, write a program that draws a circle. The center of the circle...
In java processing 3, write a program that draws a circle. The center of the circle would be first point on the canvas where the mouse is pressed on it. While the mouse is pressed, the circle can be drawn by moving the mouse cursor farther than the first point that mouse was pressed (circle center). As soon as the code is run, a timer at the bottom of the canvas should start working. Also, the radius of the circle...
/* calculating area of a circle, calculating area of a rectangle, calculating area of a triangle,...
/* calculating area of a circle, calculating area of a rectangle, calculating area of a triangle, and quit. */ import java.util.Scanner; public class GeoCalculator {    public static void main(String arg[]) { int geoCalc; //number selection of user Scanner get = new Scanner(System.in); //display section. System.out.println("Geometry Calculator"); System.out.println("Please select from the following menu:"); System.out.println("1. Calculate the Area of a Cirlce."); System.out.println("2. Calculate the Area of a Rectangle."); System.out.println("3. Calculate the Area of a Triangle."); System.out.println("4. QUIT"); System.out.println("Please make a selection:...
(Java) Create a program using 3 methods. The methods must be public and static. Ask the...
(Java) Create a program using 3 methods. The methods must be public and static. Ask the user for 3 numbers, then call the methods from main to print max, min, and average. The first method max (int x, int y, int z) returns the maximum value of 3 integer values. The second method min (int X, int y, int z) returns the minimum value of 3 integer values. And the third average (int x, int y, int z) returns the...
Write in JAVA eclipse program (comment in every line): class for calculating an area of a...
Write in JAVA eclipse program (comment in every line): class for calculating an area of a room. The program MUST ask for the length and the width of the room. Make sure you write the methods to get the width and length and print the area.
Question 3 A java source module contains the following class with the static methods main and...
Question 3 A java source module contains the following class with the static methods main and procedure1, and the instance method procedure2 (assume given the bodies of procedure1 and procedure2): public class TestQuestion3             {                         static int result, num1 = 10;                         public static void Main( String [ ] args )                         {                                     int [ ] list1 =   { 2, 4, 6, 8, 10}, list2;                                     .    .    .                         }                         static void procedure1( void )                         {                                     .   .   .                         } void procedure2( void )...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT