-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
204 lines (169 loc) · 6.15 KB
/
Copy pathsolution.java
File metadata and controls
204 lines (169 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// import java.util.*;
// abstract class Node implements Comparable<Node> {
// public int frequency;
// public char data;
// public Node left, right;
// public Node(int freq) {
// frequency = freq;
// }
// public int compareTo(Node tree) {
// return frequency - tree.frequency;
// }
// }
// class HuffmanLeaf extends Node {
// public HuffmanLeaf(int freq, char val) {
// super(freq);
// data = val;
// }
// }
// class HuffmanNode extends Node {
// public HuffmanNode(Node l, Node r) {
// super(l.frequency + r.frequency);
// left = l;
// right = r;
// }
// }
// public class Solution {
// public static Map<Character, String> mapA = new HashMap<>();
// public static Node buildTree(int[] charFreqs) {
// PriorityQueue<Node> trees = new PriorityQueue<>();
// for (int i = 0; i < charFreqs.length; i++) {
// if (charFreqs[i] > 0) {
// trees.offer(new HuffmanLeaf(charFreqs[i], (char) i));
// }
// }
// while (trees.size() > 1) {
// Node a = trees.poll();
// Node b = trees.poll();
// trees.offer(new HuffmanNode(a, b));
// }
// return trees.poll();
// }
// public static void printCodes(Node tree, StringBuffer prefix) {
// if (tree instanceof HuffmanLeaf) {
// HuffmanLeaf leaf = (HuffmanLeaf) tree;
// mapA.put(leaf.data, prefix.toString());
// } else if (tree instanceof HuffmanNode) {
// HuffmanNode node = (HuffmanNode) tree;
// prefix.append('0');
// printCodes(node.left, prefix);
// prefix.deleteCharAt(prefix.length() - 1);
// prefix.append('1');
// printCodes(node.right, prefix);
// prefix.deleteCharAt(prefix.length() - 1);
// }
// }
// public static void decodeHuff(Node root, String s) {
// Node current = root;
// StringBuilder decoded = new StringBuilder();
// for (int i = 0; i < s.length(); i++) {
// current = (s.charAt(i) == '0') ? current.left : current.right;
// if (current.left == null && current.right == null) {
// decoded.append(current.data);
// current = root;
// }
// }
// System.out.println(decoded.toString());
// }
// public static void main(String[] args) {
// Scanner input = new Scanner(System.in);
// String test = input.next();
// int[] charFreqs = new int[256];
// for (char c : test.toCharArray()) {
// charFreqs[c]++;
// }
// Node tree = buildTree(charFreqs);
// printCodes(tree, new StringBuffer());
// StringBuffer encoded = new StringBuffer();
// for (int i = 0; i < test.length(); i++) {
// encoded.append(mapA.get(test.charAt(i)));
// }
// decodeHuff(tree, encoded.toString());
// }
// }
import java.util.Scanner;
public class IPSubnettingCalculator
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("IP Subnetting Calculator");
System.out.println("1. Enter IP Address and Subnet Mask");
System.out.println("2. Exit");
System.out.print("Choose an option: ");
int choice = sc.nextInt();
switch(choice)
{
case 1:
{
System.out.print("Enter IP Address (e.g., 192.168.1.1): ");
String ipAddress = sc.next();
System.out.print("Enter Subnet Mask (e.g., 255.255.255.0): ");
String subnetMask = sc.next();
calculateSubnetting(ipAddress, subnetMask);
}
case 2:
{
break;
}
default:
{
System.out.println("Invalid choice.");
}
}
sc.close();
}
public static void calculateSubnetting(String ipAddress, String subnetMask)
{
String[] ip = ipAddress.split("\\.");
String[] mask = subnetMask.split("\\.");
int[] networkId = new int[4];
int[] broadcastId = new int[4];
int[] firstAddress = new int[4];
int[] lastAddress = new int[4];
int totalAddresses, usableAddresses;
for (int i = 0; i < 4; i++)
{
networkId[i] = Integer.parseInt(ip[i]) & Integer.parseInt(mask[i]);
broadcastId[i] = Integer.parseInt(ip[i]) | (255 - Integer.parseInt(mask[i]));
}
firstAddress[0] = networkId[0];
firstAddress[1] = networkId[1];
firstAddress[2] = networkId[2];
firstAddress[3] = networkId[3] + 1;
lastAddress[0] = broadcastId[0];
lastAddress[1] = broadcastId[1];
lastAddress[2] = broadcastId[2];
lastAddress[3] = broadcastId[3] - 1;
totalAddresses = (int) Math.pow(2, getHostBits(mask)) - 2;
usableAddresses = totalAddresses - 2;
System.out.println("Network ID : " + formatAddress(networkId));
System.out.println("Subnet Mask : " + subnetMask);
System.out.println("First Address : " + formatAddress(firstAddress));
System.out.println("Last Address : " + formatAddress(lastAddress));
System.out.println("Broadcast Address : " + formatAddress(broadcastId));
System.out.println("Usable Addresses : " + usableAddresses);
System.out.println("Available Addresses : " + totalAddresses);
System.out.println("Range of Address : " + );
}
public static int getHostBits(String[] mask)
{
int hostBits = 0;
for (int i = 0; i < 4; i++)
{
int maskOctet = Integer.parseInt(mask[i]);
for (int j = 0; j < 8; j++)
{
if ((maskOctet & (1 << (7 - j))) == 0)
{
hostBits++;
}
}
}
return hostBits;
}
public static String formatAddress(int[] address)
{
return address[0] + "." + address[1] + "." + address[2] + "." + address[3];
}
}