Exam tomorrow. 

One problem randomly selected from the posted list. 

Request: E7.12

public class Sequence {
  private int[] values; 
  public Sequence(int size) { this.values = new int[size]; }
  public void set(int i, int n) { this.values[i] = n; }
  public int get(int i) { return this.values[i]; }
  public int size() { return values.length; } 
  public String toString() {
    return java.util.Arrays.toString( this.values );  
  }
  public boolean sameValues(Sequence other) {

    return false; 
  }
  public static void main(String[] args) {
    Sequence a = new Sequence(6);
    System.out.println( a ); 
    a.set(2, -3); 
    System.out.println( a );     
    Sequence one, two; 
    one = new Sequence(9); 
    one.set(0,  1); 
    one.set(1,  4); 
    one.set(2,  9); 
    one.set(3, 16); 
    one.set(4,  9); 
    one.set(5,  7); 
    one.set(6,  4); 
    one.set(7,  9); 
    one.set(8, 11);
    System.out.println( one );     
    two = new Sequence(7); 
    two.set(0, 11); 
    two.set(1, 11); 
    two.set(2,  7); 
    two.set(3,  9); 
    two.set(4, 16); 
    two.set(5,  4); 
    two.set(6,  1); 
    System.out.println( two );     
  }
}

It runs as follows: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Sequence
[0, 0, 0, 0, 0, 0]
[0, 0, -3, 0, 0, 0]
[1, 4, 9, 16, 9, 7, 4, 9, 11]
[11, 11, 7, 9, 16, 4, 1]



Garrett Ankney
Khalea Berry-Simms
Daniel Clark
Keiland Cooper
Ryan Cotter
Kang Jie Gan
Stuart Hay
Kefei He
Misato Hiraga
Tao Hu
Elise Jing
John Kaefer
Jinsu Kim 
Katherine Kowala -- not here 
Li Liu -- not here 
Noah Matlock
Nicholas Navarro
Rafael Nieves
Taylor O'dell
Patrick Parker -- not here 
Jillian Pena
Jiang Qian
Christopher Reba -- not here 
Daniel Ruiz -- not here (for real)
Luis Salazar
Santiago Salmeron
Bihan Shen 
Justin Stamets
Iris Wang
Yibo Wang

