CSC212 Example of Generic Pair Class & Exercise Solutions
(Redirected from CSC212 Example of Generic Pair Class)
--D. Thiebaut (talk) 12:07, 23 September 2014 (EDT)
Contents
Non-Generic Pair Class
class PairInts {
private int first;
private int second;
public PairInts( int f, int s ) {
first = f;
second = s;
}
public int getFirst() { return first; }
public int getSecond() { return second; }
public void setFirst( int f ) { first = f; }
public void setSecond( int s ) { second = s; }
public String toString( ) { return String.format( "(%d,%d)", first, second ); }
}
Test Class
class TestPairInts {
static public void main( String[] args ) {
PairInts p1 = null, p2 = null;
p1 = new PairInts( 10, 3 );
p2 = new PairInts( 5, -9 );
System.out.println( "p1 = " + p1 );
System.out.println( "p2 = " + p2 );
p1.setFirst( p2.getSecond() );
System.out.println( "p1 = " + p1 );
}
}
Generic Pair Class
public class Pair<T, P> {
private T first;
private P second;
public Pair( T f, P s ) {
first = f;
second = s;
}
public T getFirst() { return first; }
public P getSecond() { return second; }
public void setFirst( T f ) { first = f; }
public void setSecond( P s ) { second = s; }
public String toString( ) { return "(" + first + ", " + second + ")"; }
}
Test Class
class TestPair {
static public void main( String[] args ) {
Pair<int, int> p1 = null, p2 = null;
p1 = new Pair<int, int>( 10, 3 );
p2 = new Pair<int, int>( 5, -9 );
System.out.println( "p1 = " + p1 );
System.out.println( "p2 = " + p2 );
p1.setFirst( p2.getSecond() );
System.out.println( "p1 = " + p1 );
}
}
Exercise 1
Problem
- Create an simple program that
- uses the PairInts class
- creates an array of 10 pairs, where the first number of the pair in a positive integer (random), and the second number is the fibonacci term equivalent to the first number. For example: (0, 1), (1, 1), (2, 2), (3, 3), (4, 5), (5, 8), (6, 13), etc.
- displays the array on the screen.
Exercise 1 Solution
public class Exercise1Pair {
private static int fibAt( int index ) {
if ( index==0 || index== 1 )
return 1;
return fibAt( index-1 ) + fibAt( index-2 );
}
public static void main(String[] args) {
// create the array of pairs
PairInts[] A = new PairInts[10];
// initialize it with different pairs
for ( int i=0; i<10; i++ ) {
int index = ( i * 2 );
int fib = fibAt( index );
A[ i ] = new PairInts( index, fib );
}
// display the array
for ( int i=0; i<10; i++ )
System.out.println( String.format( "(%d,%d)", A[i].getFirst(),
A[i].getSecond() ) );
}
}
class PairInts {
private int first;
private int second;
public PairInts( int f, int s ) {
first = f;
second = s;
}
public int getFirst() { return first; }
public int getSecond() { return second; }
public void setFirst( int f ) { first = f; }
public void setSecond( int s ) { second = s; }
public String toString( ) {
return String.format( "(%d,%d)", first, second ); }
}
Exercise 2
Problem
- Same as Exercise 1, but this time the program
- uses the generic Pair Class,
- creates an array of 10 pairs, where the first number of the pair in a positive integer (random), and the second number is the fibonacci term equivalent to the first number. For example: (0, 1), (1, 1), (2, 2), (3, 3), (4, 5), (5, 8), (6, 13), etc.
- displays the array on the screen.
Solution
public class Exercise2Pair {
private static int fibAt( int index ) {
if ( index==0 || index== 1 )
return 1;
return fibAt( index-1 ) + fibAt( index-2 );
}
public static void main(String[] args) {
// create the array of pairs
Pair<Integer, Integer>[] A = new Pair[10];
// initialize it with different pairs
for ( int i=0; i<10; i++ ) {
int index = ( i * 2 );
int fib = fibAt( index );
A[ i ] = new Pair<Integer, Integer>( index, fib );
}
// display the array
for ( int i=0; i<10; i++ )
System.out.println( String.format( "(%d,%d)", A[i].getFirst(),
A[i].getSecond() ) );
}
}
class Pair<T, P> {
private T first;
private P second;
public Pair( T f, P s ) {
first = f;
second = s;
}
public T getFirst() { return first; }
public P getSecond() { return second; }
public void setFirst( T f ) { first = f; }
public void setSecond( P s ) { second = s; }
public String toString( ) {
return "(" + first + ", " + second + ")"; }
}