1 / 35

COMP 110: Introduction to Programming

COMP 110: Introduction to Programming. Tyler Johnson Mar 30, 2009 MWF 11:00AM-12:15PM Sitterson 014. Announcements. Program 3 has been graded. Questions?. Today in COMP 110. Review from last time Arrays in Classes & Methods Intro to Sorting Programming Demo. Arrays.

Download Presentation

COMP 110: 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. COMP 110:Introduction to Programming Tyler Johnson Mar 30, 2009 MWF 11:00AM-12:15PM Sitterson 014

  2. Announcements • Program 3 has been graded

  3. Questions?

  4. Today in COMP 110 • Review from last time • Arrays in Classes & Methods • Intro to Sorting • Programming Demo

  5. Arrays • Review from last time

  6. Arrays • A special kind of object in Java used to store a collection of data • What if you wanted to store 80 basketball scores? • Instead of declaring 80 integer variables, declare a single array!

  7. Array Details Syntax for creating an array: Base_Type[] Array_Name = newBase_Type[Length]; Example: int[] pressure = new int[100]; //create 100 variables of type int that can //be referred to collectively Alternatively: int[] pressure; //declare an integer array called pressure pressure = new int[100]; //allocate memory for the array to hold 100 ints

  8. Arrays The array itself is referred to by a name “scores” or “vector” (in our examples) Indices the array scores scores[3]

  9. Array Length An array is a special kind of object It has one public instance variable: length length is equal to the length of the array Pet[] pets = new Pet[20]; int sizeOfArray = pets.length; //sizeOfArray will have the value 20 You cannot change the value of length (it is final)

  10. For Loops and Arrays • For loops are often perfectly suited to processing arrays • Why? • Because we know the number of iterations (array.length) int[] pressure = new int[100]; for(int index = 0; index < pressure.length; index++) scores[index] = 0;

  11. Be Careful with Indices Indices MUST be in bounds double[] entries = new double[5]; entries[5] = 3.7; Your code WILL compile with an out-of-bounds index But it will result in a run-time error (crash) //RUN-TIME ERROR! Index out of bounds

  12. Arrays in Classes & Methods • Section 7.2 in text

  13. Arrays as Instance Variables Arrays can be used as instance variables in classes public class Pressure { private double[] pressure; } public class Course { private Student[] enrolledStudents; }

  14. Arrays as Instance Variables public class Pressure { private double[] pressure; //ask user for the number of pressure values and read them in public void getData() { System.out.println("Hi. How many pressure values would you like to enter?"); Scanner keyboard = new Scanner(System.in);int num = keyboard.nextInt();pressure = new double[num]; System.out.println("Ok. Please enter " + num + " integers");for(int i = 0; i < pressure.length; i++) { pressure[i] = keyboard.nextDouble(); }} } An array instance variable Create storage for the array Write values into the array

  15. Arrays of Objects • Creating an array of objects does not initialize the individual objects Student[] students = new Student[35]; students[0].getGPA(); //run-time error, students[0] holds no object • Each object in the array must be instantiated students[0] = new Student(); students[1] = new Student(); … students[34] = new Student(); • Do this in a loop

  16. Student[] students = new Student[3]; for(int i = 0; i < students.length; i++) { students[i] = new Student(); } Arrays of Objects Remember: The value of an object is a memory address! ? ? students ?

  17. Indexed Variables as Arguments • Indexed Variable scores[0], pressure[4], etc. • The same as using a regular variable public void printNum(int num) { System.out.println(num); } public void doStuff() { printNum(7); //prints 7 out to screen int[] scores = { 15, 37, 95 }; for(int i = 0; i < scores.length; i++) { printNum(scores[i]); //prints the ith score out to screen } }

  18. Array Assignment & Equality • Arrays are objects • The value of an array is a memory address • Using == to compare arrays two arrays a & b returns whether they point to the same memory address int[] a = {4, 5, 6}; int[] b = {4, 5, 6}; //a == b is false! //a.equals(b) is false as well!

  19. Comparing Arrays • To determine whether two arrays hold the same data, compare the two arrays element by element • Lab 7 • equals method

  20. What is the Output? int a = 7; int b = a; b = 8; System.out.println("A: " + a); System.out.println("B: " + b); //a is not changed by this Output A: 7 B: 8

  21. What is the Output? int[] a = {4, 5, 6}; int[] b = a; b[0] = 3; System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}"); System.out.println("b: {" + b[0] + ", " + b[1] + ", " + b[2] + "}"); This is like giving the array two names (a & b) //b holds same memory address as a //we’re changing both b & a! Output a: {3, 5, 6} b: {3, 5, 6}

  22. Array Assignment int[] a = {4, 5, 6}; int[] b = a; b[0] = 3; a 3 b

  23. Copying Arrays int[] a = {4, 5, 6}; int[] b = new int[a.length]; //create a new array b //copy a’s entries into b for(int i = 0; i < b.length; b++) { b[i] = a[i]; }

  24. What is the Output? public void changeNumber(int num) { num = 7; } public static void main(String[] args) { int a = 9; changeNumber(a); System.out.println("a = " + a); int num = 9; changeNumber(num); System.out.println("num = " +num); } Output a = 9 num = 9

  25. What is the Output? public void changeNumber(int num) { num = 7; } public static void main(String[] args) { int[] a = {4, 5, 6}; changeNumber(a[0]); System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}"); } Output a: {4, 5, 6}

  26. What is the Output? public void changeArray(int[] array) { array[0] = 7; } public static void main(String[] args) { int[] a = {4, 5, 6}; changeArray(a); System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}"); } Output a: {7, 5, 6}

  27. What is the Output? public void changeArray(int[] array) { array = new int[3]; array[0] = 7; array[1] = 5; array[2] = 6; } public static void main(String[] args) { int[] a = {4, 5, 6}; changeArray(a); System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}"); } Output a: {4, 5, 6}

  28. Graphical Example public void changeArray(int[] array) { array = new int[3]; array[0] = 7; array[1] = 5; array[2] = 6; } public static void main(String[] args) { int[] a = {4, 5, 6}; changeArray(a); System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}"); } a array Output a: {4, 5, 6}

  29. Programming Demo • Sorting (Selection Sort) • Section 7.4 in text

  30. Introduction to Sorting • Given an array of numbers, sort the numbers into ascending order • Input array: • Sorted array:

  31. Selection Sort

  32. Pseudocode • for i = 0 to array.length - 1 • Find the index s of the smallest element starting at index i • Swap elements i & s in the array s i

  33. Decomposition • Methods • int indexOfSmallest(int[] array, int startInd) • Return the index of the smallest element in array starting at startInd • void swap(int[] array, int a, int b) • Swap the elements at indices a & b in array

  34. Programming Demo • Programming

  35. Wednesday • Multi-Dimensional Arrays

More Related