Difference between revisions of "CSC212 Lab 5 2014"

From dftwiki3
Jump to: navigation, search
(Problem 1: Generic Classes)
Line 4: Line 4:
 
<br />
 
<br />
 
* Store the program below in a file called '''Pair.java'''
 
* Store the program below in a file called '''Pair.java'''
 +
<br />
 +
 
:::<source lang="java">
 
:::<source lang="java">
 
public class Pair<T, P> {
 
public class Pair<T, P> {
Line 22: Line 24:
  
 
</source>
 
</source>
 +
<br />
 +
 
* Store the program below in a file called TestPair.java
 
* Store the program below in a file called TestPair.java
 
<br />
 
<br />
Line 67: Line 71:
 
: Make your program output the 6 pairs.
 
: Make your program output the 6 pairs.
 
<br />
 
<br />
 +
 
=Problem #2=
 
=Problem #2=
 
<br />
 
<br />

Revision as of 05:51, 24 September 2014

--D. Thiebaut (talk) 16:12, 23 September 2014 (EDT)


Problem 1: Generic Classes


  • Store the program below in a file called Pair.java


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 + ")"; }
}


  • Store the program below in a file called TestPair.java


class TestPair {
    static public void main( String[] args ) {
        Pair<Integer, Integer> p1 = null, p2 = null;

        p1 = new Pair<Integer, Integer>( 10, 3 );
        p2 = new Pair<Integer, Integer>( 5, -9 );

        System.out.println( "p1 = " + p1 );
        System.out.println( "p2 = " + p2 );
        p1.setFirst( p2.getSecond() );
        System.out.println( "p1 = " + p1 );
    }
}


Question 1
Compile and run your program:
javac TestPair.java Pair.java 
java TestPair

Verify that your program works.


Question 2
Modify the toString() method of Pair.java, so that it prints a pair this way: [10|3], rather than (10, 3). Verify that your edited program works.


Question 3
Change TestPair.java' and replace Integer with Double, so that your program can hold pairs of real numbers. Update the initialization of p1 and p2, so that you store real numbers in the pairs. Verify that your edited program works. Notice that you do not have to modify Pair.java at all; that's the beauty of a generic class: it will work for any type of data you want it to contain.


Question 4
Change TestPair.java again, and this time modify it so that p1 and p2 will contain ( "Smith College", 1871 ), and ( "Amherst College", 1821). Verify that your edited program works.


Question 5
Study the solution program for Exercise 2 (see in class). Modify TestPair.java so that it holds an array of 6 (String, Integer) pairs. Initialize the array with
  • U. Bologna, 1088       (these are the dates these universities were founded)
  • U. Oxford, 1167
  • U. Cambridge, 1209
  • U. Salamanca, 1218
  • U. Padua, 1222
  • U. Naples, 1224
Make your program output the 6 pairs.


Problem #2


  1. create a generic class for triplet
  2. create array of 10 triplets( )
  3. exception:
  1. secure some pieces of code with try catch
  2. throw