010-002-012

Overloading with Varargs

Medium

Problem Description

Overloading with Varargs

In this problem, you will create a program that reads integers from standard input and implements overloaded sum methods using both fixed parameters and varargs, calls them with 2, 3, and 5 arguments respectively, and displays the sum results to standard output.

Learning Objective: Understand the distinction between methods using varargs and fixed-parameter methods

Overview

Varargs (...) allow defining methods with an indefinite number of parameters. Combined with fixed-parameter overloads, flexible APIs can be built.

Specifications

  • sum(int a, int b): returns sum of two integers
  • sum(int... numbers): returns sum of varargs integers
  • Read integers from standard input and call with 2, 3, and 5 arguments, displaying results

Input Format

a b
a b c
a b c d e

Line 1: 2 integers (for fixed-parameter version)
Line 2: 3 integers (for varargs version)
Line 3: 5 integers (for varargs version)

Output Format

Sum of 2 numbers: 30
Sum of 3 numbers: 60
Sum of 5 numbers: 150

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