008-005-007
Method Overloading: Addition Methods
Medium
Problem Description
Method Overloading: Addition Methods
In this problem, you will create a program that defines two add methods with the same name but different numbers of parameters in the Calculator class, reads values from standard input, and prints the computed sums.
Learning Objective: Understand how to define multiple methods with the same name but different parameter types or counts
Overview
Define two add methods in the Calculator class. One receives two int arguments, the other receives three int arguments. Both return the sum as a return value. In the Main class, read numbers from standard input and call each add method.
Specifications
- Define the following two add methods in the Calculator class
public int add(int a, int b)- return sum of two numberspublic int add(int a, int b, int c)- return sum of three numbers
- In Main, read input values using Scanner and print the results of calling both overloads
Input Format
Line 1: a b (two integers for the 2-argument add, space-separated)
Line 2: a b c (three integers for the 3-argument add, space-separated)
Output Format
Line 1: result of add(a, b)
Line 2: result of add(a, b, c)
Examples
Input:
10 20
10 20 30
Output:
30
60
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