Professional Documents
Culture Documents
class,network
mask, network id, host id, first host of network, last host of network, no. of host per network,
broadcast address.
Program: -
import java.util.*;
class IPclass{
private char class_alpha;
private String networkid = "";
private String netmask = "";
private String hostid = "";
private String firsthost = "";
private String lasthost = "";
private String broadcastid = "";
private String application;
private int hostno;
private int n;
IPclass(String[] ipoctets){
setClass_alpha(class_alpha,ipoctets);
setN(n);
setNetmask(netmask);
setNetworkid(networkid,ipoctets);
setBroadcastid(broadcastid);
setHostid(hostid,ipoctets);
setFirsthost(firsthost);
setLasthost(lasthost);
setApplication(application);
setHostno(hostno);}
private void setN(int n) {if(class_alpha=='A'){
n = 1;}
else if(class_alpha == 'B'){
n = 2;}
else if(class_alpha == 'C'){
n=3;}
else {
n = 0;}
this.n = n;}
private void setClass_alpha(char class_alpha , String[] ipoctets) {
int a = Integer.parseInt(ipoctets[0]);
if(a<=127 && a>=0){
class_alpha = 'A';}
else if (a<=191&&a>127){
class_alpha = 'B';}
else if(a<=224 && a>191){
class_alpha = 'C';}
else if(a<=240 && a>224){
class_alpha = 'D';}
else {
class_alpha = 'E';}
this.class_alpha = class_alpha;}
private void setNetmask(String netmask) {
if (class_alpha == 'A'){
netmask = "255.0.0.0";}
else if (class_alpha == 'B'){
netmask = "255.255.0.0";}else if(class_alpha == 'C'){
netmask = "255.255.255.0";}
else {
netmask = "not applicable";}
this.netmask = netmask;}
private void setHostid(String hostid, String[] ipoctet) {
if (n!=0) {
for (int i = 0; i < n; i++) {
hostid += "0";
hostid += ".";}
for (int i = n; i < 4; i++) {
hostid += ipoctet[i];
if (i < 3) {
hostid += ".";} }}
else {
hostid = "not applicable";}
this.hostid = hostid;}
private void setNetworkid(String networkid, String[] ipoctet) {
if (n!=0) {
for (int i = 0; i < n; i++) {
networkid += ipoctet[i];
networkid += ".";}
for (int i = n; i < 4; i++) {networkid += "0";
if (i < 3) {
networkid += ".";} }}
else {
networkid = "not applicable";}
this.networkid = networkid;}
private void setHostno(int hostno) {
if (n!=0) {
hostno = (int) Math.pow(2, (double) (8 * (4 - n))) - 2;
}else hostno =0;
this.hostno = hostno;}
public void setFirsthost(String firsthost) {
if (networkid=="not applicable"){
firsthost = networkid;}
else {
String[] x = networkid.split("\\.");
for (int i = 0; i < 3; i++) {
firsthost += x[i];
firsthost += ".";}
firsthost += "1";}
this.firsthost = firsthost;}
private void setBroadcastid(String broadcastid) {
if (networkid=="not applicable"){broadcastid = networkid;}
else {
String[] x = networkid.split("\\.");
for (int i = 0; i < n; i++) {
broadcastid += x[i];
broadcastid += '.';}
for (int i = n; i < 4; i++) {
broadcastid += "255";
if (i < 3) {
broadcastid += ".";} } }
this.broadcastid = broadcastid;}
private void setLasthost(String lasthost) {
if (networkid=="not applicable"){
lasthost = networkid;}
else {
String[] x = broadcastid.split("\\.");
for (int i = 0; i < 3; i++) {
lasthost += x[i];
lasthost += ".";}
lasthost += "254";}
this.lasthost = lasthost;}
private void setApplication(String application) {
if(n!=0){application = "Unicast";}
else {
application = "Multicast";}
this.application = application;}
public char getClass_alpha() {
return class_alpha;}
public int getN() {
return n;}
public String getNetmask() {
return netmask;}
public String getNetworkid() {
return networkid;}
public String getHostid() {
return hostid;}
public String getFirsthost() {
return firsthost;}
public String getLasthost() {
return lasthost;}
public String getBroadcastid() {
return broadcastid;}
public int getHostno() {return hostno;}
public String getApplication() {
return application;} }
public class CNLAB9 {
//check if theres a mixture of binary & dotted decimal
static void nobinarynocheck(String[] ipoctets){
for (int i=0;i< ipoctets.length;i++){
boolean flag = false;
for (int j=0;j<ipoctets[i].length();j++){
if(Integer.parseInt(String.valueOf(ipoctets[i].charAt(j)))>1){
flag = true;} }
if(ipoctets[i].length()>3 && !flag){
System.out.println("Validity Check : Entered IP Address is invalid");
System.out.println("Error : A mixture of Binary and Dotted Decimal notation is not allowed");
System.out.println();
System.exit(0);} } }
//check leading zero error
static void leadingzerocheck(String[] ipoctets){
for (int i=0;i< ipoctets.length;i++){
if(Integer.parseInt(ipoctets[i])==0 && ipoctets[i].length()>1){
System.out.println("Validity Check : Entered IP Address is invalid");
System.out.println("Error : Leading zero error");
System.out.println();System.exit(0);}
else {
if (Integer.parseInt(ipoctets[i])!=0 && ipoctets[i].charAt(0)=='0'){
System.out.println("Validity Check : Entered IP Address is invalid");
System.out.println("Error : Leading zero error");
System.out.println();
System.exit(0);} } } }
//check if no.of octets is 4
static void noofoctets(String[] ipoctet){
if (ipoctet.length>4){
System.out.println("Validity Check : Entered IP Address is invalid");
System.out.println("Error : There cannot be more or less than 4 octets");
System.out.println();
System.exit(0);} }
//check value of a octet is in range 0-255
static void octatevalidity(String[] ipoctets){
for (int i=0;i< ipoctets.length;i++){
if (Integer.parseInt(ipoctets[i])>255 || Integer.parseInt(ipoctets[i])<0){
System.out.println("Validity Check : Entered IP Address is invalid");
System.out.println("Error : Value of octet can only be in range of 0-255");
System.out.println();
System.exit(0);} } }
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println();
System.out.println("--------------------IPv4 Addressing------------------");
System.out.print("Enter IP address : ");
String ip = sc.nextLine();
String[] ipoctets = ip.split("\\.");
System.out.println();
noofoctets(ipoctets);
nobinarynocheck(ipoctets);
octatevalidity(ipoctets);
leadingzerocheck(ipoctets);
System.out.println("Validity Check : Entered IP Address is valid");
System.out.println();
IPclass ipaddress = new IPclass(ipoctets);
System.out.println("IP address - "+ip);
System.out.println("Class - " + ipaddress.getClass_alpha());
System.out.println("Network mask - "+ipaddress.getNetmask());
System.out.println("Network ID - "+ipaddress.getNetworkid());
System.out.println("Host ID - " + ipaddress.getHostid());
System.out.println("First Host ID - "+ipaddress.getFirsthost());
System.out.println("Last Host ID - " + ipaddress.getLasthost());
System.out.println("Broadcast Address - " + ipaddress.getBroadcastid());
if(ipaddress.getN()!=0) {
System.out.println("No. of Host per network - "+ipaddress.getHostno());}
else{
System.out.println("No. of Host per network - "+"Not applicable");}
System.out.println("Application - "+ipaddress.getApplication());
System.out.println();
sc.close();} }
OUTPUT: -
Validity checks
A.) Leading Zero checks
2.) Class B:
3.) Class C:
4.) Class D:
5.) Class E: