Problem 1: 

Create an ArrayList<Integer> with values like [3, 1, 5, 2, 8, 3]

Write a loop that checks and removes the even numbers. Do it both ways. 

Problem 2: 

What is a reflexive relation? 

https://en.wikipedia.org/wiki/Reflexive_relation

What makes a relation symmetric? 

https://en.wikipedia.org/wiki/Symmetric_relation

What makes a relation transitive? 

https://en.wikipedia.org/wiki/Transitive_relation


 { (1, 1), (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 4) }


     1 2 3 4
   1 x x    
   2 x x x  
   3   x x x
   4       x

By the diagonal: reflexive 

Make it symmetric: 

  (a) either by removing (3, 4)

  (b) or by adding (4, 3)

  (c) or by many other ways

Transitivity: see the question marks. 

     1 2 3 4
   1 x x ?   (1, 2) causes it 
   2 x x x ? (2, 3) causes it 
   3 ? x x x (3, 2) causes it
   4       x

http://staff.scem.uws.edu.au/cgi-bin/cgiwrap/zhuhan/dmath/dm_readall.cgi?page=18&part=2

https://www.csee.umbc.edu/~stephens/203/PDF/10-2.pdf

https://www.ida.liu.se/opendsa/OpenDSA/Books/TDDC76F16/html/BinaryTreeFullThm.html#

https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/

--

* 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 One {
  public static void main(String[] args) {
    ArrayList<Integer> a = new ArrayList<Integer>();  
    a.add(3); 
    a.add(1); 
    a.add(2); 
    a.add(8); 
    a.add(1); 
    a.add(5); 
    a.add(6); 
    System.out.println( a ); // [3, 1, 2, 8, 1, 5, 6]
    
    a.remove( new Integer(1) ); 

    System.out.println( a ); // [3, 2, 8, 1, 5, 6]
    
    ArrayList<Integer> b = new ArrayList<Integer>(); 
    
    for (Integer i : a)
      if (i % 2 != 0)
        b.add(i); 

    System.out.println( b ); // [3, 1, 5]

    for (int i = 0; i < a.size();     ) {
      Integer value = a.get(i); 
      if (value % 2 == 0) 
        a.remove(new Integer( value )); 
      else 
        i++; 
    }
                             // [3, 2, 8, 1, 5, 6]
    System.out.println( a ); // [3, 1, 5]
  }
}

import java.util.ArrayList;

public class One {
  public static void main(String[] args) {
    ArrayList<Integer> a = new ArrayList<Integer>();  
    a.add(3); 
    a.add(1); 
    a.add(2); 
    a.add(8); 
    a.add(1); 
    a.add(5); 
    a.add(6); 
    System.out.println( a ); // [3, 1, 2, 8, 1, 5, 6]
    
    a.remove( new Integer(1) ); 

    System.out.println( a ); // [3, 2, 8, 1, 5, 6]
    
    ArrayList<Integer> b = new ArrayList<Integer>(); 
    
    for (Integer i : a)
      if (i % 2 != 0)
        b.add(i); 

    System.out.println( b ); // [3, 1, 5]

    for (int i = 0; i < a.size(); i++) {
      Integer value = a.get(i); 
      if (value % 2 == 0) 
        a.remove(new Integer( value )); 
    }
                             // [3, 2, 8, 1, 5, 6]
    System.out.println( a ); // [3, 8, 1, 5]
  }
}

{ (1, 1),(1, 2),(2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 4) }
   ----   ----

Not transitive because (1, 2) and (2, 3) demand (1, 3) which is missing. 

--

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 

class BigBang extends JComponent implements KeyListener, ActionListener, MouseListener {
  Timer timer; 
  World world; 
  BigBang(int delay, World world) {
    timer = new Timer(delay, this); 
    this.world = world;
  } 
  public void start() {
    timer.start();  
  }
  BigBang(World world) {
    this(1000, world);  
  }
  public void paintComponent(Graphics g) {
    world.draw(g);  
  }
  public void actionPerformed(ActionEvent e) {
    world.update(); 
    if (world.hasEnded())
      timer.stop(); 
    this.repaint(); 
  }
  public void keyPressed(KeyEvent e) { 
    world.keyPressed(e); 
    this.repaint(); 
  } 
  public void keyTyped(KeyEvent e) { } 
  public void keyReleased(KeyEvent e) { } 

  public void mousePressed(MouseEvent e) { 
    world.mousePressed(e); 
    this.repaint(); 
  } 
  public void mouseReleased(MouseEvent e) { } 
  public void mouseClicked(MouseEvent e) { } 
  public void mouseEntered(MouseEvent e) { } 
  public void mouseExited(MouseEvent e) { } 

}

--

import java.awt.Graphics;
  
