Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions