Question

In: Computer Science

Write a method named area with one double parameter name radius. The method needs to return...

Write a method named area with one double parameter name 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 present an invalid value

Write another overload method with 2 parameters side1 and side2 (both double) where side1 and side2 represent the sides of a rectangle. The method needs to return an area of a rectangle . If either or both parameters is/ are negative return -1.0 indicate an invalid value.

Example output:

area(5.0) should return 78.53975

area(-1) should return -1 since the parameter is nagative

area(5.0, 6.0) should return 30.0 (5 * 6 = 30)

All method need to be defined as public static

Add a main method to your solution code

Please write in Java

Solutions

Expert Solution

Explanation:

Here is the code which has 2 methods, both having the same name area() but different number of parameters.

Code:



public class Main
{
public static double area(double radius)
{
if(radius<0)
return -1.0;
  
return Math.PI*radius*radius;
}
  
public static double area(double side1, double side2)
{
if(side1<0 || side2<0)
return -1.0;
  
return side1*side2;
}
   public static void main(String[] args) {
      
       System.out.println(area(5.0));
       System.out.println(area(-1.0));
       System.out.println(area(5.0, 6.0));
   }
}

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

PLEASE COMMENT IF YOU NEED ANY HELP!


Related Solutions

Write a method named area with one double parameter name radius, the method needs to return...
Write a method named area with one double parameter name 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 represents an invalid value write another overload method with 2 parameter side 1 and side 2 (double) where side 1 and side 2 represents the sides of a rectangle. The method needs to return an area of a rectangle. If either or both parameters...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
Write a method that, given an array of grades stored as double values, computes and return...
Write a method that, given an array of grades stored as double values, computes and return their mean.    Write another method that will return an array of the mean grade obtained by an arbitrary number of student.    This method will take a 2-dimensional array of double values in which each row corresponds to the grade vector    for a different student. You can compute the mean for each row as you did before...    Once you are done,...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
Which is NOT a valid return type for a method declaration Select one: a. double b....
Which is NOT a valid return type for a method declaration Select one: a. double b. char c. null d. String e. boolean f. float g. int h. void Statements that allow a program to choose, based on a boolean expression, between alternative blocks of code are known as: Select one: a. Selection statements or Conditionals b. Declarations c. Methods d. Repetition Statements or Loops e. Composition
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter...
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter is true, the function returns current hours; if the Boolean parameter is false, the function returns current month. o This function will obtain the current system time and extract the hours or the month value from the system time depending on the Boolean being received. o This function will return the extracted value as an integer value. o Note that the system time displays...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT