Introduction to Software Development
GWU Computer Science
By the end of this module, you will:
If you do not have your Codio course ready, use any text editor or simple IDE. Some possibilities are:
Before we move forward, let's catch up (complete any remaining work from the previous module)
In this case, make sure we've got:
Let's use word squares as an example:
String[] words = {"bard", "area", "rear", "dart"}; System.out.println ( isWordSquare(words) );is a word square, as in
b a r d a r e a r e a r d a r twhere each row is the same as its corresponding column.
public static void main (String[] argv) { char[][] letters = { {'b', 'a', 'r', 'd'}, {'a', 'r', 'e', 'a'}, {'r', 'e', 'a', 'r'}, {'d', 'a', 'r', 't'} }; System.out.println ( isWordSquare(letters) ); } static boolean isWordSquare (char[][] letters) { for (int i=1; i<letters.length; i++) { for (int j=0; j<letters.length; j++) { if (letters[i][j] != letters[j][i]) { return false; } } } return true; }
char[][] letters = ...
int[][] someNumbers = ...
char[] someLetters = {'b', 'a', 'r', 'd'};
char[][] letters = { {'b', 'a', 'r', 'd'}, {'a', 'r', 'e', 'a'}, {'r', 'e', 'a', 'r'}, {'d', 'a', 'r', 't'} };
char[][] letters={{'b','a','r','d'},{'a','r','e','a'},{'r','e','a','r'},{'d','a','r','t'}};
letters[1][2]refers to the element in row 1 (2nd row) and column 2 (3rd column):
char[][] letters = { {'b', 'a', 'r', 'd'}, {'a', 'r', 'e', 'a'}, {'r', 'e', 'a', 'r'}, {'d', 'a', 'r', 't'} };
letters[1]refers to the 1D array in position 1 of the array of rows:
char[][] letters = { {'b', 'a', 'r', 'd'}, {'a', 'r', 'e', 'a'}, {'r', 'e', 'a', 'r'}, {'d', 'a', 'r', 't'} };
letters[1][2]refers to the element ('e') in position 2 of that 1D array.
char[][] letters;
letters = new char[4][4];
letters[0][0] = 'b'; letters[0][1] = 'a'; // ...
char[][] letters = new char[4][4];
for (int i=0; i<letters.length; i++) { for (int j=0; j<letters.length; j++) { // Use print so that the row appears on one line: System.out.print (letters[i][j]); } // Go to next line for next row: System.out.println (); }
We could choose to print the elements of the main diagonal: top-left(TL) to bottom-right(BR) as in:
for (int i=0; i<letters.length; i++) {
System.out.print (letters[i][i]);
}
System.out.println ();
Activity 2:
In
MyWordSquare2.java
print the whole square, and both diagonals (TL-to-BR and BL-to-TR).
Dealing with rectangles:
for (int i=1; i<letters.length; i++) { for (int j=0; j<letters.length; j++) { // ... printing/whatever ... } }we have the same upper limit for both rows and colums.
char[][] letters = { {'f','r','a','c','t','u','r','e'}, {'o','u','t','l','i','n','e','d'}, {'b','l','o','o','m','i','n','g'}, {'s','e','p','t','e','t','t','e'} }; for (int i=1; i<letters.length; i++) { for (int j=0; j<letters[i].length; j++) { // ... } }
letters[i]is the whole row and therefore has a length:
letters[i].length
int[][] A = { {1}, {2,2}, {3,3,3}, {4,4,4,4} }; for (int i=0; i<A.length; i++) { for (int j=0; j<A[i].length; j++) { System.out.print (A[i][j]); } System.out.println (); }
int[][] A = { {1, 2, -3, 4}, {1, 22, 333, 4444}, {4, -5, 66, 777} };In the above example, you saw the the result was an unappealing jagged print.
We will instead use printf to neatly print a 2D array:
for (int i=0; i<A.length; i++) {
for (int j=0; j<A[i].length; j++) {
System.out.printf (" %5d", A[i][j]);
}
System.out.println ();
}
System.out.printf (" %5d", A[i][j]);specifies 5 digits for an integer after a space.
System.out.printf (" %8.3f", A[i][j]);to specify a total of 8 digits with 3 after the decimal point.
double[][] A = { {1.1, 2.222, -3.3, 4.4444}, {1.0, 2.2, 33.3, 444.4}, {0.4, -5.0, 0.66, 0.0777} };