BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
BIT 115: Introduction To Programming 2 Quiz 15 hington.edu/webq/surve y/babaw/ hington.edu/webq/surve y/babaw/
BIT 115: Introduction To Programming Today… Arrays again (passing, using) –Ch ICE 15 on website Stepwise Refinement, Pseudocode –Ch No class Monday (Memorial Day) More on Arrays, Stepwise Refinement, and Pseudocode on Wednesday
BIT 115: Introduction To Programming 4 Careful thinking What makes programming difficult isn't the typing –Isn't not the Java language –It's not getting the computer to work It's the level of detail that the computer forces you to think at –Simultaneously high-level to figure out the big picture –All the way down to the nitty-gritty, to figure out individual services. You need to think through the problems, very carefully –With a fine-toothed comb 4
BIT 115: Introduction To Programming 5 Stepwise Refinement A good general process for doing both top- down design and bottom-up design together See 3.2.8, p. 132 Use pseudocode in TODO comments to document your plans along with method stubs –Then begin implementing them leaving the comments there and updating them if your plans change –Avoids forgetting & getting lost! –Comments easy to convert to JavaDoc 5
BIT 115: Introduction To Programming 6 Software Design Questions to Ask Yourself What’s the big picture? What are we trying to DO (usually solving a problem) for some USER? –What should the user experience? What classes do we need for that? –What classes from Sun or 3rd party (like Becker) do I need to import (check JavaDoc websites)? –For the remaining classes I must write, what methods/services should be in those classes? How do we implement those with code? What code like that have I seen before (from a book, website, previous code I wrote) and can copy and rewrite for my needs? UP DOWN
BIT 115: Introduction To Programming 7 Working in BIT115 NEVER remove a comment I put into an assignment! You can write another comment under it to explain why you didn’t do it –Why you failed to do it –Why my plan was in error –Either way you should ask me a question in- person or via after checking with the instructions, textbook, JavaDocs, and partner! 7
BIT 115: Introduction To Programming 8 ICE 15 int[] longArray = new int[14]; // We want to put numbers into the long array in reverse order. // We should thus count DOWN through the slot numbers (with arrayIndex), // and simultaneously have a counter (increasingCounter) count UP. int increasingCounter = 0; for (int arrayIndex = longArray.length - 1; arrayIndex >= 0; arrayIndex--) { longArray[arrayIndex] = (increasingCounter + 1) * 3; increasingCounter++; } 8
BIT 115: Introduction To Programming 9 ICE 15 int[] longArray = new int[14]; // We want to put numbers into the long array in reverse order. // We should thus count DOWN through the slot numbers (with arrayIndex), // and simultaneously have a counter (increasingCounter) count UP. int increasingCounter = 0; for (int arrayIndex = longArray.length - 1; arrayIndex >= 0; arrayIndex--) { longArray[arrayIndex] = (increasingCounter + 1) * 3; increasingCounter++; } 9
BIT 115: Introduction To Programming 10 Passing An Array To A Method 2 parts: 1.Declaring the parameter In the parameter list, put the [] after the type, similar to declaring one as a variable 2.Passing the array to the method (service) Make sure NOT to include () after the name of the array argument Example: public void printArray(int [] array) { int i; for(i = 0; i < array.length; i++) { System.out.println(array[i]); } Note that arrays are always passed by reference 10
BIT 115: Introduction To Programming 11 Returning An Array From A Method 2 parts: 1.Declaring the return type In the parameter list, put the [] after the type, similar to declaring one as a variable 2.Returning the array Make sure NOT to include () after the name of the array argument Example: public int[] createArray(int howMany) { int [] newArray = new int[howMany]; return newArray; } Note that arrays are always passed by reference 11