04b: Recap

ICS211, Spring 2013
Dr. Zach

(switch view)

Status check

Parallels

Comparison of software development process and algorithm/code distinction

Discussion 01: Algorithm Selection Criteria

Discussion 01: Conclusions

Java Review

Odds and Ends

Arrays: Useful Tricks

int[] nums = {1, 2, 3, 4};
nums = {2, 4, 6, 8}; // NO!  Can only initialize this way
nums = new int[]{2, 4, 6, 8};  // works

java.util.Arrays has many useful methods, including copyOf, equals, and toString.

String asStr = java.util.Arrays.toString(nums);
System.out.println(asStr);  //prints: [2, 4, 6, 8]
System.out.println(java.util.Arrays.toString(new int[]{5, 8, 13, 21}));

Some people also like System.arraycopy method. (Very versatile. Check out the API.)

Arrays and the Heap (1/2)

int[] evens = {2, 4, 6}; //implicitly invoking new
int[] odds = {1, 3, 5};
odds = evens;
odds[1] = 7;
System.out.println(evens[1]);

What does this print?

Arrays and the Heap (2/2)

public class Example {
  public static void main(String[] args) {
    int[] nums = {1, 3, 5, 8};
    int a = 1;
    int b = 2;
    swap(nums, a, b);
    System.out.println("nums[a] = " + nums[a] + ", nums[b] = " + nums[b]);
  }

  public static void swap(int[] array, int left, int right) {
    int temp = array[left];
    array[left] = array[right];
    array[right] = temp;
  }
}    

Wrapper classes

Wrapper classes: Autoboxing example

//old manual way
int num = 5;
Integer number = new Integer(num);  //wrapping up value
//...could now pass number to some method that only takes an object
int x = number.intValue()           //unwrapping 

//now, with autoboxing
Integer five = num;   //autobox: assign int to Integer and it worked!
Integer f = 5;        //same
int n = five;         //unboxing: assigned Integer value to int

java.util.ArrayList

  java.util.ArrayList<Integer> nums = new java.util.ArrayList<Integer>();
  nums.add(3);   //autoboxing at work
  nums.add(6);
  nums.add(1, 4);
  int x = nums.get(0);  //x == 3
  nums.set(2, 5);  //changes 6 to 5
  System.out.println(nums);  //prints: [3, 4, 5]
  System.out.println("size=" + nums.size());  //prints: size=3

Footnote: Later in the term we'll learn why it's better to use List<Integer> nums = new ArrayList<Integer>(); here and to write methods to take and return List rather than ArrayList.

Exam 1

What to expect

What we've learned so far...

"Let me explain... No, there is too much. Let me sum up."

      --Inigo Montoya, The Princess Bride

What you should know for the exam... Java

What you should know for the exam... concepts

What you should know for the exam... processes

The exam itself

Looking ahead...

Java Review

Objects

Classes

public class Simple {
}
public static void main(String[] args) {
  Simple s = new Simple();
}

Like arrays, objects are in the heap.

Object state = Instance variables

public class Cat {
  String name;
  int lives;
}

Constructor

public class Cat {
  String name;
  int lives;
  
  public Cat(String name, int lives) {
    this.name = name;
    this.lives = (lives < 1) ? 1 : lives;
  }
}

this

public class Cat {
  String name;
  int lives;

  //preferred version  
  public Cat(String name, int lives) {
    this.name = name;
    this.lives = (lives < 1) ? 1 : lives;
  }

  //alt: works (but is not as good)
  public Cat(String n, int l) {
    name = n;
    lives = (l < 1) ? 1 : l;  //Tip: NEVER use l as a variable name!
  }

  //alt: broken!
  public Cat(String name, int lives) {
    name = name;
    lives = (lives < 1) ? 1 : lives;
  }
}

Overloading constructors

public class Cat {
  String name;
  int lives;

  public Cat(String name, int lives) {
    this.name = name;
    this.lives = (lives < 1) ? 1 : lives;
  }
  
  public Cat(String name) {
    this(name, 9);  //invoke other constructor rather than repeat code here
  }
}

Object Behavior = Instance Methods

public class Cat {
  String name;
  int lives;

  //(constructors omitted)
  
  public void die() {
    this.lives--;
  }
  
  public boolean isReallyDead() {
    return lives <= 0;
  }
}

Encapsulation: Good practice

public class Cat {
  private String name;
  private int lives;

  public Cat(String name, int lives) {
    this.name = name;
    this.lives = (lives < 1) ? 1 : lives;
  }
  
  public String getName() {
    return this.name;
  }

  public void setName(String name) {  //can rename a cat?
    this.name = name;  
  }

  public int getLives() {
    return this.lives;
  }  
}

Summary

Before next time...