Subversion Repositories programming

Rev

Rev 222 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
224 ira 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
 ******************************************************************************/
221 ira 12
 
13
public class IPAddr
14
{
224 ira 15
    /* Instance Variables */
221 ira 16
    private int[] fields = new int[4];
17
 
224 ira 18
    /**
19
     * Method: IPAddr constructor
20
     * Purpose: Construct an IPAddr object
21
     */
221 ira 22
    public IPAddr (String ip)
23
    {
222 ira 24
        int i;
221 ira 25
        String[] sp = ip.split("\\.");
222 ira 26
 
224 ira 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
222 ira 32
        for (i=0; i<sp.length; i++)
221 ira 33
            fields[i] = Integer.parseInt(sp[i]);
34
    }
35
 
224 ira 36
    /**
37
     * Method: getField
38
     * Purpose: Get one of the fields of an IP Address
39
     */
221 ira 40
    public int getField (int fieldNum)
41
    {
224 ira 42
        if (fieldNum < 0 || fieldNum > 3)
43
            throw new IllegalArgumentException();
44
 
221 ira 45
        return fields[fieldNum];
46
    }
47
 
224 ira 48
    /**
49
     * Method: toString
50
     * Purpose: Convert this IPAddr object to a String
51
     */
221 ira 52
    public String toString ()
53
    {
222 ira 54
        String s = new String("" + fields[0] + '.' + fields[1] + '.'
55
                            + fields[2] + '.' + fields[3]);
221 ira 56
        return s;
57
    }
58
}
59