Site Loader
Rock Street, San Francisco

import java.util.Scanner; //scanner importedpublic class Subnet {        public static void main(String args){                Scanner sc = new Scanner(System.in);                System.out.print(“Enter the ip address: “); //get the ip address        String ip = sc.nextLine();                String split_ip = ip.split(“.”); //SPlit the string after every .        String split_bip = new String4; //split binary ip        String bip = “”;                for(int i=0;i<4;i++){            split_bipi = appendZeros(Integer.toBinaryString(Integer.parseInt(split_ipi))); // "19" => 19 => 10011 => 00010011            bip += split_bipi; // then add it to the atring        }                System.out.println(“IP in binary is “+bip); // show them up here                System.out.print(“Enter the number of addresses: “);        int n = sc.nextInt();//get the number of addresses                //Calculation of mask        int bits = (int)Math.ceil(Math.log(n)/Math.log(2));         //for Example : if n = 120, log 120/log 2 gives log to the base 2 => 6.9068 >> = 7        System.out.println(“Number of bits required for address = “+bits);                int mask = 32-bits;        System.out.println(“The subnet mask is = “+mask); //get the mask by 32- num of bits                //Calculation of first address and last address        int fbip = new int32;        for(int i=0; i<32;i++)             fbipi = (int)bip.charAt(i)-48; //convert cahracter 0,1 to integer 0,1                for (int i = 31; i > 31-bits; i–)            fbipi &= 0;                   //Get first address by ANDing last n bits with 0                String fip = {“”,””,””,””};       // make empty array of strings of 4 elements to store each part of ip                for(int i=0;i<32;i++)            fipi/8 = fipi/8+fbipi;            //one by one                System.out.print("First address is = ");                // show the first address                for(int i=0;i<4;i++){            System.out.print(Integer.parseInt(fipi,2));            if(i!=3)                 System.out.print(".");                          //show them one by one        }                System.out.println();                       // new line                //then calculate the last add                int lbip = new int32;        for(int i=0; i<32;i++)             lbipi = (int)bip.charAt(i)-48; //convert cahracter 0,1 to integer 0,1                for (int i = 31; i > 31-bits; i–)            lbipi |= 1;                       //Or it with 1                String lip = {“”,””,””,””};                           //empty arr to store ip parts                for(int i=0;i<32;i++)            lipi/8 = lipi/8+lbipi;                     // go one by one                 System.out.print("Last address is = ");             //show the last add                for(int i=0;i<4;i++){            System.out.print(Integer.parseInt(lipi,2));            if(i!=3)                System.out.print(".");                                   // go one by one showing them        }        System.out.println();    }    //function to append some zeros to complete 8 digit     static String appendZeros(String s){        String temp = "00000000";        return temp.substring(s.length())+ s;    }}

Post Author: admin

x

Hi!
I'm Eric!

Would you like to get a custom essay? How about receiving a customized one?

Check it out