350 likes | 519 Views
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.
E N D
COMP 110:Introduction to Programming Tyler Johnson Mar 30, 2009 MWF 11:00AM-12:15PM Sitterson 014
Announcements • Program 3 has been graded
Today in COMP 110 • Review from last time • Arrays in Classes & Methods • Intro to Sorting • Programming Demo
Arrays • Review from last time
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!
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
Arrays The array itself is referred to by a name “scores” or “vector” (in our examples) Indices the array scores scores[3]
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)
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;
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
Arrays in Classes & Methods • Section 7.2 in text
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; }
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
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
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 ?
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 } }
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!
Comparing Arrays • To determine whether two arrays hold the same data, compare the two arrays element by element • Lab 7 • equals method
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
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}
Array Assignment int[] a = {4, 5, 6}; int[] b = a; b[0] = 3; a 3 b
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]; }
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
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}
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}
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}
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}
Programming Demo • Sorting (Selection Sort) • Section 7.4 in text
Introduction to Sorting • Given an array of numbers, sort the numbers into ascending order • Input array: • Sorted array:
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
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
Programming Demo • Programming
Wednesday • Multi-Dimensional Arrays