Subversion Repositories programming

Rev

Rev 222 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 222 Rev 224
Line -... Line 1...
-
 
1
/*******************************************************************************
-
 
2
 * File: IPAddr.java
-
 
3
 * Author: Ira W. Snyder (devel@irasnyder.com)
-
 
4
 * License: GNU General Public License v2
-
 
5
 * Class: CS380 - Computer Networking
-
 
6
 *
-
 
7
 * Assignment: Project #3
-
 
8
 * Date Last Modified: 2006-02-15
-
 
9
 *
-
 
10
 * Purpose: //FIXME
-
 
11
 ******************************************************************************/
1
 
12
 
2
public class IPAddr
13
public class IPAddr
3
{
14
{
-
 
15
    /* Instance Variables */
4
    private int[] fields = new int[4];
16
    private int[] fields = new int[4];
5
 
17
 
-
 
18
    /**
-
 
19
     * Method: IPAddr constructor
-
 
20
     * Purpose: Construct an IPAddr object
-
 
21
     */
6
    public IPAddr (String ip)
22
    public IPAddr (String ip)
7
    {
23
    {
8
        int i;
24
        int i;
9
        String[] sp = ip.split("\\.");
25
        String[] sp = ip.split("\\.");
10
 
26
 
-
 
27
        // We must have a bad argument
-
 
28
        if (sp.length < 4)
-
 
29
            throw new IllegalArgumentException();
-
 
30
 
-
 
31
        // Set the private variable array to the argument
11
        for (i=0; i<sp.length; i++)
32
        for (i=0; i<sp.length; i++)
12
            fields[i] = Integer.parseInt(sp[i]);
33
            fields[i] = Integer.parseInt(sp[i]);
13
    }
34
    }
14
 
35
 
-
 
36
    /**
-
 
37
     * Method: getField
-
 
38
     * Purpose: Get one of the fields of an IP Address
-
 
39
     */
15
    public int getField (int fieldNum)
40
    public int getField (int fieldNum)
16
    {
41
    {
17
        return fields[fieldNum];
-
 
18
    }
-
 
19
 
-
 
20
    public int compareTo (IPAddr ip)
-
 
21
    {
-
 
22
        int i;
-
 
23
 
-
 
24
        for (i=0; i<4; i++)
-
 
25
            if (fields[i] < ip.getField(i))
42
        if (fieldNum < 0 || fieldNum > 3)
26
                return -1;
-
 
27
            else if (fields[i] > ip.getField(i))
43
            throw new IllegalArgumentException();
28
                return 1;
-
 
29
 
-
 
30
        return 0; // Equal
-
 
31
    }
-
 
32
    
-
 
33
    public int compareTo (String ip)
-
 
34
    {
-
 
35
        IPAddr that = new IPAddr (ip);
-
 
36
 
44
 
37
        return this.compareTo(that);
45
        return fields[fieldNum];
38
    }
46
    }
39
 
47
 
-
 
48
    /**
-
 
49
     * Method: toString
-
 
50
     * Purpose: Convert this IPAddr object to a String
-
 
51
     */
40
    public String toString ()
52
    public String toString ()
41
    {
53
    {
42
        String s = new String("" + fields[0] + '.' + fields[1] + '.'
54
        String s = new String("" + fields[0] + '.' + fields[1] + '.'
43
                            + fields[2] + '.' + fields[3]);
55
                            + fields[2] + '.' + fields[3]);
44
        return s;
56
        return s;