Upload
nathan-chavez
View
213
Download
0
Embed Size (px)
Citation preview
COMP 110:Introduction to Programming
Tyler JohnsonApr 27, 2009
MWF 11:00AM-12:15PMSitterson 014
COMP 110: Spring 20092
Announcements
Final exam Monday May 4, 12 noonIn this room
Program 5 due today by 5pm
COMP 110: Spring 20093
Questions?
COMP 110: Spring 20094
Final Exam
20% of final grade
Exam will be cumulativeLecturesReadings from textbookLabsProgramsMidterm
No computers, notes, calculators etc.
You will be allowed a 1-page cheat-sheet
COMP 110: Spring 20095
Today in COMP 110
Review for Final
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!
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
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//we’re changing both b & a!
This is like giving the array two names (a & b)
COMP 110: Spring 20099
Array Assignment
int[] a = {4, 5, 6};int[] b = a;b[0] = 3;
4 5 6a
b
3
COMP 110: Spring 200910
Copying Arrays
int[] a = {4, 5, 6};int[] b = new int[a.length]; //create a new array b
//copy a’s entries into bfor(int i = 0; i < b.length; b++) {
b[i] = a[i];}
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
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
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
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
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] + "}");
}
4 5 6a
array 7 5 6
a: {4, 5, 6}
Output
COMP 110: Spring 200916
Review Worksheet
COMP 110: Spring 200917
Course Evaluation