Subversion Repositories programming

Rev

Rev 224 | 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
 *
260 ira 10
 * Purpose: Implement a simple-to-access representation of an IP Address.
11
 * This is used extensively by the DHCPTable class. This class allows easy
12
 * access to each of the fields in the IP Address.
224 ira 13
 ******************************************************************************/
221 ira 14
 
15
public class IPAddr
16
{
224 ira 17
    /* Instance Variables */
221 ira 18
    private int[] fields = new int[4];
19
 
224 ira 20
    /**
21
     * Method: IPAddr constructor
22
     * Purpose: Construct an IPAddr object
23
     */
221 ira 24
    public IPAddr (String ip)
25
    {
222 ira 26
        int i;
221 ira 27
        String[] sp = ip.split("\\.");
222 ira 28
 
224 ira 29
        // We must have a bad argument
30
        if (sp.length < 4)
31
            throw new IllegalArgumentException();
32
 
33
        // Set the private variable array to the argument
222 ira 34
        for (i=0; i<sp.length; i++)
221 ira 35
            fields[i] = Integer.parseInt(sp[i]);
36
    }
37
 
224 ira 38
    /**
39
     * Method: getField
40
     * Purpose: Get one of the fields of an IP Address
41
     */
221 ira 42
    public int getField (int fieldNum)
43
    {
224 ira 44
        if (fieldNum < 0 || fieldNum > 3)
45
            throw new IllegalArgumentException();
46
 
221 ira 47
        return fields[fieldNum];
48
    }
49
 
224 ira 50
    /**
51
     * Method: toString
52
     * Purpose: Convert this IPAddr object to a String
53
     */
221 ira 54
    public String toString ()
55
    {
222 ira 56
        String s = new String("" + fields[0] + '.' + fields[1] + '.'
57
                            + fields[2] + '.' + fields[3]);
221 ira 58
        return s;
59
    }
60
}
61