public class DataSet { public DataSet(int aNum1, int aNum2, int aNum3, int aNum4) { Num1 = aNum1; Num2 = aNum2; Num3 = aNum3; Num4 = aNum4; } public void addvalue(int x) { x=x+1; } public int getSUM() { SUM = Num1 + Num2 + Num3 + Num4; return SUM; } public double getAVE() { double AVE; AVE = SUM/x; return AVE; } private int Num1; private int Num2; private int Num3; private int Num4; private int SUM; private int x; }
what i need is that x counts how many numbers i input, when i run the program like this it says that the value cannot be divided by 0, how can I fix this?
Your addvalue method is not updating the value of instance variable x as you intended. Therefore, the instance variable x is always zero and that is why you are getting the error. Make the following change in your addvalue method. To refer to instance variable x write this.x. Simply x means your parameter name x for your method addvalue.