CS 151 - Lecture 23

Cover page image

Cay S. Horstmann

Lab

Classes for this Lab

We start with this Pair class.

import java.util.AbstractList;

public class Pair<T>
{
   public Pair() { first = null; second = null; }
   public Pair(T first, T second) { this.first = first;  this.second = second; }

   public T get(int n) { return n == 0 ? first : n == 1 ? second : null; }

   public void set(int n, T t) 
   {
      if (n == 0) first = t;
      else if (n == 1) second = t; 
   }

   private T first;
   private T second;
}

In order to avoid the tedious Employee/Manager examples, we will instead use the infinitely more exciting

import java.math.*;

public class LabeledDecimal extends BigDecimal
{
   public LabeledDecimal(String label, String digits)
   {
      super(digits);
      this.label = label;
   }

   public String getLabel() { return label; }
   public String toString() { return label + "=" + super.toString(); }

   private String label;
}

For example,

new LabeledDecimal("pi", "3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825"); 

Wildcards

Min with Comparator

Min with Comparable