Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

help me to finish those 2 question int[] data = {}; //data array store digits in

ID: 3904385 • Letter: H

Question

help me to finish those 2 question

int[] data = {}; //data array store digits in this natural number

/**

* THIS IS A HELPER method which converses a digit character to its integer value.

* @param c is character and assumed be one of the following '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'

* @return an integer value of character in c

*/

private int toInt(char c)

{

return (int)c - (int)'0';

}

/**

* A parameterized constructor

* @param initData is a String containing the digits of this NaturalNumber

* instantiate the data array to correct size and store the digits from initData

* as an integer in array data.

*

* The constructor should use its setter to set each digit to

*/

public NaturalNumber(String initData)

{

// write code

return;

}

/**

* A setter : setting an integer between 0 to 9 to a digit specified by parameter digitIndex to the data array

* @param digitIndex is the index to data array

* @param value is an integer between 0 and 9

*

* setter validation:

* @param digitIndex must be between 0 and data.length - 1 AND @param value must be between 0 and 9

* otherwise, this setter should not do anything to the data array.

*/

public void setDigit(int digitIndex, int value)

{

//write code

return;

}

Explanation / Answer

Completed methods:

public NaturalNumber(String initData)

{

              data = new int[initData.length()];

              for(int i=0; i<initData.length(); i++)

              {

                             setDigit(i, toInt(initData.charAt(i)));

              }

              return;

}

public void setDigit(int digitIndex, int value)

{

              if(digitIndex>=0 && digitIndex<=data.length-1)

              {

                             if(value>=0 && value<=9)

                                           data[digitIndex] = value;

              }

              return;

}