Exp3 9
Exp3 9
Experiment No. 3
Program 1
FLOWCHART:
PROGRAM: import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter IP Address:");
String s = scan.next();
String check = "";
int n =s.length();
for(int i=0;i<n;i++){
if(s.charAt(i)=='.'){
break;
}
else check= check+Character.toString(s.charAt(i));
}
int find = Integer.parseInt(check);
// binary array of length 8
int[] arr = new int[8];
for(int i = 7;i>=0;i--){
arr[i] = (int)(find&1);
find= find>>1;
}
int cnt = 0;
// using concept of Priority Bits of each class to
differentiate:
for(int i =0;i<4;i++){
if(arr[i] ==1) cnt++;
else break;
}
//calling specific function:
if(cnt == 0){
A();
}
else if(cnt == 1){
B();
}
else if(cnt == 2){
C();
}
else if(cnt == 3){
D();
}
else if(cnt == 4){
E();
}
else {
System.out.println("Not a valid ip");
}
String line;
Pattern pattern = Pattern.compile("IPv4 Address[. ]*:
([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)");
while ((line = reader.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
return matcher.group(1);
}
}
return null;
}
}
RESULT:
Program 2
}
public static void subnet(String s,int no_of_required_networks, int
getClass){
// 2^n>=15
// calculating n (NO of Network Bits:)
double nd =
Math.log((double)(no_of_required_networks))/Math.log((double)(2));
int n;
if((int)nd == nd){
n = (int)nd;
}
else{
n = (int)nd +1;
}
//calculating Subnets:
int subnets = (int)Math.pow(2,n);
//Calculating hosts/subnet
int hosts_per_subnet = (int)(Math.pow(2,h));
System.out.println("No of Subnets: "+subnets);
System.out.println("No of Hosts/Subnet: "+hosts_per_subnet);
for(int i = 0;i<4;i++){
String temp = "";
inner:
for(int k = j;k<size;k++,j++){
if(s.charAt(k)=='.'){
j++;
break inner;
}
else{
temp+=Character.toString(s.charAt(k));
}
}
int val = Integer.parseInt(temp);
int idx = str_id[i];
while(val!=0){
ip[idx] = (int)(val&1);
idx--;
val=val/2;
}
}
//calculating customised subnet mask and additions to print the range
of IPs
int[] custom_mask = new int[32];
int[] static_addition = new int[32];
Arrays.fill(custom_mask, 1);
for(int i = 31;i>31-h;i--){
custom_mask[i] = 0;
static_addition[i] = 1;
ip[i] = 0;
}
int[] customised_subnet_mask = new int[4];
for(int i = 0;i<4;i++){
int idx = str_id[i]-7;
String temp = "";
while(idx<=str_id[i]){
temp+=Integer.toString(custom_mask[idx]);
idx++;
}
customised_subnet_mask[i] = Integer.parseInt(temp,2);
}
//Printing customised subnet mask:
System.out.print("Customised Subnet Mask:");
for (int i = 0; i < 4; i++) {
if(i!=3) System.out.print(customised_subnet_mask[i]+".");
else System.out.println(customised_subnet_mask[i]);
}
// Printing required subnets range
System.out.println("Subnets ranges from :");
int cnt = 0;
for(int i = 0;i<subnets;i++){
if(i!=0){
//updating ip by adding 1 at last
int id = 31;
ip[id]+=1;
while(ip[id]!=1){
ip[id-1]+=1;
ip[id] = 0;
id--;
}
}
cnt++;
if(cnt>no_of_required_networks){
// ip to string and ip last
System.out.println();
temp+=Integer.toString(ip[l]);
res+= (Integer.toString(Integer.parseInt(temp,2)));
}
else if(l!=0){
res+= (Integer.toString(Integer.parseInt(temp,2))+".");
temp = "";
}
}
temp+=Integer.toString(ip[l]);
}
temp ="";
String res2="";
if(getClass == 3) for(int l = 24;l <32;l++) ip[l] =1;
else if(getClass == 2) for(int l = 16;l <32;l++) ip[l] =1;
else for(int l = 8;l <32;l++) ip[l] =1;
inner3:
for(int l = 31;l>=0;l--){
if(ip[l] == 0) ip[l] = 1;
else break inner3;
}
for(int l = 0;l<32;l++){
if(l%8 == 0 || l == 31){
if(l == 31) {
temp+=Integer.toString(ip[l]);
res2+= (Integer.toString(Integer.parseInt(temp,2)));
}
else if(l!=0){
res2+= (Integer.toString(Integer.parseInt(temp,2))+".");
temp = "";
}
}
temp+=Integer.toString(ip[l]);
}
System.out.println(res+" to "+res2);
break;
}
getStr(ip,static_addition);
}
if(i%8 == 0 || i == 31){
if(i == 31) temp+=Integer.toString(ip[i]);
if(i!=0){
start[idx] = Integer.toString(Integer.parseInt(temp,2));
idx++;
temp = "";
}
}
temp+=Integer.toString(ip[i]);
}
System.out.print(start[0]+"."+start[1]+"."+start[2]+"."+start[3]+" to
");
for(int i = 31;i>=0;i--){
ip[i] += static_addition[i];
if(ip[i] == 2){
ip[i-1] += 1;
ip[i] = 0;
}
if(ip[i] == 3){
ip[i-1]+=1;
ip[i] = 1;
}
}
idx = 0;
temp = "";
for(int i = 0;i<32;i++){
if(i%8 == 0 || i == 31){
if(i == 31) temp+=Integer.toString(ip[i]);
if(i!=0){
end[idx] = Integer.toString(Integer.parseInt(temp,2));
idx++;
temp = "";
}
}
temp+=Integer.toString(ip[i]);
}
System.out.println(end[0]+"."+end[1]+"."+end[2]+"."+end[3]);
}
public static void A(){
System.out.println("Basic Details of Class: ");
System.out.println("The IP Address entered is of Class A.");
System.out.println("Default Subnet: 255.0.0.0");
System.out.println("Range: 0.0.0.0 to 127.255.255.255");
}