|
For the first, you can simply pull the length of the string to verify its 9, and then pull the char located at col * row. Cast it to an integer and return the results. You can verify the numbers by simply attempting to cast the entire string into a number, if its successful then all the characters are numbers. You don't want to use this to fetch the specific number though as that is quite difficult to do.
The second and third require a loop. The "grid" is always 3x3, so you simply want to pull the value of the char at the specified locations and sum them up. For rows, you choose 3 sequential chars starting at 3 * row. For columns you pull for every 3 % col so long as its treated as 1 based instead of 0 based. IMO, you would be better off chaining all of them into the getNumAt method to pull the three numbers you need (row 1 [not the first row, 1 is the second row] would use location @ 1,0; 1,1; 1,2; col 2 [not the second col, 2 is the last col] would use location @ 0,2; 1,2; 2,2;). Notice the trend you have above, so a single loop of 3 iterations is sufficient with a chain to the getNumAt call.
You could also write a Matrix class that uses type <Integer> as a generic. Then simply write the methods required within there to actually treat the storage as an Integer[][]/int[][] instead.
|