1 / 31

BIT115: Introduction to Programming

BIT115: Introduction to Programming. Lecture 13. Instructor: Craig Duckett. Announcements. Assignment 3 Revision. Due TONIGHT Wednesday, August 13 th , by midnight. Assignment 4. Due next Wednesday, August 20 th , by midnight. Today’s Topics. Arrays (Recap) Arrays as Parameters

lei
Télécharger la présentation

BIT115: Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BIT115: Introduction to Programming Lecture 13 Instructor: Craig Duckett

  2. Announcements Assignment 3 Revision Due TONIGHTWednesday, August 13th, by midnight Assignment 4 Due next Wednesday, August 20th , by midnight

  3. Today’s Topics • Arrays (Recap) • Arrays as Parameters • Returning an Array • Assignment 4 Basic and Advanced Overview • WALK-THROUGH: ICE 17.1

  4. And now ... The Quiz

  5. Recap: Arrays What is an Array? • Primitive variables are designed to hold only one value at a time. • Arraysallow us to create a collection of like valuesthat are indexed. • An arraycan store any type of data but only one type of data at a time. • An arrayis a list of data elements.

  6. Recap: Arrays Let’s pretend we have a list of five (5) grades we’d like to store in memory. We could declare the value for each grade one at a time like this: int grade1 = 100; int grade2 = 89; int grade3 = 96; int grade4 = 100; int grade5 = 98; The five grades are then stored in memory wherever there happens to be room. The “address” in memory where each grade is store is it’s declared name (e.g., grade1) and the data stored there is the integer value itself (e.g., 100). To access each grade number and do something with it (e.g., display it), we’d call up the memory location by its assigned (declared) name: System.out.println("The first grade is: " + grade1);

  7. Recap: Arrays In memory, the array elements are mostly stored in sequential order We could, however, simplify our work by doing the same thing with an array. Instead of declaring each integer one at a time, we could declare the whole shebang at the same time, like so: Even though you declared an array with [5]elements, the “count” of the elements starts with [0] grades = newint[5]; Initially, this creates a location in memory to hold the five elements that make up the array, automatically initializing each of those storage spaces to zero. All you have to do now is fill those elements. grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; Of course, you could have also declared and set them up like this all in one step: int[ ] grades = { 100, 89, 96, 100, 98 };

  8. Recap: Arrays grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; Each individual element identifier in the index is called a subscript The entire collection of element identifiers is called the index

  9. Recap: Arrays Comparing individual integer declarations to an array of integers: int grade1 = 100; int grade2 = 89; int grade3 = 96; int grade4 = 100; int grade5 = 98; In memory, the individual integers are stored wherever there happens to be an available space System.out.println("The first grade is: " + grade1); grades = newint[5]; grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; In memory, the array elements are mostly stored in sequential order System.out.println("The first grade is: " + grade[0]);

  10. Recap: Arrays Because array elements are numbered sequentially, it is a no-brainer to either populate the data in the array, or to call up and access the data in the array using Loops Scanner keyboard = new Scanner(System.in); // Original code thanks to Kamran Majidimehr!intcounter = 0;intarraylength = 0;System.out.println("How many grades would you like to enter?");arraylength = keyboard.nextInt();System.out.println("You have indicated that you'd like to enter " + arraylength + " grades.\nPlease enter grades now, one at a time.");intcountdown = arraylength;doubleaverage = 0;doublegrades[] = newdouble [arraylength]; while( counter < arraylength ){ grades[counter] = keyboard.nextDouble(); System.out.println("(" + (counter + 1) + ") You entered: " + grades[counter] + " [There are " + (countdown - 1) + " grades remaining]"); counter++; countdown--; } With loops you can either enter data manually when prompted or enter data automatically based on the particular requirements of the data going into the array

  11. Arrays as Parameters

  12. Arrays as Parameters class ArrayHelper - PrintArray method main - shortArray - longArray

  13. Arrays as Parameters Passing an array as a parameter

  14. Arrays as Parameters

  15. Arrays as Parameters

  16. Arrays as Parameters

  17. Arrays as Parameters But how does this really work in memory? How does the PrintArray method actually know which array data to grab and use?

  18. Don’t worry about any of this memory, stack, or heap information as it won’t show up on the Final Exam  Arrays as Parameters Is allocated when program loads;“temporary” Is allocated when program runs;“persistent”

  19. Arrays as Parameters shortArray longArray (1) PrintArray method is called and and checks name in parameter which is a named storage space in the memory heap (2) PrintArray than takes the array element data as needed and plugs it through the arrayName placeholder into the body of the method where it can do its work

  20. Lecture 18: Returning an Array I’m going to go over the Lecture 18 part of this now, and repeat it again on Mondaywhere you will do the ICES for Returning an Array at that time.

  21. showArray(numbers); 5 10 15 20 25 30 35 40 Address public static void showArray(int[] array) { for (inti = 0; i < array.length; i++) System.out.print(array[i] + " "); } Passing Arrays as Arguments Passing and Returning Arrays • Arrays are objects. • Their references can be passed to methods like any other object reference variable. Example: PassArray.java

  22. Passing and Returning Arrays In Last Wednesday’s Class we learned how you could pass an array into a method. Today we’re going to take this to the next step to learn how you can return an array from a method. A method can return a reference to an array. To do so, the return type of the method must be declared properly. For example, look at the following method definition: public static double[]getArray() // <-- No parameters called { double[] array = {1.2, 2.3, 4.5, 6.7, 8.9} return array; } double is the return type return sends back a double • The getArray method is a public static method that returns an array of doubles. See example: ReturnArray.java

  23. Assignment 4 Basic (Demo) NOTE: You will need to pass an array as a parameterto “solve” Assignment 4 Basic, but you DO NOT need to return an array. After today’s ICE you will have learned everything you need to successfully complete Assignment 4 Basic

  24. Assignment 4 Advanced A look at the Advanced “Guessing Game” assignment… NOTE: You do NOT need to pass an array as a parameter to “solve” Assignment 4 Advanced, and you DO NOT need to return an array, although you could do it that way if you wanted to. You WILL need to return a Boolean ‘true’ or ‘false’ however in some of your methods as well as return the number guessed to be successful with Assignment 4 Advanced.

  25. ICE: Arrays as Parameters I’m going to walk-through the building of the PrintArray method for ICE PART 1 ICE_18_PrintArray.java

  26. importjava.util.*;classArrayHelperextends Object{public void PrintArray( int[] arrayName) // Whatever array is passed in main will populate arrayName here { // Use a for loop here that checks the count against the length of arrayName, then iterates if less than length // HINT: You might want to check out the SOLUTIONS to previous lecture ICE exercises // for ( ?? ; ?? ; ??) {// print out the array } }}public class ICE_18_PrintArray extends Object{public static void main(String[] args) {ArrayHelperarrayTester = newArrayHelper(); // We're creating the two arrays in two different ways: // the shortArray has 3 locations and the longArray has 5, but in the longArray // we can shorten the way we assign numbers using the single line "array literal" methodint[] shortArray; // Instantiate the shortArray here with 3 slots holding the three numbers: 1, 3, 5 // Add number to slot 0 // Add number to slot 1 // Add number to slot 2 int[] longArray; // fill the longArray as an "array literal" with five numbers: 1, 2, 3, 5, 7 // print out both the short and long arraysSystem.out.println("\nThe Short Array: ");arrayTester.PrintArray(shortArray); // Passing the shortArray to the PrintArray() methodSystem.out.println("\nThe Long Array: ");arrayTester.PrintArray(longArray); // Passing the longArray to the PrintArray() method }}

  27. importjava.util.*;classArrayHelperextends Object{public void PrintArray( int[] arrayName) // Whatever array is passed in main will populate arrayName here { // Use a for loop here that checks the count against the length of arrayName, then iterates if less than length // HINT: You might want to check out the SOLUTIONS to previous lecture ICE exercises // for ( ?? ; ?? ; ??) {// print out the array } }}public class ICE_18_PrintArray extends Object{public static void main(String[] args) {ArrayHelperarrayTester = newArrayHelper(); // We're creating the two arrays in two different ways: // the shortArray has 3 locations and the longArray has 5, but in the longArray // we can shorten the way we assign numbers using the single line "array literal" methodint[] shortArray = newint [3]; // Instantiate the shortArray here with 3 slots holding the three numbers: 1, 3, 5 shortArray[0] = 1; shortArray[1] = 3; shortArray[3] = 5;int[] longArray; // fill the longArray as an "array literal" with five numbers: 1, 2, 3, 5, 7 // print out both the short and long arraysSystem.out.println("\nThe Short Array: ");arrayTester.PrintArray(shortArray); // Passing the shortArray to the PrintArray() methodSystem.out.println("\nThe Long Array: ");arrayTester.PrintArray(longArray); // Passing the longArray to the PrintArray() method }}

  28. importjava.util.*;classArrayHelperextends Object{public void PrintArray( int[] arrayName) // Whatever array is passed in main will populate arrayName here { // Use a for loop here that checks the count against the length of arrayName, then iterates if less than length // HINT: You might want to check out the SOLUTIONS to previous lecture ICE exercises // for ( ?? ; ?? ; ??) {// print out the array } }}public class ICE_18_PrintArray extends Object{public static void main(String[] args) {ArrayHelperarrayTester = newArrayHelper(); // We're creating the two arrays in two different ways: // the shortArray has 3 locations and the longArray has 5, but in the longArray // we can shorten the way we assign numbers using the single line "array literal" methodint[] shortArray = newint [3]; // Instantiate the shortArray here with 3 slots holding the three numbers: 1, 3, 5 shortArray[0] = 1; shortArray[1] = 3; shortArray[3] = 5;int[] longArray = {1, 2, 3, 5, 7}; // print out both the short and long arraysSystem.out.println("\nThe Short Array: ");arrayTester.PrintArray(shortArray); // Passing the shortArray to the PrintArray() methodSystem.out.println("\nThe Long Array: ");arrayTester.PrintArray(longArray); // Passing the longArray to the PrintArray() method }}

  29. importjava.util.*;classArrayHelperextends Object{public void PrintArray( int[] arrayName) // Whatever array is passed in main will populate arrayName here { // Use a for loop here that checks the count against the length of arrayName, then iterates if less than length // HINT: You might want to check out the SOLUTIONS to previous lecture ICE exercisesfor( intcount = 0; count < arrayName.length; count++) {System.out.println( count + ") Number: " + arrayName[count]); } }}public class ICE_18_PrintArray extends Object{public static void main(String[] args) {ArrayHelperarrayTester = newArrayHelper(); // We're creating the two arrays in two different ways: // the shortArray has 3 locations and the longArray has 5, but in the longArray // we can shorten the way we assign numbers using the single line "array literal" methodint[] shortArray = newint [3]; // Instantiate the shortArray here with 3 slots holding the three numbers: 1, 3, 5 shortArray[0] = 1; shortArray[1] = 3; shortArray[3] = 5;int[] longArray = {1, 2, 3, 5, 7}; // print out both the short and long arraysSystem.out.println("\nThe Short Array: ");arrayTester.PrintArray(shortArray); // Passing the shortArray to the PrintArray() methodSystem.out.println("\nThe Long Array: ");arrayTester.PrintArray(longArray); // Passing the longArray to the PrintArray() method }}

  30. importjava.util.*;classArrayHelperextends Object{public void PrintArray( int[] arrayName) {for( intcount = 0; count < arrayName.length; count++) {System.out.println( count + ") Number: " + arrayName[count]); } }}public class ICE_18_PrintArray extends Object{public static void main(String[] args) {ArrayHelperarrayTester = newArrayHelper(); // We're creating the two arrays in two different ways: // the shortArray has 3 locations and the longArray has 5, but in the longArray // we can shorten the way we assign numbers using the single line "array literal" methodint[] shortArray = newint [3]; // Instantiate the shortArray here with 3 slots holding the three numbers: 1, 3, 5 shortArray[0] = 1; shortArray[1] = 3; shortArray[3] = 5;int[] longArray = {1, 2, 3, 5, 7}; // print out both the short and long arraysSystem.out.println("\nThe Short Array: ");arrayTester.PrintArray(shortArray); System.out.println("\nThe Long Array: ");arrayTester.PrintArray(longArray); }} arrayName shortArray longArray

  31. ICE: Arrays as Parameters • In-Class Exercises: Arrays as Parameters The ICE exercises will go the remainder of class. If you finish early you are either: free to go home for the evening meet with your team members regarding Assignment 4 work on Assignment 4

More Related