Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Experiment No.

14102B0064

To Implement and find out class of IP address, subnet mask and first & last address of that block

Experiment No.

Implementation

14102B0064

import java.util.*;
class test{
public static void main(String args[])
{ int p=0,count=0,j=0,q,r,u,i,p1,q1,r1,u1;
Scanner t=new Scanner(System.in);
System.out.println("Enter IP Address.\n");
String s = t.nextLine();
String b = "999";
String c = "999";
String d = "999";
String e = "999";
for(i=0;i<s.length();i++)
{
if(s.charAt(i)=='.')
{
if (count==0)
{
b=s.substring(0,i);
j=i;
count++;
}
else if(count==1)
{
c=s.substring(j+1,i);
j=i;
count++;
}
else if(count==2)
{
d=s.substring(j+1,i);
e=s.substring(i+1,s.length());

To Implement and find out class of IP address, subnet mask and first & last address of that block

Experiment No.

14102B0064
count++;
}
else
{
System.out.println("Invalid IP Address.");
return;
}
}
}
p=Integer.parseInt(b);
q=Integer.parseInt(c);
r=Integer.parseInt(d);
u=Integer.parseInt(e);
if((0<=p)&&(p<=255) && (0<=q)&&(q<=255) &&
(0<=r)&&(r<=255) && (0<=u)&&(u<=255))
{
if((0<=p)&&(p<=127))
{
System.out.println("IP Address is class A.");
System.out.println("Host ID: 0."+c+"."+d+"."+e);
p1=p&255;
q1=q&0;
r1=r&0;
u1=u&0;
System.out.println("Net ID: "+p1+"."+q1+"."+r1+"."+u1);
System.out.println("First IP: "+p1+"."+q1+"."+r1+"."+u1);
p1=p|0;
q1=q|255;
r1=r|255;
u1=u|255;
System.out.println("Last IP: "+p1+"."+q1+"."+r1+"."+u1);
}
else if((128<=p)&&(p<=191))
{
System.out.println("IP Address is class B.");
System.out.println("Host ID: 0.0."+d+"."+e);
p1=p&255;
q1=q&255;
r1=r&0;
u1=u&0;
System.out.println("Net ID: "+p1+"."+q1+"."+r1+"."+u1);

To Implement and find out class of IP address, subnet mask and first & last address of that block

Experiment No.

14102B0064
System.out.println("First IP: "+p1+"."+q1+"."+r1+"."+u1);
p1=p|0;
q1=q|0;
r1=r|255;
u1=u|255;
System.out.println("Last IP: "+p1+"."+q1+"."+r1+"."+u1);
}
else if((192<=p)&&(p<=223))
{
System.out.println("IP Address is class C.");
System.out.println("Host ID: 0.0.0."+e);
p1=p&255;
q1=q&255;
r1=r&255;
u1=u&0;
System.out.println("Net ID: "+p1+"."+q1+"."+r1+"."+u1);
System.out.println("First IP: "+p1+"."+q1+"."+r1+"."+u1);
p1=p|0;
q1=q|0;
r1=r|0;
u1=u|255;
System.out.println("Last IP: "+p1+"."+q1+"."+r1+"."+u1);
}
else if((224<=p)&&(p<=239))
{
System.out.println("IP Address is class D.");
}
else if((240<=p)&&(p<=255))
{
System.out.println("IP Address is class E.");
}
}
else
{
System.out.println("Invalid IP Address.");
}
return;
}
}

To Implement and find out class of IP address, subnet mask and first & last address of that block

Experiment No.
Output:

Conclusion

14102B0064

We learn that IP addresses are divided into five classes based on their range, of
which class A, B, C are used in network and class D, E are reserved for
maintenance. The addresses of the classes A, B & C are further divided into
two parts Net ID (Network ID) & Host ID. The Net ID is used to uniquely
identify the network whereas the Host ID is used to uniquely identify the
machines in the network.

To Implement and find out class of IP address, subnet mask and first & last address of that block

You might also like