public class Circle {
  private int x, y, radius;
  public Circle(int x, int y, int r) {
    this.x = x; 
    this.y = y; 
    this.radius = r; 
  }
  public Circle(Circle other) {
    this.x = other.x; 
    this.y = other.y;
    this.radius = other.radius; 
  }
  public void draw(Graphics g, int x, int y) {
    this.x = x;
    this.y = y; 
    g.drawOval(this.x, this.y, this.radius * 2, this.radius * 2);  
  }
  public void draw(Graphics g) {
    g.drawOval(this.x, this.y, this.radius * 2, this.radius * 2);  
  }
}

--

// Game.java 

import javax.swing.*;
import java.awt.event.*; 
import java.util.*; 
import java.awt.*;

public class Game implements World {
  Snake snake; 
  public Game() {
    this.snake = new Snake("Larry");  
  }
  public void update() {
    this.snake.move(); 
  }
  public void draw(Graphics g) {
    this.snake.draw(g); 
  }
  public void mousePressed(MouseEvent e) { 
  
  } 
  public void keyPressed(KeyEvent e) { 
    // System.out.println( e ); // user responsibility 
    int code = e.getKeyCode(); 
    if (code == 37) { // left 
      this.snake.face("West");
    } else if (code == 38) { // up 
      this.snake.face("North");
    } else if (code == 39) { // right
      this.snake.face("East");
    } else if (code == 40) { // down
      this.snake.face("South");
    } else {
      this.snake.face("Nowhere");
    }
  }
  public static void main(String[] args) {
    BigBang game = new BigBang(30, new Game());  
    JFrame frame = new JFrame("Game"); 
    frame.getContentPane().add( game ); 
    game.addMouseListener( game ); 
    frame.addKeyListener( game ); // user responsibility
    frame.setVisible(true); 
    frame.setSize(400, 400); 
    // frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
    game.start(); 
  }
  public boolean hasEnded() {
    return false; // never!! 
  }
}

--

import java.awt.Graphics; 
import java.util.ArrayList; 

public class Snake {
  private String name;
  private int x, y; 
  private String direction = "Nowhere";
  public void face(String direction) {
    this.direction = direction;      
  }
  Circle head; 
  ArrayList<Circle> body; 
  private int RADIUS =  10; 
  public Snake(String name) {
    this.name = name;  
    this.x = 120; 
    this.y = 70;
    this.direction = "Nowhere";
    this.head = new Circle(this.x, this.y, RADIUS); 
    this.body = new ArrayList<Circle>(); 
    this.body.add(new Circle(this.x + 2 * RADIUS, this.y - 0 * RADIUS, RADIUS)); 
    this.body.add(new Circle(this.x + 4 * RADIUS, this.y - 0 * RADIUS, RADIUS)); 
    this.body.add(new Circle(this.x + 6 * RADIUS, this.y - 0 * RADIUS, RADIUS)); 
    this.body.add(new Circle(this.x + 6 * RADIUS, this.y + 2 * RADIUS, RADIUS)); 
    this.body.add(new Circle(this.x + 6 * RADIUS, this.y + 4 * RADIUS, RADIUS)); 
    this.body.add(new Circle(this.x + 8 * RADIUS, this.y + 4 * RADIUS, RADIUS)); 
    this.body.add(new Circle(this.x + 8 * RADIUS, this.y + 6 * RADIUS, RADIUS)); 
    this.body.add(new Circle(this.x + 8 * RADIUS, this.y + 8 * RADIUS, RADIUS)); 
    this.body.add(new Circle(this.x + 6 * RADIUS, this.y + 8 * RADIUS, RADIUS)); 
    this.body.add(new Circle(this.x + 4 * RADIUS, this.y + 8 * RADIUS, RADIUS)); 
    this.body.add(new Circle(this.x + 2 * RADIUS, this.y + 8 * RADIUS, RADIUS)); 
    this.body.add(new Circle(this.x + 2 * RADIUS, this.y + 6 * RADIUS, RADIUS)); 
  }
  public void draw(Graphics g) {
    g.drawString(name, this.x, this.y); 
    this.head.draw( g, this.x, this.y ); 
    for (Circle c : this.body)
      c.draw(g); 
  }
  public void move() {
    
    if (this.direction.equals("Nowhere")) { 
    
    } else {  
      body.add(0, new Circle(this.head));
      body.remove(body.size() - 1); 
    }    
    if (this.direction.equals("East" )) this.x += 1 * this.RADIUS; 
    if (this.direction.equals("West" )) this.x -= 1 * this.RADIUS;
    if (this.direction.equals("North")) this.y -= 1 * this.RADIUS;
    if (this.direction.equals("South")) this.y += 1 * this.RADIUS;

    if (this.direction.equals("East" )) this.x += 1 * this.RADIUS;
    if (this.direction.equals("West" )) this.x -= 1 * this.RADIUS;
    if (this.direction.equals("North")) this.y -= 1 * this.RADIUS;
    if (this.direction.equals("South")) this.y += 1 * this.RADIUS;
    
  }
}

--

import java.awt.*; 
import java.awt.event.*; 

interface World  {
  public void draw(Graphics g);
  public void update(); 
  public boolean hasEnded();
  public void keyPressed(KeyEvent e); 
  public void mousePressed(MouseEvent e); 
}

--