Advertisement
Eternoseeker

ass3subnet

Jun 7th, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | Source Code | 0 0
  1. import java.util.Scanner;
  2.  
  3. class ass3subnet {
  4.     public static void main(String args[]) {
  5.         try (Scanner sc = new Scanner(System.in)) {
  6.             System.out.print("Enter the ip address: ");
  7.             String ip = sc.nextLine();
  8.             String split_ip[] = ip.split("\\.");
  9.             String split_bip[] = new String[4]; // split binary ip
  10.             String bip = "";
  11.             for (int i = 0; i < 4; i++) {
  12.                 split_bip[i] = appendZeros(Integer.toBinaryString(Integer.parseInt(split_ip[i])));
  13.                 bip += split_bip[i];
  14.             }
  15.             System.out.println("IP in binary is " + bip);
  16.             System.out.print("Enter the number of addresses: ");
  17.             int n = sc.nextInt();
  18.             // Calculation of mask
  19.             int bits = (int) Math.ceil(Math.log(n) / Math.log(2));
  20.             System.out.println("Number of bits required for address = " + bits);
  21.             int mask = 32 - bits;
  22.             System.out.println("The subnet mask is = " + mask);
  23.             // Calculation of first address and last address
  24.             int fbip[] = new int[32];
  25.             for (int i = 0; i < 32; i++)
  26.                 fbip[i] = (int) bip.charAt(i) - 48;
  27.             for (int i = 31; i > 31 - bits; i--)
  28.                 fbip[i] &= 0;
  29.             String fip[] = { "", "", "", "" };
  30.             for (int i = 0; i < 32; i++)
  31.                 fip[i / 8] = new String(fip[i / 8] + fbip[i]);
  32.             System.out.print("Subnet address is = ");
  33.             for (int i = 0; i < 4; i++) {
  34.                 System.out.print(Integer.parseInt(fip[i], 2));
  35.                 if (i != 3)
  36.                     System.out.print(".");
  37.             }
  38.             System.out.println();
  39.             int lbip[] = new int[32];
  40.             for (int i = 0; i < 32; i++)
  41.                 lbip[i] = (int) bip.charAt(i) - 48;
  42.             for (int i = 31; i > 31 - bits; i--)// Get last address by ORing last n bits with 1
  43.                 lbip[i] |= 1;
  44.             String lip[] = { "", "", "", "" };
  45.             for (int i = 0; i < 32; i++)
  46.                 lip[i / 8] = new String(lip[i / 8] + lbip[i]);
  47.             System.out.print("Broadcast address is = ");
  48.             for (int i = 0; i < 4; i++) {
  49.                 System.out.print(Integer.parseInt(lip[i], 2));
  50.                 if (i != 3)
  51.                     System.out.print(".");
  52.             }
  53.         } catch (NumberFormatException e) {
  54.             e.printStackTrace();
  55.         }
  56.         System.out.println();
  57.     }
  58.  
  59.     static String appendZeros(String s) {
  60.         String temp = new String("00000000");
  61.         return temp.substring(s.length()) + s;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement