Exam 02 (Midterm Exam) this Wednesday. 

Comes with Lab 09. 

You are working on Homework 06 and 07. 

I am grading Homework 05 tonight, the other two Thursday. 

Chapters 13, 14 are planned for Thu. 

Topics: Recursion and Sorting and Searching.

Tomorrow: chapter 12, review. 

Today in lab: chapter 11.

Exam 03 is going to test: 

(a) inheritance, polymorphism, interfaces, abstract classes 
(b) constructors, constructor chaining, shadowing of variables
(c) dynamic method lookup (overriding) basic modeling (stages)
(d) capturing and processing mouse and keyboard events 
(e) sorting with Comparators and Comparable interfaces
(f) Exceptions, GUI (labels, buttons, text fields etc.)
(g) capturing and processing timer/JButton events (BigBang) 

  mahazuga: Mary Ann
* serepate: Serena
* hgarciah: Henri
* balbakr: Bakr
* ruifan: Rui
* mzelenin: Matthew
* jnp2: Jay
* zeyang: Zejun
* zhang486: Jingyun
  yiwecao: Yiwei
* rthammon: Ryan
* wang686: Jiaxing
* dweissma: Andrew
* kevcao: Kevin
  ssalmero: Salmeron, Santiago (TA)
  luo23: Yawen
* runxzhao: Runxia
* dgerman: German, Dan-Adrian (Primary Instructor)
  creba: Chris

import java.util.ArrayList; 

public class Sequence {
  private ArrayList<Integer> values; 
  public Sequence() {
    this.values = new ArrayList<Integer>();
  }
  public Sequence(int[] values){ 
    this(); // ha, ha!
    for (int v : values)
      this.values.add( v);
  } 
  public void add(int n) {
    this.values.add(n);
  }
  public String toString() {
    return this.values.toString();
  }
  public static void main(String[] args) {    
    Sequence w = new Sequence( new int[] { 4, 5, 2, 3, 1, 7, 6 } ); 
              // notice how easy it is to initialize a Sequence now
    System.out.println( w ); 
    
    Sequence a = new Sequence( new int[] { 1, 4, 9, 16 } );
    Sequence b = new Sequence( new int[] { 4, 7, 9, 9, 11} );
    
    Sequence c = b.merge(a); 
    
    System.out.println( "merge(" + a + ", " + b + ") = " + c ); 
    
  }
  public int size() {
    return this.values.size();  
  }
  public Integer get(int index) {
    return this.values.get(index); 
  }
  public Sequence merge(Sequence other) {
    Sequence result = new Sequence();  
    if (this.size() == 0) { // host is empty 
      if (other.size() == 0) { // the other sequence also empty
        
      } else { // the other sequence not empty
        for (int i = 0; i < other.size(); i++) 
          result.add(other.get(i)); 
      }
    } else { // host is not empty
      if (other.size() == 0) { // the other sequence is empty 
        for (int i = 0; i < this.size(); i++) 
          result.add(this.get(i)); 
      } else {
        for (int i = 0, j = 0; i < this.size() || j < other.size();   ) {
          if (i < this.size() && j < other.size()) {
            if (this.get(i) < other.get(j)) {
              result.add(this.get(i++));  
            } else {
              result.add(other.get(j++));            
            }
          } else if (i >= this.size()) {
            result.add(other.get(j++));
          } else {
            result.add(this.get(i++));
          }
        }
      }
    }
    return result; 
  }
}

This is the answer to E7.24

import java.util.ArrayList; 

public class Sequence {
  
  public Sequence longestPrefix() {
    Sequence result = new Sequence();
    for (int i = 0; i < this.size();    ) {
      if (result.size() == 0) 
        result.add(this.get(i++));
      else if (result.get(result.size() - 1) <= this.get(i)) 
        result.add(this.get(i++));
      else break; 
    }
    return result; 
  }
  