public class Sequence {
  private int[] values; 
  public Sequence(int size) { this.values = new int[size]; }
  public void set(int i, int n) { this.values[i] = n; }
  public int get(int i) { return this.values[i]; }
  public int size() { return values.length; } 
  public String toString() {
    return java.util.Arrays.toString( this.values );  
  }
  public boolean sameValues(Sequence other) {
    return this.subsetOf(other) && other.subsetOf(this); 
  }
  public boolean subsetOf(Sequence other) {
    for (int value : this.values)
      if (! other.contains(value))
        return false; 
    return true; 
  }
  public boolean contains(int number) {
    for (int value : this.values) 
      if (value == number)
        return true;
    return false; 
  }
  public static void main(String[] args) {
    Sequence a = new Sequence(6);
    System.out.println( a ); 
    a.set(2, -3); 
    System.out.println( a );     
    Sequence one, two; 
    one = new Sequence(9); 
    one.set(0,  1); 
    one.set(1,  4); 
    one.set(2,  9); 
    one.set(3, 16); 
    one.set(4,  9); 
    one.set(5,  7); 
    one.set(6,  4); 
    one.set(7,  9); 
    one.set(8, 11);
    System.out.println( one );     
    two = new Sequence(7); 
    two.set(0, 11); 
    two.set(1, 11); 
    two.set(2,  7); 
    two.set(3,  9); 
    two.set(4, 16); 
    two.set(5,  4); 
    two.set(6,  1); 
    System.out.println( two );    
    System.out.println( two.contains(8) ); // no 
    System.out.println( two.contains(9) ); // yes
    System.out.println( two.sameValues(one) ); // yes
    System.out.println( one.sameValues(two) ); // yes
    System.out.println( one.sameValues(a) ); // no
        
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Sequence
[0, 0, 0, 0, 0, 0]
[0, 0, -3, 0, 0, 0]
[1, 4, 9, 16, 9, 7, 4, 9, 11]
[11, 11, 7, 9, 16, 4, 1]
false
true
true
true
false


That's it.

--

P7.6


import java.util.Scanner; 

public class Magic {
  public static void main(String[] args) {
    System.out.print("Size: "); 
    Scanner in = new Scanner( System.in ); 
    int n = Integer.parseInt( in.nextLine() ); 
    int size = 2 * n + 1; 
    int[][] magic = new int[size][size]; 
    System.out.println( magic ); 
    Magic.show( magic ); 
  }
  public static void show(int[][] a) {
    for (int line = 0; line < a.length; line++) {
      System.out.println( java.util.Arrays.toString ( a[line] ) ); 
    }
  }
}

It runs like this: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Magic
Size:  [DrJava Input Box]
[[I@7dec8faf
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
> run Magic
Size:  [DrJava Input Box]
[[I@493877a4
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
> run Magic
Size:  [DrJava Input Box]
[[I@7a15fdf9
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
>

import java.util.Scanner; 

public class Magic {
  public static void main(String[] args) {
    System.out.print("Size: "); 
    Scanner in = new Scanner( System.in ); 
    int n = Integer.parseInt( in.nextLine() ); 
    int size = 2 * n + 1; 
    int[][] magic = new int[size][size]; 
    System.out.println( magic ); 
    Magic.show( magic ); 
    System.out.println("----------------------"); 
    int k = 1; // next value to be placed
    int i = size-1; // bottom line  
    int j = size/2; // middle column
    while (k <= size*size) { // size squared is the limit 
      magic[i][j] = k;
      if (magic[(i+1)%size][(j+1)%size] == 0) {
        i = (i+1)%size;
        j = (j+1)%size; 
      } else {
        i = i - 1;  
      }
      k = k + 1; 
      Magic.show( magic ); 
      System.out.println("----------------------"); 
    }
  }
  public static void show(int[][] a) {
    for (int line = 0; line < a.length; line++) {
      System.out.println( java.util.Arrays.toString ( a[line] ) ); 
    }
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Magic
Size:  [DrJava Input Box]
[[I@7dec8faf
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
----------------------
[0, 0, 0]
[0, 0, 0]
[0, 1, 0]
----------------------
[0, 0, 2]
[0, 0, 0]
[0, 1, 0]
----------------------
[0, 0, 2]
[3, 0, 0]
[0, 1, 0]
----------------------
[4, 0, 2]
[3, 0, 0]
[0, 1, 0]
----------------------
[4, 0, 2]
[3, 5, 0]
[0, 1, 0]
----------------------
[4, 0, 2]
[3, 5, 0]
[0, 1, 6]
----------------------
[4, 0, 2]
[3, 5, 7]
[0, 1, 6]
----------------------
[4, 0, 2]
[3, 5, 7]
[8, 1, 6]
----------------------
[4, 9, 2]
[3, 5, 7]
[8, 1, 6]
----------------------
> run Magic
Size:  [DrJava Input Box]
[[I@7c2b7a8d
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
----------------------
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 1, 0, 0]
----------------------
[0, 0, 0, 2, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 1, 0, 0]
----------------------
[0, 0, 0, 2, 0]
[0, 0, 0, 0, 3]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 1, 0, 0]
----------------------
[0, 0, 0, 2, 0]
[0, 0, 0, 0, 3]
[4, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 1, 0, 0]
----------------------
[0, 0, 0, 2, 0]
[0, 0, 0, 0, 3]
[4, 0, 0, 0, 0]
[0, 5, 0, 0, 0]
[0, 0, 1, 0, 0]
----------------------
[0, 0, 0, 2, 0]
[0, 0, 0, 0, 3]
[4, 6, 0, 0, 0]
[0, 5, 0, 0, 0]
[0, 0, 1, 0, 0]
----------------------
[0, 0, 0, 2, 0]
[0, 0, 0, 0, 3]
[4, 6, 0, 0, 0]
[0, 5, 7, 0, 0]
[0, 0, 1, 0, 0]
----------------------
[0, 0, 0, 2, 0]
[0, 0, 0, 0, 3]
[4, 6, 0, 0, 0]
[0, 5, 7, 0, 0]
[0, 0, 1, 8, 0]
----------------------
[0, 0, 0, 2, 9]
[0, 0, 0, 0, 3]
[4, 6, 0, 0, 0]
[0, 5, 7, 0, 0]
[0, 0, 1, 8, 0]
----------------------
[0, 0, 0, 2, 9]
[10, 0, 0, 0, 3]
[4, 6, 0, 0, 0]
[0, 5, 7, 0, 0]
[0, 0, 1, 8, 0]
----------------------
[11, 0, 0, 2, 9]
[10, 0, 0, 0, 3]
[4, 6, 0, 0, 0]
[0, 5, 7, 0, 0]
[0, 0, 1, 8, 0]
----------------------
[11, 0, 0, 2, 9]
[10, 12, 0, 0, 3]
[4, 6, 0, 0, 0]
[0, 5, 7, 0, 0]
[0, 0, 1, 8, 0]
----------------------
[11, 0, 0, 2, 9]
[10, 12, 0, 0, 3]
[4, 6, 13, 0, 0]
[0, 5, 7, 0, 0]
[0, 0, 1, 8, 0]
----------------------
[11, 0, 0, 2, 9]
[10, 12, 0, 0, 3]
[4, 6, 13, 0, 0]
[0, 5, 7, 14, 0]
[0, 0, 1, 8, 0]
----------------------
[11, 0, 0, 2, 9]
[10, 12, 0, 0, 3]
[4, 6, 13, 0, 0]
[0, 5, 7, 14, 0]
[0, 0, 1, 8, 15]
----------------------
[11, 0, 0, 2, 9]
[10, 12, 0, 0, 3]
[4, 6, 13, 0, 0]
[0, 5, 7, 14, 16]
[0, 0, 1, 8, 15]
----------------------
[11, 0, 0, 2, 9]
[10, 12, 0, 0, 3]
[4, 6, 13, 0, 0]
[0, 5, 7, 14, 16]
[17, 0, 1, 8, 15]
----------------------
[11, 18, 0, 2, 9]
[10, 12, 0, 0, 3]
[4, 6, 13, 0, 0]
[0, 5, 7, 14, 16]
[17, 0, 1, 8, 15]
----------------------
[11, 18, 0, 2, 9]
[10, 12, 19, 0, 3]
[4, 6, 13, 0, 0]
[0, 5, 7, 14, 16]
[17, 0, 1, 8, 15]
----------------------
[11, 18, 0, 2, 9]
[10, 12, 19, 0, 3]
[4, 6, 13, 20, 0]
[0, 5, 7, 14, 16]
[17, 0, 1, 8, 15]
----------------------
[11, 18, 0, 2, 9]
[10, 12, 19, 21, 3]
[4, 6, 13, 20, 0]
[0, 5, 7, 14, 16]
[17, 0, 1, 8, 15]
----------------------
[11, 18, 0, 2, 9]
[10, 12, 19, 21, 3]
[4, 6, 13, 20, 22]
[0, 5, 7, 14, 16]
[17, 0, 1, 8, 15]
----------------------
[11, 18, 0, 2, 9]
[10, 12, 19, 21, 3]
[4, 6, 13, 20, 22]
[23, 5, 7, 14, 16]
[17, 0, 1, 8, 15]
----------------------
[11, 18, 0, 2, 9]
[10, 12, 19, 21, 3]
[4, 6, 13, 20, 22]
[23, 5, 7, 14, 16]
[17, 24, 1, 8, 15]
----------------------
[11, 18, 25, 2, 9]
[10, 12, 19, 21, 3]
[4, 6, 13, 20, 22]
[23, 5, 7, 14, 16]
[17, 24, 1, 8, 15]
----------------------


We should try to do it in lab with ArrayLists instead. 

--

Discussion during office hours

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> String a = "tom", b = "Tom";  
> a
"tom"
> b
"Tom"
> 't' + 0
116
> 'T' + 0
84
> 'a' - 'A' == 116 - 84
true
> 116 - 84
32
> a.compareTo(b)
32
> b.compareTo(a)
-32
> "tim".compareTo("tom") // rdnieves: positive 4-5 dgerman: negative 5-6 
-6
> "raphael".compareTo("rafael") // positive means raphael comes after rafael 
10
> "rafael".compareTo("raphael") // negative means rafael needs to stay in front 
-10