-bash-4.2$ date
Mon Dec  2 12:34:22 EST 2019
-bash-4.2$

--

Exam 03 grades hopefully soon 

Final Exam: needs a study guide 

https://cs.indiana.edu/classes/c212/fall2019/pre-test-c343.pdf

Generic Types

public class Fraction {
  private int num, den; 
  public Fraction(int num, int den) {
    this.num = num; 
    this.den = den;
  }
  public Fraction add(Fraction other) {
    return new Fraction( this.num * other.den +
                         this.den * other.num , 
                         this.den * other.den);  
  }
  public String toString() {
    return this.num + "/" + this.den; 
  }
  public static void main(String[] args) {
    Fraction a = new Fraction(1, 2); 
    System.out.println( a + " + " + a + " = " + a.add(a)); // 1 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Fraction
1/2 + 1/2 = 4/4


--

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> ArrayList<Integer> a 
Static Error: Undefined class 'ArrayList'
> import java.util.ArrayList; // auto-import
ArrayList<Integer> a
> a
null
> ArrayList<String> b
> b = new ArrayList<String>()
[]
> b
[]
> b.add("Ata")
true
> b.add("Kyle")
true
> b
[Ata, Kyle]


--

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> ArrayList<Integer> a 
Static Error: Undefined class 'ArrayList'
> import java.util.ArrayList; // auto-import
ArrayList<Integer> a
> a
null
> ArrayList<String> b
> b = new ArrayList<String>()
[]
> b
[]
> b.add("Ata")
true
> b.add("Kyle")
true
> b
[Ata, Kyle]
> ArrayList<ArrayList<String>> room = new ArrayList<ArrayList<String>>()
> room
[]
> room.add(new ArrayList<String>())
true
> room.add(new ArrayList<String>())
true
> room
[[], []]
> room.get(1).add("Brooke")
true
> room
[[], [Brooke]]
> room.get(0).add("Brooke")
true
> room.get(1).remove("Brooke")
true
> room
[[Brooke], []]
> room.get(1).add("Mason")
true
> room.get(1).add("Josh")
true
> room
[[Brooke], [Mason, Josh]]


--

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Map<String, Integer> ali
Static Error: Undefined class 'Map'
> import java.util.Map; // auto-import
Map<String, Integer> ali
> ali 
null
> ali = new Map<String, Integer>()
Static Error: Cannot construct a Map<String, Integer>: a concrete class is required
> ali = new HashMap<String, Integer>()
Static Error: Undefined class 'HashMap'
> import java.util.HashMap; // auto-import
ali = new HashMap<String, Integer>()
{}
> ali
{}
> ali.put("Zach", 12)
null
> ali
{Zach=12}
> ali.put("Jonah", 8)
null
> ali
{Zach=12, Jonah=8}


--

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Pair<String, Integer> ali
> ali
null
> ali = new Pair<String, Integer>()
Static Error: No constructor in Pair<String, Integer> matches this invocation
    Arguments: ()
    Expected return type: Pair<String, Integer>
    Candidate signatures: Pair(String, Integer)
> ali = new Pair<String, Integer>("Zach", 12)
(Zach, 12)
> ali
(Zach, 12)
> ali = new Pair<String, Integer>("Jonah", 8)
(Jonah, 8)
> ali
(Jonah, 8)
> ali.getFirst()
"Jonah"
> ali.getSecond()
8
>  Pair<String, Integer> result = new Pair<String, Integer>("Larry Joe Bird", 1955); 
> result
(Larry Joe Bird, 1955)


--

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Pair<String, Integer> a = new Pair<String, Integer>("One", 23);
> Pair<Pair<String, Integer>, Integer> b = new Pair<Pair<String, Integer>, Integer>(a, -2);  
> b
((One, 23), -2)
> ArrayList<Integer> scores
Static Error: Undefined class 'ArrayList'
> import java.util.ArrayList; // auto-import
ArrayList<Integer> scores
> scores
null
> scores = new ArrayList<Integer>()
[]
> scores.add(89)
true
> scores.add(93)
true
> scores.add(90)
true
> scores
[89, 93, 90]
> Pair<Pair<String, Integer>, ArrayList<Integer>> structure 
> structure
null
> Pair<String, Integer> player = new Pair("Zach", 12)
Static Error: Bad types in assignment: from raw Pair to Pair<String, Integer>
> Pair<String, Integer> player = new Pair<String, Integer>("Zach", 12)
> player
(Zach, 12)
> scores
[89, 93, 90]
> structure
null
> structure = new Pair<Pair<String, Integer>, ArrayList<Integer>>(player, scores)
((Zach, 12), [89, 93, 90])
> structure
((Zach, 12), [89, 93, 90])


--

class Methods {
  public static void main(String[] args) {
    Integer[] a = {1, 2, 3}; 
    Double[] b = {3.141592, 1.4142, 1.73}; 
    String[] c = args; 
    Methods.show( a ); 
    Methods.show( b ); 
    Methods.show( c ); 
  } 
  public static <E> void show(E[] a) {
    for (E element : a) 
      System.out.print( element + " " ); 
    System.out.println("\n---------------------"); 
  } 
}

--

A Player is a name with a number of points in [10, 30). 

Create an array list of Players and sort it. 

All players with scores <= 21 come first in descending order. 

Then the players with scores > 21 come in ascending order. 

Approach: 

public class Player implements Comparable<Player> {
  String name; 
  int points; 
  public Player(String name, int points) { ... }
  public String toString() { ... }
  public int compareTo(Player other) {
    if (this.points > 21 && other.points <= 21) return 1; // other in front  
    else if ... 
  } 
}

--

import java.text.*;
import java.util.*;

interface Vertailukelpoinen<T> { // in honor of Linus Torvalds
  int vertaa(T other);
}

--

class Student implements Vertailukelpoinen<Student> {
  String name;
  int age;
  Student(String name, int age) {
    this.name = name;
    this.age = age;
  }
  public int vertaa(Student other) {
    if (this.age < other.age) return -1;
    else if (this.age > other.age) return 1;
    else return 0;
  }
  public String toString() {
    return name + ":" + age;
  }
}

--

class One {
  public static void main(String[] args) {
    Student[] students = new Student[6];
    students[0] = new Student("Alice" , 4);
    students[1] = new Student("Bob"   , 8);
    students[2] = new Student("John"  , 6);
    students[3] = new Student("Tom"   , 7);
    students[4] = new Student("Leslie", 8);
    students[5] = new Student("Alex"  , 5);

    System.out.println( "1: " + Two.toString(students) );
    Two.sort( students );
    System.out.println( "2: " + Two.toString(students) );
  }
}

--

class Two {

  static <T extends Vertailukelpoinen<T>> void sort(T[] a) {
    boolean sorted;
    do {
      sorted = true;
      for (int i = 0; i < a.length - 1; i++) {
        if ((a[i]).vertaa(a[i+1]) > 0) {
          sorted = false;
          T temp = a[i];
          a[i] = a[i+1];
          a[i+1] = temp;
        }
      }
      System.out.println( " :" + Two.toString(a) );
    } while ( ! sorted );
  }

  static <E> String toString(E[] a) {
    String result = "";
    for (E e : a)
      result += e + ", ";
    return result.substring(0, result.length() - 2);
  }

}

--

Compile and run One.