-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAreaCalculator.java
More file actions
47 lines (32 loc) · 1.59 KB
/
AreaCalculator.java
File metadata and controls
47 lines (32 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*********************************************************************************************************************
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 {
public static double area(double radius){
if (radius<0){
return -1.0;
}
return 3.14159*radius*radius;
}
public static double area(double x, double y){
if (x<0 || y<0){
return -1.0;
}
return x*y;
}
}