Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
Thread: sodoku tools
03-10-2012, 04:50 PM #1
- Join Date
- Feb 2012
- Thanked 0 Times in 0 Posts
im having trouble writing the following metohds
int getNumAt(String str, int col, int row):
Must be a private static method that returns the integer value in the specified column and row of the specified nonet. Must return 0 if the specified String is not a valid nonet, of if the specified column or row is out of range (1 - 3
int getRowSum(String str, int row):
Must return the sum of the values in the specified row of the specified nonet.
Must return 0 if the specified String is not a valid nonet, or if the specified row is out of range (1 - 3).
int getColSum(String str, int col):
Must return the sum of the values in the specified column of the specified nonet. Must return 0 if the specified String is not a valid nonet, or if the specified column is out of range (1 - 3).
all of this will be working with an string input of 9 characters that are only digits
the string will be written as "123456789"
but is a matrix like this
any help is appreciated thanks
03-10-2012, 05:42 PM #2
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,659 Times in 2,628 Posts
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.