Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110: Introduction to Programming Tyler Johnson Apr 27, 2009 MWF 11:00AM-12:15PM Sitterson 014.

Similar presentations


Presentation on theme: "COMP 110: Introduction to Programming Tyler Johnson Apr 27, 2009 MWF 11:00AM-12:15PM Sitterson 014."— Presentation transcript:

1 COMP 110: Introduction to Programming Tyler Johnson Apr 27, 2009 MWF 11:00AM-12:15PM Sitterson 014

2 COMP 110: Spring Announcements Final exam Monday May 4, 12 noon In this room Program 5 due today by 5pm

3 COMP 110: Spring Questions?

4 COMP 110: Spring Final Exam 20% of final grade Exam will be cumulative Lectures Readings from textbook Labs Programs Midterm No computers, notes, calculators etc. You will be allowed a 1-page cheat-sheet

5 COMP 110: Spring Today in COMP 110 Review for Final

6 COMP 110: Spring Objects/Arrays & Memory The value of an object is a memory address Student s1 = new Student("Jack Smith", ); Student s2 = new Student("Jack Smith", ); s1 == s2 is false! Arrays are objects!

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

8 COMP 110: Spring 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] + "}"); a: {3, 5, 6} b: {3, 5, 6} Output //b holds same memory address as a //were changing both b & a! This is like giving the array two names (a & b)

9 COMP 110: Spring Array Assignment int[] a = {4, 5, 6}; int[] b = a; b[0] = 3; 456 a b 3

10 COMP 110: Spring Copying Arrays int[] a = {4, 5, 6}; int[] b = new int[a.length]; //create a new array b //copy as entries into b for(int i = 0; i < b.length; b++) { b[i] = a[i]; }

11 COMP 110: Spring 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); } a = 9 num = 9 Output

12 COMP 110: Spring 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] + "}" ); } a: {4, 5, 6} Output

13 COMP 110: Spring 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] + "}" ); } a: {7, 5, 6} Output

14 COMP 110: Spring 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] + "}" ); } a: {4, 5, 6} Output

15 COMP 110: Spring 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] + "}"); } 456 a array 756 a: {4, 5, 6} Output

16 COMP 110: Spring Review Worksheet

17 COMP 110: Spring Course Evaluation


Download ppt "COMP 110: Introduction to Programming Tyler Johnson Apr 27, 2009 MWF 11:00AM-12:15PM Sitterson 014."

Similar presentations


Ads by Google