Question: Write a method called getSum ( ) that takes one to three double parameters and returns the sum of these parameter ( s ) as

Write a method called getSum() that takes one to three double parameters and returns the sum of these parameter(s) as double. If any of the parameters is not an integer or a double, the program will catch an exception and print One or more arguments is not an int or a double.
When your code is ready, click the button below to submit your work for grading. //FREEZE CODE BEGINS
import java.util.*;
public class MethodOverloading {
//FREEZE CODE ENDS
//FREEZE CODE BEGINS
public static void main(String args[]){
try {
if(args.length ==1){
double x = Double.parseDouble(args[0]);
System.out.println(getSum(x));
}
if(args.length ==2){
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
System.out.println(getSum(x, y));
}
if(args.length ==3){
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
double z = Double.parseDouble(args[2]);
System.out.println(getSum(x, y, z));
}
}
//FREEZE CODE ENDS
//FREEZE CODE BEGINS
}
}
//FREEZE CODE ENDS Click to compile your code
COMPILE CODE
code/final/MethodOverloading.java:14: error: 'try' without 'catch', 'finally' or resource declarations
try {
^
code/final/MethodOverloading.java:32: error: illegal start of type
} finally (NumberFormatException e){
^
2 errors
Test your code with a few different values
TEST WITH 89.5
Expected Output:89.5
TEST WITH 55.55
Expected Output:10.55
TEST WITH 152133
Expected Output:69.0
TEST WITH HELLO
Expected Output:One or more arguments is not an int or a double

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!