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↵

Solutions

Expert Solution

import java.util.Scanner;

public class Area {

  public static double area(double r){
    return Math.PI * r * r;
  }

  public static double area(double width, double height){
    return width*height;
  }

  public static double area(double pi, double r, double h){
    return pi * r * r * h;
  }

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.println("=== Area Calculator ===\n");
    System.out.print("Enter radius to calculate circle area:");
    double radius = scanner.nextDouble();

    System.out.print("Enter width to calculate rectangle area:");
    double width = scanner.nextDouble();

    System.out.print("Enter length to calculate rectangle area:");
    double length = scanner.nextDouble();

    System.out.print("Enter base radius to calculate cylinder area:");
    double baseRadius = scanner.nextDouble();

    System.out.print("Enter height to calculate cylinder area:");
    double height = scanner.nextDouble();
    System.out.println("\n--------");
    System.out.println("Results:");
    System.out.println("--------\n");

    System.out.printf("The area of the circle is: %.2f\n", area(radius));
    System.out.printf("The area of the rectangle is: %.2f\n", area(width,length));
    System.out.printf("The area of the cylinder is: %.2f\n", area(Math.PI, baseRadius, height));
  }
}


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