Challenge Two

Know how to solve all of these problems:

Here are some notes to help you with these programs.

-bash-3.2$ cat One.java
import java.util.*; 

class One {
  public static void main(String[] args) {
    ArrayList<Toy> animals; 
    animals = new ArrayList<Toy>(); 
    animals.add(new Dog()); 
    animals.add(new Pig());
    animals.add(new Pig()); 
    for (Toy a : animals) {
      a.squeeze(); 
    } 
  } 
}

abstract class Toy {
  abstract void squeeze(); 
} 

class Pig extends Toy {
  void squeeze() {
    System.out.println("Oink! Oink!"); 
  } 
}

class Dog extends Toy {
  void squeeze() {
    System.out.println("Bow-wow!"); 
  } 
}

-bash-3.2$ javac One.java
-bash-3.2$ java One
Bow-wow!
Oink! Oink!
Oink! Oink!
-bash-3.2$ 
We will discuss this example in class.

Here's another one:

-bash-3.2$ cat Two.java
import java.util.*; 

class Two {
  public static void main(String[] args) {
    ArrayList<Student> students; 
    students = new ArrayList<Student>(); 
    students.add(new Student("Matt Roth")); 
    students.add(new Student("Tom Pritchard")); 
    students.add(new Student("Malik Story")); 
    students.add(new Student("Nick Williams")); 
    students.add(new Student("Daniel Moore")); 
    students.add(new Student("Kip Schutz")); 

    for (Student student : students) {
      student.report(); 
    }

    System.out.println("-----------------------------"); 

    Collections.sort(students); 

    for (Student student : students) {
      student.report(); 
    }
  } 
}

class Student implements Comparable<Student> {
  int age; 
  String name; 
  Student(String name) {
    this.name = name; 
    this.age = (int)(Math.random() * 10 + 16); 
  } 
  void report() {
    System.out.println("Student: " + this.name + ", age: " + this.age); 
  } 
  public int compareTo(Student object) {
    double value = this.age - ((Student)object).age;
    if (value > 0) return 1;
    else if (value == 0) return 0;
    else return -1;    
  }
}

-bash-3.2$ javac Two.java
-bash-3.2$ java Two
Student: Matt Roth, age: 21
Student: Tom Pritchard, age: 21
Student: Malik Story, age: 16
Student: Nick Williams, age: 19
Student: Daniel Moore, age: 19
Student: Kip Schutz, age: 24
-----------------------------
Student: Malik Story, age: 16
Student: Nick Williams, age: 19
Student: Daniel Moore, age: 19
Student: Matt Roth, age: 21
Student: Tom Pritchard, age: 21
Student: Kip Schutz, age: 24
-bash-3.2$ java Two
Student: Matt Roth, age: 22
Student: Tom Pritchard, age: 20
Student: Malik Story, age: 17
Student: Nick Williams, age: 21
Student: Daniel Moore, age: 16
Student: Kip Schutz, age: 17
-----------------------------
Student: Daniel Moore, age: 16
Student: Malik Story, age: 17
Student: Kip Schutz, age: 17
Student: Tom Pritchard, age: 20
Student: Nick Williams, age: 21
Student: Matt Roth, age: 22
-bash-3.2$ 
Here's where you can read about it.

Here's the really big index.

Here's some more information.

And the equivalent from the Java tutorial.

This link is also useful (or this one).