  private ArrayList<Integer> values; 
  public Sequence() {
    this.values = new ArrayList<Integer>();
  }
  public Sequence(int[] values){ 
    this(); // ha, ha!
    for (int v : values)
      this.values.add( v);
  } 
  public void add(int n) {
    this.values.add(n);
  }
  public String toString() {
    return this.values.toString();
  }
  public static void main(String[] args) {    
    Sequence w = new Sequence( new int[] { 4, 5, 2, 3, 1, 7, 6 } ); 
              // notice how easy it is to initialize a Sequence now
    System.out.println( w ); 
    
    Sequence a = new Sequence( new int[] { 1, 4, 9, 16 } );
    Sequence b = new Sequence( new int[] { 4, 7, 9, 9, 11} );
    
    Sequence c = b.merge(a); 
    
    System.out.println( "merge(" + a + ", " + b + ") = " + c ); 
    
    Sequence d = new Sequence();
    System.out.println( "longest(" + d + ") = " + d.longestPrefix() ); 
    
    Sequence e = new Sequence(new int[] {6, 5, 4, 3, 2, 1});
    System.out.println( "longest(" + e + ") = " + e.longestPrefix() ); 
    
    Sequence f = new Sequence(new int[] {4, 5, 6, 7, 1, 2, 3});
    System.out.println( "longest(" + f + ") = " + f.longestPrefix() ); 
  
    Sequence g = new Sequence(new int[] {1, 2, 2, 3, 4, 5, 5, 6, 7});
    System.out.println( "longest(" + g + ") = " + g.longestPrefix() ); 

  }
  public int size() {
    return this.values.size();  
  }
  public Integer get(int index) {
    return this.values.get(index); 
  }
  public Sequence merge(Sequence other) {
    Sequence result = new Sequence();  
    if (this.size() == 0) { // host is empty 
      if (other.size() == 0) { // the other sequence also empty
        
      } else { // the other sequence not empty
        for (int i = 0; i < other.size(); i++) 
          result.add(other.get(i)); 
      }
    } else { // host is not empty
      if (other.size() == 0) { // the other sequence is empty 
        for (int i = 0; i < this.size(); i++) 
          result.add(this.get(i)); 
      } else {
        for (int i = 0, j = 0; i < this.size() || j < other.size();   ) {
          if (i < this.size() && j < other.size()) {
            if (this.get(i) < other.get(j)) {
              result.add(this.get(i++));  
            } else {
              result.add(other.get(j++));            
            }
          } else if (i >= this.size()) {
            result.add(other.get(j++));
          } else {
            result.add(this.get(i++));
          }
        }
      }
    }
    return result; 
  }
}

Now I have longest. 

import java.util.ArrayList; 

public class Sequence {
  
  public Sequence longestPrefix() {
    Sequence result = new Sequence();
    for (int i = 0; i < this.size();    ) {
      if (result.size() == 0) 
        result.add(this.get(i++));
      else if (result.get(result.size() - 1) <= this.get(i)) 
        result.add(this.get(i++));
      else break; 
    }
    return result; 
  }
  
  private ArrayList<Integer> values; 
  public Sequence() {
    this.values = new ArrayList<Integer>();
  }
  public Sequence(int[] values){ 
    this(); // ha, ha!
    for (int v : values)
      this.values.add( v);
  } 
  public void add(int n) {
    this.values.add(n);
  }
  public String toString() {
    return this.values.toString();
  }
  public static void main(String[] args) {    
    Sequence w = new Sequence( new int[] { 4, 5, 2, 3, 1, 7, 6 } ); 
    // notice how easy it is to initialize a Sequence now
    System.out.println( w ); 
    
    Sequence a = new Sequence( new int[] { 1, 4, 9, 16 } );
    Sequence b = new Sequence( new int[] { 4, 7, 9, 9, 11} );
    
    Sequence c = b.merge(a); 
    
    System.out.println( "merge(" + a + ", " + b + ") = " + c ); 
    
    Sequence d = new Sequence();
    System.out.println( "longest(" + d + ") = " + d.longestPrefix() ); 
    
    Sequence e = new Sequence(new int[] {6, 5, 4, 3, 2, 1});
    System.out.println( "longest(" + e + ") = " + e.longestPrefix() ); 
    
    Sequence f = new Sequence(new int[] {4, 5, 6, 7, 1, 2, 3});
    System.out.println( "longest(" + f + ") = " + f.longestPrefix() ); 
    
    Sequence g = new Sequence(new int[] {1, 2, 2, 3, 4, 5, 5, 6, 7});
    System.out.println( "longest(" + g + ") = " + g.longestPrefix() ); 
    
    Sequence h = new Sequence( new int[] { 8, 2, 3, 1 } ); 
    Sequence i = new Sequence( new int[] { 6, 6, 9, 2, 3, 11 } ); 
    
    System.out.println( h.longestPrefix().merge(i.longestPrefix()) ); 
    // I expect [6, 6, 8, 9]
    
  }
  public int size() {
    return this.values.size();  
  }
  public Integer get(int index) {
    return this.values.get(index); 
  }
  public Sequence merge(Sequence other) {
    Sequence result = new Sequence();  
    if (this.size() == 0) { // host is empty 
      if (other.size() == 0) { // the other sequence also empty
        
      } else { // the other sequence not empty
        for (int i = 0; i < other.size(); i++) 
          result.add(other.get(i)); 
      }
    } else { // host is not empty
      if (other.size() == 0) { // the other sequence is empty 
        for (int i = 0; i < this.size(); i++) 
          result.add(this.get(i)); 
      } else {
        for (int i = 0, j = 0; i < this.size() || j < other.size();   ) {
          if (i < this.size() && j < other.size()) {
            if (this.get(i) < other.get(j)) {
              result.add(this.get(i++));  
            } else {
              result.add(other.get(j++));            
            }
          } else if (i >= this.size()) {
            result.add(other.get(j++));
          } else {
            result.add(this.get(i++));
          }
        }
      }
    }
    return result; 
  }
}

