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 20092 Announcements Final exam Monday May 4, 12 noon In this room Program 5 due today by 5pm

3 COMP 110: Spring 20093 Questions?

4 COMP 110: Spring 20094 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 20095 Today in COMP 110 Review for Final

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

7 COMP 110: Spring 20097 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 20098 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 20099 Array Assignment int[] a = {4, 5, 6}; int[] b = a; b[0] = 3; 456 a b 3

10 COMP 110: Spring 200910 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 200911 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 200912 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 200913 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 200914 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 200915 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 200916 Review Worksheet

17 COMP 110: Spring 200917 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