02a: Software Engineering

ICS211, Fall 2012
Dr. Zach

(switch view)

How's things?

Today: How to write code

Software Development Steps

Waterfall model (1/2)

Waterfall model

Waterfall model (2/2)

Spiral Model

Spiral model

Iterative + Incremental Development (class of models)

Agile Programming

Which model is best?

Software Development Steps (again)

Focus is usually on the first 4, but code spends most of its lifetime in Maintenance phase

Exercise: Design for A02

In this class (1/2)

In this class (2/2)

Java Review

Methods

What is a class?

Method definition format

Important terms

Method calls

Examples from API

Shortcuts and Gotchas

In and out of a method

Example: pass-by-value

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

Example: Still pass-by-value

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

Parameters and local variables?

Example: Return values

public class Example {
  public static void main(String[] args) {
    String hello = "hello";
    hello = toTitleCase(hello);  //what if no assignment here?
    System.out.println(hello);
  }

  public static String toTitleCase(String str) {
    if (str.length() > 0) {  //XXX: doesn't support nulls
      return Character.toUpperCase(str.charAt(0)) + str.substring(1);
    }else {
      return ""; 
    }
  }
}

BTW: Strings are immutable: any method you call on a String that would change the String returns a changed copy instead.

Example: Nested method calls

System.out.println(mars.getAge());
String hello = "Hello";
System.out.println(hello.substring(hello.lastIndexOf('l')));

Don't do this too much though (hard to read/understand). Alternative:

String hello = "Hello";
int cutFrom = hello.lastIndexOf('l');
String end = hello.substring(cutFrom);
System.out.println(end);

For next time...