So I want rest. 

import java.util.ArrayList; 

public class Sequence {
  
  public Sequence longestPrefix() {
    Sequence result = new Sequence();
    for (int i = 0; i < this.size();    ) {
      if (result.size() == 0) 
        result.add(this.get(i++));
      else if (result.get(result.size() - 1) <= this.get(i)) 
        result.add(this.get(i++));
      else break; 
    }
    return result; 
  }

  public Sequence rest() {
    Sequence result = new Sequence();
    int i = 0; 
    for (     ; i < this.size();    ) {
      if (result.size() == 0) 
        result.add(this.get(i++));
      else if (result.get(result.size() - 1) <= this.get(i)) 
        result.add(this.get(i++));
      else break; 
    }
    result = new Sequence(); 
    for (int j = i; j < this.size(); j++) 
      result.add(this.get(j));
    return result; 
  }
  
  private ArrayList<Integer> values; 
  public Sequence() {
    this.values = new ArrayList<Integer>();
  }
  public Sequence(int[] values){ 
    this(); // ha, ha!
    for (int v : values)
      this.values.add( v);
  } 
  public void add(int n) {
    this.values.add(n);
  }
  public String toString() {
    return this.values.toString();
  }
  public static void main(String[] args) {    
    Sequence w = new Sequence( new int[] { 4, 5, 2, 3, 1, 7, 6 } ); 
    // notice how easy it is to initialize a Sequence now
    System.out.println( w ); 
    
    Sequence a = new Sequence( new int[] { 1, 4, 9, 16 } );
    Sequence b = new Sequence( new int[] { 4, 7, 9, 9, 11} );
    
    Sequence c = b.merge(a); 
    
    System.out.println( "merge(" + a + ", " + b + ") = " + c ); 
    
    Sequence d = new Sequence();
    System.out.println( "longest(" + d + ") = " + d.longestPrefix() ); 
    
    Sequence e = new Sequence(new int[] {6, 5, 4, 3, 2, 1});
    System.out.println( "longest(" + e + ") = " + e.longestPrefix() ); 
    
    Sequence f = new Sequence(new int[] {4, 5, 6, 7, 1, 2, 3});
    System.out.println( "longest(" + f + ") = " + f.longestPrefix() ); 
    
    Sequence g = new Sequence(new int[] {1, 2, 2, 3, 4, 5, 5, 6, 7});
    System.out.println( "longest(" + g + ") = " + g.longestPrefix() ); 
    
    Sequence h = new Sequence( new int[] { 8, 2, 3, 1 } ); 
    Sequence i = new Sequence( new int[] { 6, 6, 9, 2, 3, 11 } ); 
    
    System.out.println( h.longestPrefix().merge(i.longestPrefix()) ); 
    // I expect [6, 6, 8, 9]

    System.out.println(h.rest()); // [2, 3, 1]
    System.out.println(i.rest()); // [2, 3, 11]
    
  }
  public int size() {
    return this.values.size();  
  }
  public Integer get(int index) {
    return this.values.get(index); 
  }
  public Sequence merge(Sequence other) {
    Sequence result = new Sequence();  
    if (this.size() == 0) { // host is empty 
      if (other.size() == 0) { // the other sequence also empty
        
      } else { // the other sequence not empty
        for (int i = 0; i < other.size(); i++) 
          result.add(other.get(i)); 
      }
    } else { // host is not empty
      if (other.size() == 0) { // the other sequence is empty 
        for (int i = 0; i < this.size(); i++) 
          result.add(this.get(i)); 
      } else {
        for (int i = 0, j = 0; i < this.size() || j < other.size();   ) {
          if (i < this.size() && j < other.size()) {
            if (this.get(i) < other.get(j)) {
              result.add(this.get(i++));  
            } else {
              result.add(other.get(j++));            
            }
          } else if (i >= this.size()) {
            result.add(other.get(j++));
          } else {
            result.add(this.get(i++));
          }
        }
      }
    }
    return result; 
  }
}

I will see you in lab for:

(a) start with jUnit
(b) files and exceptions
(c) merge sort from longestPrefix, merge and rest

--