Skip to content

Instantly share code, notes, and snippets.

@palaashatri
Created December 12, 2023 12:48
Show Gist options
  • Save palaashatri/5c473d49ccbe9cbdce6ee91518de9ad3 to your computer and use it in GitHub Desktop.
Save palaashatri/5c473d49ccbe9cbdce6ee91518de9ad3 to your computer and use it in GitHub Desktop.
Area Calculator
/**
Area Calculator
Write a method named area with one double parameter named radius.
The method needs to return a double value that represents the area of a circle.
If the parameter radius is negative then return -1.0 to represent an invalid value.
Write another overloaded method with 2 parameters x and y (both doubles), where x and y represent the sides of a rectangle.
The method needs to return an area of a rectangle.
If either or both parameters is/are a negative return -1.0 to indicate an invalid value.
For formulas and PI value please check the tips below.
Examples of input/output:
area(5.0); should return 78.53981633974483 or 78.53981
area(-1); should return -1 since the parameter is negative
area(5.0, 4.0); should return 20.0 (5 * 4 = 20)
area(-1.0, 4.0); should return -1 since first the parameter is negative
NOTE: All methods need to be defined as public static ​like we have been doing so far in the course.
NOTE: Do not add a main method to solution code.
**/
public class AreaCalculator {
// write code here
public static double area(double radius) {
return (radius >= 0) ? Math.PI * radius * radius : -1.0;
}
public static double area(double x, double y) {
return (x >=0 && y >=0 ) ? x * y : -1.0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment