Homework 06 due today

Chapter 14 15

Lists, Maps, Sets, Stacks, Queues

Exam 03 

import java.util.*;
import java.io.*; 

public class Example {
  public static void main(String[] args) throws Exception {
    Scanner in = new Scanner(new File(args[0]));  
    Map<String, Integer> d = new HashMap<String, Integer>(); 
    while (in.hasNext()) {
      String token = in.next(); 
      if (d.get(token) == null) {
        d.put(token, 1);   
      } else {
        d.put(token, 1 + d.get(token));   
      }
      System.out.println( d ); 
    }
    ArrayList<Pair> p = new ArrayList<Pair>();
    for ( String name : d.keySet() ) {
      p.add( new Pair( name, d.get(name) ) ); 
    }
    System.out.println( p ); 
    Collections.sort( p ); 
    System.out.println( p ); 
  }
}

public class Pair implements Comparable<Pair> {
  private String name; 
  private int votes;
  Pair(String name, int votes) {
    this.name = name;
    this.votes = votes; 
  }
  public int compareTo(Pair other) {
    return - this.votes + other.votes;  
  }
  public String toString() {
    return "(" + this.name + ":" + this.votes + ")"; 
  }
}

Exercise: 

import java.util.*; 

public class Exercise {
  public static void main(String[] args) {
    Stack<String> s = new Stack<String>();
    String sentence = "Mary had a little lamb.";
    Scanner in = new Scanner(sentence); 
    while (in.hasNext()) {
      String word = in.next(); 
      s.push( word ); 
      System.out.println( s ); 
    }
    String answer = ""; 
    while (! s.empty()) {
      String word = s.pop(); 
      answer = answer + " " + word; // word + " " + answer;
    }
    System.out.println( answer );
  }
}

How can I build a general kind of sort? 

public class Utilities {
  public static void sort(int[] a) {
    boolean sorted = true; 
    for (int i = 0; i < a.length - 1; i++) {
      if (a[i] < a[i+1]) {
        int temp = a[i];
        a[i] = a[i+1]; 
        a[i+1] = temp; 
        sorted = false; 
      }
    }
    if (sorted) return;
    else sort(a); 
  }
  public static void main(String[] args) {
    int[] u = new int[] {3, 2, 4, 5, 2, 6, 1, 4}; 
    System.out.println( java.util.Arrays.toString( u ) ); 
    Utilities.sort( u ); 
    System.out.println( java.util.Arrays.toString( u ) ); 
  }
}

Here's the general approach:

public class Utilities {
  public static void sort(Sortable[] a) {
    boolean sorted = true; 
    for (int i = 0; i < a.length - 1; i++) {
      if (a[i].rctmpx(a[i+1]) == 1) { // a[i] < a[i+1]) {
        Sortable temp = a[i];
        a[i] = a[i+1]; 
        a[i+1] = temp; 
        sorted = false; 
      }
    }
    if (sorted) return;
    else sort(a); 
  }
  public static void main(String[] args) {
    //int[] u = new int[] {3, 2, 4, 5, 2, 6, 1, 4}; 
    //System.out.println( java.util.Arrays.toString( u ) ); 
    Sortable[] u = new Sortable[3]; 
    u[0] = new Thing("Larry", 3);
    u[1] = new Thing("Larry", 5);
    u[2] = new Thing("Larry", 1);
    System.out.println( java.util.Arrays.toString( u ) ); 
    Utilities.sort( u ); 
    System.out.println( java.util.Arrays.toString( u ) ); 
  }
}


interface Sortable {
  public int rctmpx(Sortable other);  
  public int getPoints();
}

public class Thing implements Sortable {
  String name;
  int points;
  public Thing(String name, int points) {
    this.name = name;
    this.points = points; 
  }
  public String toString() {
    return "T(" + name + "-" + points + ")";  
  }
  public int getPoints() {
    return this.points;  
  }
  public int rctmpx(Sortable other) {
     if (this.getPoints() < other.getPoints()) return 1; // swap 
     else if (this.getPoints() > other.getPoints()) return -1; 
     else return 0; 
  }
}

So because you primise to comply to my expected interface I can sort your objects.

--