Question

In: Computer Science

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 the cylinder's base, and h is the cylinder's height

Because the three methods are to be overloaded, they should each have the same name, but different parameter lists. Demonstrate the class in a complete program.

Sample Run
java Area

===·Area·Calculator·===↵

·Enter·radius·to·calculate·circle·area:2.0↵
·Enter·width·to·calculate·rectangle·area:3.5↵
·Enter·length·to·calculate·rectangle·area:4.0↵
·Enter·base·radius·to·calculate·cylinder·area:8.0↵
·Enter·height·to·calculate·cylinder·area:5.7↵
·↵
--------↵
Results:↵
--------↵
↵ The·area·of·the·circle·is:·12.57↵
The·area·of·the·rectangle·is:·14.00↵
The·area·of·the·cylinder·is:·1146.05↵

(In Java)

Solutions

Expert Solution

NOTE : PROGRAM IS DONE WITH JAVA PROGRAMMING LANGUAGE.

HERE area_shape CLASS HAS THREE OVERLOADED STATIC METHOD area() WITH DIFFERENT PARAMETERS, THEY AREA CALLED IN MAIN FUNCTION OF DRIVER CLASS overload_area.

IN overload_area CLASS THE OBJECT ob IS CREATED BY CLASS  area_shape AND ALL OVERLOADED STATIC METHODS ARE EXECUTED WITH THE OBJECT ob.

PROGRAM

import java.util.Scanner;
class area_shape
{
/* static function area is overloaded with different parameters*/
public static double area(double r)
{
double res;
   res=Math.PI*r*r;
   return res;
}
public static double area(double w, double h)
{
double res;
   res=w*h;
   return res;
}
public static double area(double r, double h,double p)
{
double res;
   res=p*r*r*h;
   return res;
}
}

public class overload_area
{
public static void main(String[] args)
{
   double r,width,length,h,br,p;
   /* scanner object */
   Scanner sc=new Scanner(System.in);
   /* ob is object of class area_shape*/
   area_shape ob= new area_shape();
   p=Math.PI;
   /* console input */
   System.out.println("\n=== Area Calculator ===\n");
   System.out.print("Enter radius to calculate circle area :");
   r=sc.nextDouble();
   System.out.print("Enter width to calculate rectangle area:");
   width=sc.nextDouble();
   System.out.print("Enter length to calculate rectangle area:");
   length=sc.nextDouble();
   System.out.print("Enter base radius to calculate cylinder area:");
   br=sc.nextDouble();
   System.out.print("Enter height to calculate cylinder area::");
   h=sc.nextDouble();
   System.out.println("\n--------------");
   System.out.println("Results:");
   System.out.println("--------------");
   /* print result*/
   System.out.printf("The area of the circle is: %.2f\n",ob.area(r));
System.out.printf("The area of the rectangle is: %.2f\n",ob.area(width,length));
System.out.printf("The area of the cylinder is: %.2f\n", ob.area(br,h,p));
   }
}  

SCREEN SHOT

OUTPUT


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 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!!
Coding Java Assignment Write the following static methods. Assume they are all in the same class....
Coding Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a class...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
Create a class named Billing that includes three overloaded computeBill() methods for a photo book store....
Create a class named Billing that includes three overloaded computeBill() methods for a photo book store. • When computeBill() receives a single parameter, it represents the price of one photo book ordered. Add 8% tax, and return the total due. • When computeBill() receives two parameters, they represent the price of a photo book and the quantity ordered. Multiply the two values, add 8% tax, and return the total due. • When computeBill() receives three parameters, they represent the price...
Create a class named Billing that includes three overloaded computeBill() methods for a photo book store....
Create a class named Billing that includes three overloaded computeBill() methods for a photo book store. • When computeBill() receives a single parameter, it represents the price of one photo book ordered. Add 8% tax, and return the total due. • When computeBill() receives two parameters, they represent the price of a photo book and the quantity ordered. Multiply the two values, add 8% tax, and return the total due. • When computeBill() receives three parameters, they represent the price...
Java Create a class named Billing that includes three overloaded computeBill() methods for a photo book...
Java Create a class named Billing that includes three overloaded computeBill() methods for a photo book store. main() main() will ask the user for input and then call functions to do calculations. The calculations will be returned to main() where they will be printed out. First function Create a function named computeBill that receives on parameter. It receives the price of an item. It will then add 8.25% sales tax to this and return the total due back to main()....
In Lab 4, you made a class with static methods. The static methods converted an integer...
In Lab 4, you made a class with static methods. The static methods converted an integer in binary, Hex, and Octal. To do this, you made a class of static methods. The class did not have any fields. The static methods received the integer value as a parameter. For this lab, make all of the static methods, non-static methods. Lab 5 Requirements Create a class named “Lab5_IntConv_Class Have one private field name intValue. Add two constructors: One is a default...
Design a Geometry class with the following methods: * A static method that accepts the radius...
Design a Geometry class with the following methods: * A static method that accepts the radius of a circle and returns the area of the circle. Use the following formula: Area=?r^2 * A static method that accepts the length and width of a rectangle and returns the area of the rectangle. Use the folliwng formula: Area = Length x Width * A static method that accepts the length of a triangle's base and the triangle's hight. The method should return...
a) Describe the two methods for calculating areas enclosed by irregular lines, and comment on the...
a) Describe the two methods for calculating areas enclosed by irregular lines, and comment on the use and limitations the methods. b) What is a planimeter and name the two types of planimeters. c) Describe how the volume of a reservoir can be calculated from contours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT