@@ -9,8 +9,8 @@ public class VLSM extends Subnetting {
99 public  List <Integer > listHostRequired ;
1010 public  List <Integer > listPrefix ;
1111 public  List <Integer > listJump ;
12-   
13-  public  VLSM   () {
12+ 
13+  public  VLSM () {
1414
1515 listRedIP  = new  ArrayList <>();
1616 listSubredMask  = new  ArrayList <>();
@@ -21,14 +21,14 @@ public VLSM () {
2121
2222 }
2323
24-  public  void  PrefixAndHostRequired   () {
25-  for  (int  o = 0 ; o < listQuantityHost .size (); o ++) {
24+  public  void  PrefixAndHostRequired () {
25+  for  (int  o  =  0 ; o  <  listQuantityHost .size (); o ++) {
2626 int  i  = 1 ;
2727 while  (true ) {
2828 int  formula  = (int ) (Math .pow (2 , i ) - 2 );
2929 if  (formula  >= listQuantityHost .get (o )) {
3030 listHostRequired .add (formula );
31-   
31+ 
3232 listPrefix .add (32  - i );
3333 i  = 1 ;
3434 break ;
@@ -38,7 +38,7 @@ public void PrefixAndHostRequired () {
3838 }
3939 }
4040
41-  public  void  Jumper   () {
41+  public  void  Jumper () {
4242 for  (int  x  = 0 ; x  < listPrefix .size (); x ++) {
4343 int  countZeros  = 1 ;
4444 StringBuilder  strBuilder  = new  StringBuilder ();
@@ -55,23 +55,23 @@ public void Jumper () {
5555 strBuilder .insert (17 , '.' );
5656 strBuilder .insert (26 , '.' );
5757 String [] octects  = strBuilder .toString ().split ("\\ ." );
58-   
58+ 
5959 for  (int  g  = 0 ; g  < octects .length ; g ++) {
6060 if  (octects [g ].contains ("1" ) && octects [g ].contains ("0" )) {
61-  for  (int  j  = 0 ; j  < octects [g ].length ();j ++) {
61+  for  (int  j  = 0 ; j  < octects [g ].length ();  j ++) {
6262 if  (octects [g ].charAt (j ) == '0' ) {
6363 countZeros ++;
6464 }
6565 }
6666 break ;
6767 }
68-   
68+ 
6969 }
70-  listJump .add ((int ) Math .pow (2 ,countZeros ));
70+  listJump .add ((int ) Math .pow (2 ,  countZeros ));
7171 }
7272 }
7373
74-  public  void  FindSubredMask   () {
74+  public  void  FindSubredMask () {
7575 for  (int  q  = 0 ; q  < listPrefix .size (); q ++) {
7676 int  ll  = 1 ;
7777 StringBuilder  strBuilder  = new  StringBuilder ();
@@ -88,74 +88,84 @@ public void FindSubredMask () {
8888 strBuilder .insert (26 , '.' );
8989 String [] splitSubredZeroAndOne  = strBuilder .toString ().split ("\\ ." );
9090 int  firstOctetValue  = Integer 
91-  .parseInt (splitSubredZeroAndOne [0 ],2 );
91+    .parseInt (splitSubredZeroAndOne [0 ],  2 );
9292 int  secondOctetValue  = Integer 
93-  .parseInt (splitSubredZeroAndOne [1 ], 2 );
93+    .parseInt (splitSubredZeroAndOne [1 ], 2 );
9494 int  thirdOctetValue  = Integer 
95-  .parseInt (splitSubredZeroAndOne [2 ], 2 );
96-  int  fourthOctetValue  = Integer .
97-  parseInt (splitSubredZeroAndOne [3 ], 2 );
98-  String  subredMask  = firstOctetValue +"." +secondOctetValue +"." 
99-  +thirdOctetValue +"." +fourthOctetValue ;
95+  .parseInt (splitSubredZeroAndOne [2 ], 2 );
96+  int  fourthOctetValue  = Integer .parseInt (splitSubredZeroAndOne [3 ], 2 );
97+  String  subredMask  = firstOctetValue  + "."  + secondOctetValue  + "." 
98+  + thirdOctetValue  + "."  + fourthOctetValue ;
10099 listSubredMask .add (subredMask );
101100 }
102101 }
103102
104-  public  void  FindRedIP   () {
103+  public  void  FindRedIP () {
105104 int  w  = 0 ;
106105 while  (w  < listSubredMask .size ()) {
107106 String  redIP  = listRedIP .get (w );
108107 String [] splitRedIP  = redIP .split ("\\ ." );
109-  String [] splitSubredMask  = listSubredMask .get (w ).split ("\\ ." );
110-  for  (int  i  = 0 ; i  < splitSubredMask .length ; i ++) {
111-  if  (!splitSubredMask [i ].contains ("255" )) {
112-  int  octectRed  = Integer .parseInt (splitRedIP [i ]);
113-  int  jumper  = listJump .get (w );
114-  int  octectModified  = octectRed  + jumper ;
115-  splitRedIP [i ] = String .valueOf (octectModified );
108+  int  jumper  = listJump .get (w );
109+ 
110+  int [] octets  = new  int [4 ];
111+  for  (int  i  = 0 ; i  < 4 ; i ++) {
112+  octets [i ] = Integer .parseInt (splitRedIP [i ]);
113+  }
114+ 
115+  int  carry  = jumper ;
116+  for  (int  i  = 3 ; i  >= 0 ; i --) {
117+  octets [i ] += carry ;
118+  if  (octets [i ] > 255 ) {
119+  carry  = octets [i ] / 256 ;
120+  octets [i ] = octets [i ] % 256 ;
121+  } else  {
122+  carry  = 0 ;
116123 break ;
117124 }
118125 }
119-  if  (w  < listSubredMask .size ()-1 ) {
120-  String  newNet  = String .join ("." ,splitRedIP );
126+ 
127+  String  newNet  = octets [0 ] + "."  + octets [1 ] + "."  + octets [2 ] + "."  + octets [3 ];
128+ 
129+  if  (w  < listSubredMask .size () - 1 ) {
121130 listRedIP .add (newNet );
122131 }
132+ 
123133 w ++;
124134 }
125135 }
126136
127137 /* 
128138 * Table 
129139 */ 
130-  public  void  Overview   () {
131-  String [][] data  = new  String [listRedIP .size ()+ 1 ][6 ];
132-  data [0 ] = new  String []{ "Subnet" ,"Host" ,"Host Required" ,"Red IP" ,"Prefix" ,"Subred Mask" };
133-  int  num = 1 ;
140+  public  void  Overview () {
141+  String [][] data  = new  String [listRedIP .size () +  1 ][6 ];
142+  data [0 ] = new  String [] {  "Subnet" ,  "Host" ,  "Host Required" ,  "Red IP" ,  "Prefix" ,  "Subred Mask"   };
143+  int  num  =  1 ;
134144 for  (int  k  = 0 ; k  < listRedIP .size (); k ++) {
135-  data [k + 1 ][0 ] = "Subnet " + num ++;
136-  data [k + 1 ][1 ] = listQuantityHost .get (k ).toString ();
137-  data [k + 1 ][2 ] = listHostRequired .get (k ).toString ();
138-  data [k + 1 ][3 ] = listRedIP .get (k ).toString ();
139-  data [k + 1 ][4 ] = listPrefix .get (k ).toString ();
140-  data [k + 1 ][5 ] = listSubredMask .get (k ).toString ();
145+  data [k  +  1 ][0 ] = "Subnet "  +  num ++;
146+  data [k  +  1 ][1 ] = listQuantityHost .get (k ).toString ();
147+  data [k  +  1 ][2 ] = listHostRequired .get (k ).toString ();
148+  data [k  +  1 ][3 ] = listRedIP .get (k ).toString ();
149+  data [k  +  1 ][4 ] = listPrefix .get (k ).toString ();
150+  data [k  +  1 ][5 ] = listSubredMask .get (k ).toString ();
141151 }
142152
143-  int [] columnWidths  = {10 ,5 , 13 ,15 ,6 , 15 };
153+  int [] columnWidths  = {  10 ,  5 ,  13 ,  15 ,  6 ,  15   };
144154 printTable (data , columnWidths );
145155 }
146156
147-  private  String  paddingRight   (String  cell , int  width ) {
148-  return  String .format ("%-" + width + "s" ,cell );
157+  private  String  paddingRight (String  cell , int  width ) {
158+  return  String .format ("%-"  +  width  +  "s" ,  cell );
149159 }
150160
151-  private  void  printLine   (int  width ) {
161+  private  void  printLine (int  width ) {
152162 for  (int  i  = 0 ; i  < width ; i ++) {
153163 System .out .print ("-" );
154164 }
155165 System .out .println ();
156166 }
157167
158-  private  void  printTable   (String [][] data , int [] columnWidths ) {
168+  private  void  printTable (String [][] data , int [] columnWidths ) {
159169 int  totalWidth  = 1 ;
160170 for  (int  w  : columnWidths ) {
161171 totalWidth  += w  + 3 ;
@@ -168,7 +178,7 @@ private void printTable (String[][] data, int[] columnWidths) {
168178 for  (int  j  = 0 ; j  < data [i ].length ; j ++) {
169179 String  cell  = data [i ][j ];
170180 int  width  = columnWidths [j ];
171-  System .out .print (paddingRight (cell , width )+ " | " );
181+  System .out .print (paddingRight (cell , width ) +  " | " );
172182 }
173183 System .out .println ();
174184
0 commit comments