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;
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;
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;
Submitted by : Mara.Lokesh 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 punishments.After many escapes he finally caught to his principal who is ready to give him a punishment.To 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 immediately.To give him confidence the principal will let raviknow how many numbers are going to be there that has to be added.Use 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.