0% found this document useful (0 votes)
85 views2 pages

Java Constructor Chaining for Addition

Here is the code to help Ravi solve his math problem using constructor overloading and chaining in Java: public class MathHelper { int num1, num2, num3, num4; public MathHelper(int x) { switch(x) { case 2: num1 = 0; num2 = 0; break; case 3: num1 = 0; num2 = 0; num3 = 0; break; case 4: this(3); num4 = 0; break; } } public MathHelper(int a, int b) { this(); num1 = a; num2 = b;

Uploaded by

lokesh mara
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views2 pages

Java Constructor Chaining for Addition

Here is the code to help Ravi solve his math problem using constructor overloading and chaining in Java: public class MathHelper { int num1, num2, num3, num4; public MathHelper(int x) { switch(x) { case 2: num1 = 0; num2 = 0; break; case 3: num1 = 0; num2 = 0; num3 = 0; break; case 4: this(3); num4 = 0; break; } } public MathHelper(int a, int b) { this(); num1 = a; num2 = b;

Uploaded by

lokesh mara
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TS:ADV JAVA : SKILL:2

Submitted by : [Link] naganka sri prabhu surya sai.

ID:2000031385

SEC : 4 B

DATE : 06-08-21.

Problem: HELPING A CONFUSED STUDENT (Constructor chaining… Numbers)Ravi who is lazy


to do his homework given by his math teacher,always finds a way to escape his
[Link] many escapes he finally caught to his principal who is ready to give him a
[Link] test his knowledge in mathematics the principal randomly giving numbers for
addition (2,3,4) numbers they might be a combination of decimals and numbers -If 4 numbers are
given return the sum of first two and first three and four number’s sum-if 3 numbers are given
return sum of first two and three numbers sum-if two numbers are given return return sum of
two numbersAnd he asked to answer [Link] give him confidence the principal will let
raviknow how many numbers are going to be there that has to be [Link] constructor
overloading and constructor chaining concept in java and help him by developing java code
effectively to solve his problemNOTE- use switch case INPUT:X-Case of many numbers to be
addeddecimals and numbers accordingly OUTPUT:Addition of 1sttwo,three and four given
numbers.

Screen shots:
OUTPUT:

You might also like