010-003-008

Method Overload: Variable Arguments

Medium

Problem Description

method Overload: variable Arguments

In this problem, you will create a program that reads integers from standard input, implements overloaded sum methods using both fixed arguments and variable-length arguments (varargs), and displays the result of each call to standard output.

Learning Objective: Understand and implement method overloading with variable-length arguments

Overview

Implement two versions of a sum method that calculates the total of multiple integers. One version takes two fixed arguments, the other uses varargs to accept any number of arguments.

Specifications

  • sum method (int, int): Returns sum of two integers
  • sum method (int...): Returns sum of any number of integers
  • In main method, read input values with Scanner and call both versions with 2 and 4 arguments respectively, then output results

Input Format

a b
c d e f
  • Line 1: Two integers a, b (space-separated)
  • Line 2: Four integers c, d, e, f (space-separated)

Output Format

Sum of 2 args: <sum of a and b>
Sum of 4 args: <sum of c, d, e, and f>

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up