CS100, Spring 2000, Project 6 Tips This is the third of 4 documents for Project 6: 1. Project 6 Write-Up 2. Project 6 Milestones --> 3. Project 6 Tips 4. Project 6 What To Hand In Here are some tips and reminders to help you with Project 6. This document is structured in a fashion parallel to the Milestones write-up. =============================================================================== Overall Tips =============================================================================== + You can use $DecimalFormat$ to print fewer floating point digits. + Do NOT instantiate arrays or objects unless actually needed. For example, this is legal code: Parthon a = new Parthon(); Parthon b = new Parthon(); // swap a and b Parthon tmp; tmp = a; a = b; b = tmp; Observe that we did NOT need $tmp = new Parthon()$. + Read the tip above: We really, really mean it!!! In the example above, if $a$, $b$, and $tmp$ were arrays, we would NOT need to instantiate/create an array for $tmp$. + Do not hard-code "magic numbers", e.g. used named constants and instance variable or method $length$ as appropriate. =============================================================================== Tips for Milestone 1: Decimal Format, Initializer Lists, Anonymous Arrays =============================================================================== + The example from section when we first saw nested loops is relevant: /* print 11 12 13 14 21 22 23 23 31 32 33 34 41 42 43 44 */ for (int i = 1; i <= 4; i++) { for (int j = 1; j<= 4; j++) System.out.print(i + "" + j + " "); System.out.println(); } + You can use $DecimalFormat$ to make columns line up; refer to Online examples and Lewis and Loftus. + An initializer list can contain (references to) objects, e.g. (references to) arrays. If a loop is no help for initializing an array, recall 3 other techniques. For example, consider creating the one dimensional array -20 37 48 -8. + Using only assignment statements is rather verbose: int[] x; x = new int[4]; x[0] = -20; x[1] = 37; x[2] = 48; x[3] = -8; + Using an initializer list is nice and concise: int[] x = { -20, 37, 48, -8 }; However, note that an initializer list is a special abbreviation -- the following is *not* legal Java code: ILLEGAL! --> int[] x; x = { -20, 37, 48, -8 }; <-- ILLEGAL! + Using an "anonymous array" is almost as concise as an initializer list: int[] x = new int[] { -20, 37, 48, -8 }; Furthermore, anonymous arrays can be used more generally, e.g. the following *is* legal Java code: int[] z; z = new int[] { -20, 37, 48, -8 }; =============================================================================== Tips for Milestone 2: Basic $char$ and $String$ operations =============================================================================== You will find some of the following tips helpful, but not necessarily all, and are not required to use all of them. $char$ tips: + Use $(int)$ or arithmetic on $char$s to convert a $char$ to its integer representation, e.g. $(int) 'a' - (int) 'A'$ or $'a' - 'A'$. + Use $(char)$ to convert an integer to the character it represents, e.g. $(char) 65$. + Integer representations for lower-case letters occur in sorted order. Integer representations for upper-case letters occur in sorted order. There is a constant integer "shift" between lower- and upper-case letters. + Characters can be compared, e.g. $'a'<'b'$ is $true$, $'b'<'a'$ is $false$. $String$ tips: + Use method $int length()$ to find the length of a String. + Use method $char charAt(int i)$ to find the character at position $i$ in a String. + Use constructor $String(char[] s)$ to create a String whose contents correspond to those of $char$ array $s$. + Use operator $+$ to concatenate Strings. + Use method $int indexOf(char c)$ to get the position of the first occurrence (from left-to-right, if it exists) of $c$ in a String. Note that $-1$ is reported ($return$ed) as the position for non-occurring characters. + Method $String toLowerCase()$ returns a string with all upper-case letters converted to lower-case. Here is an example that illustrates many, but not all, of these tips: // return result of transforming lower-case letters to upper-case String toUpper(String s) { char[] t = new char[s.length()]; for (int i = 0; i