0% found this document useful (0 votes)
4 views3 pages

CN Lab 4

Uploaded by

Aadesh Lawande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

CN Lab 4

Uploaded by

Aadesh Lawande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

LAB ASSIGNMENT - 4

Problem Statement : Write a program to demonstrate Sub-netting and find


subnet masks.

Code-

import java.util.Scanner;

public class SubnetCalculator {

// Convert IP address from string to integer


public static int ipToInt(String ipAddress) {
String[] parts = ipAddress.split("\\.");
int ip = 0;
for (int i = 0; i < 4; i++) {
ip |= Integer.parseInt(parts[i]) << (24 - (8 * i));
}
return ip;
}

// Convert integer back to IP address string


public static String intToIp(int ip) {
return String.format("%d.%d.%d.%d",
(ip >>> 24) & 0xff,
(ip >>> 16) & 0xff,
(ip >>> 8) & 0xff,
ip & 0xff);
}

// Determine the class of the IP address


public static String getClassType(String ipAddress) {
int firstOctet = Integer.parseInt(ipAddress.split("\\.")[0]);

if (firstOctet >= 1 && firstOctet <= 126) {


return "Class A";
} else if (firstOctet >= 128 && firstOctet <= 191) {
return "Class B";
} else if (firstOctet >= 192 && firstOctet <= 223) {
return "Class C";
} else if (firstOctet >= 224 && firstOctet <= 239) {
return "Class D (Multicast)";
} else if (firstOctet >= 240 && firstOctet <= 255) {
return "Class E (Experimental)";
} else {
return "Invalid IP Address";
}
}

// Calculate default subnet mask based on class type


public static String getDefaultSubnetMask(String classType) {
switch (classType) {
case "Class A":
return "255.0.0.0";
case "Class B":
return "255.255.0.0";
case "Class C":
return "255.255.255.0";
default:
return "N/A"; // Class D and E don't use subnet masks
}
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Prompt user to enter IP in a single line


System.out.print("Enter IP address (e.g., 192.168.1.10): ");
String ipAddress = scanner.nextLine();

// Determine the class of the IP address


String classType = getClassType(ipAddress);

// Calculate default subnet mask based on class type


String subnetMask = getDefaultSubnetMask(classType);

// Display results
System.out.println("IP Address: " + ipAddress);
System.out.println("Class Type: " + classType);
System.out.println("Default Subnet Mask: " + subnetMask);

scanner.close();
}
}
Output-

You might also like