Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Programming TA Session 2

Similar presentations


Presentation on theme: "Advanced Programming TA Session 2"— Presentation transcript:

1 Advanced Programming TA Session 2
Advanced Programing TA Session, Sharif University of Technology TA Session 2

2 What are the properties of an Object?
State Behavior Identity Book.java class Book { String title; void printTitle() { System.out.println("I am: " + title); } Advanced Programing TA Session, Sharif University of Technology

3 Identity What is the output? class Book { String title;
void printTitle() { System.out.println("I am: " + title); } public static void main(String[] args) { Book book1 = new Book(), book2 = new Book(); book1.title = "TIJ"; book2.title = "TIJ"; if (book1 == book2) { System.out.println("equal"); Advanced Programing TA Session, Sharif University of Technology

4 Setting values in Arrays
Write a function to get a int[][] array and a number as input and set all cells of the array to that number. class TwoDimArrayUtil { public static void setArray(int[][] array, int value) { } public static void main(String[] args) { int [][] A; A = new int[3][]; A[0] = new int[2]; A[1] = new int[4]; A[2] = new int[1]; Advanced Programing TA Session, Sharif University of Technology

5 Setting values in Arrays - Solution
Write a function to get a int[][] array and a number as input and set all cells of the array to that number. class TwoDimArrayUtil { public static void setArray(int[][] array, int value) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { array[i][j] = value; } public static void main(String[] args) { int [][] A; A = new int[3][]; A[0] = new int[2]; A[1] = new int[4]; A[2] = new int[1]; Advanced Programing TA Session, Sharif University of Technology

6 Getting values in arrays
Write a function to convert an int[][] array to a String, like this: [[1, 2], [3, 4, 5], [6, 7]] class TwoDimArrayUtil { public static String convertArrayToString(int[][] array) { } public static void main(String[] args) { int [][] A; A = new int[3][]; A[0] = new int[] {1, 2}; A[1] = new int[] {3, 4, 5}; A[2] = new int[] {6, 7}; System.out.println(convertArrayToString(A)); Advanced Programing TA Session, Sharif University of Technology

7 Getting values in arrays - Solution
public static String convertArrayToString(int[][] array) { String val = "["; for (int i = 0; i < array.length; i++) { if (i != 0) val += ", "; val += "["; for (int j = 0; j < array[i].length; j++) { if (j != 0) val += ", "; val += array[i][j]; } val += "]"; return val; Advanced Programing TA Session, Sharif University of Technology

8 Changing arrays What is the output?
class TwoDimArrayUtil { static void changeSize(int[][] A, int r, int s) { A[r] = new int[s]; } public static void main(String[] args) { int [][] A; A = new int[3][]; A[0] = new int[] {1, 2}; A[1] = new int[] {3, 4, 5}; A[2] = new int[] {6, 7}; changeSize(A, 0, 10); System.out.println(A[0].length); Advanced Programing TA Session, Sharif University of Technology The output is 10, because arrays are passed by reference.

9 Swap int What is the output?
class Swap1 { static void swap(int x, int y) { int t = x; x = y; y = t; } public static void main(String[] args) { int x = 10, y = 20; swap(x, y); System.out.println(x + ", " + y); Advanced Programing TA Session, Sharif University of Technology The output is “10, 20” because primitive data types are passed by reference.

10 Swap Integer What is the output?
class Swap2 { static void swap(Integer x, Integer y) { Integer t = x; x = y; y = t; } public static void main(String[] args) { Integer x = 10, y = 20; swap(x, y); System.out.println(x + ", " + y); Advanced Programing TA Session, Sharif University of Technology Still, The output is “10, 20”, but for another reason: Because we are modifying the reference, not the content

11 Correct Swapper Advanced Programing TA Session, Sharif University of Technology


Download ppt "Advanced Programming TA Session 2"

Similar presentations


Ads by Google