Write a java program that classifies IP Classes from Classes A-E using a 32-bit
ID: 3674166 • Letter: W
Question
Write a java program that classifies IP Classes from Classes A-E using a 32-bit integer. Class A ranges from 0-127. Class B ranges from 128-191. Class C ranges from 192-223. Class D ranges from 224-239. Class E ranges from 240-255. Negative integers are not allowed nor floating point numbers. (The code should include statements performing those incorrect inputs.) The program should also printout what network class (A-E) the IP address is in, the number of network bits needed, the number of networks (for ex: 2^8 - 2 for 8 network bits), the host bits, and number of hosts/Network (for ex: 2^24 -2 for 24 hosts bits.
Explanation / Answer
Hi, Please find my code. It might be that formula to calculate Number of HostId and NetId will be different what i have used, but that you can do easily if you know your formula.
import java.util.Scanner;
public class IpClassIdentifier {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int ip;
System.out.print("Please Enter first 8-bit Ip Value(-999 to stop): ");
ip = sc.nextInt();
while(ip != -999){
if((ip<0) || (ip>255))
System.out.println("Invalid Ip Address");
else if(ip>=0 && ip<=127){
System.out.println("Ip belongs to Class A");
System.out.println("Number of Host Id: "+((int)Math.pow(2, 8)-2)+
", and Number of Network Id: "+((int)Math.pow(2, 24)-2));
}else if(ip>=128 && ip<=191){
System.out.println("Ip belongs to Class B");
System.out.println("Number of Host Id: "+((int)Math.pow(2, 16)-2)+
", and Number of Network Id: "+((int)Math.pow(2, 16)-2));
}else if(ip>=192 && ip<=223){
System.out.println("Ip belongs to Class C");
System.out.println("Number of Host Id: "+((int)Math.pow(2, 24)-2)+
", and Number of Network Id: "+((int)Math.pow(2, 8)-2));
}else if(ip>=224 && ip<=239){
System.out.println("Ip belongs to Class D");
System.out.println("Number of Host Id and Number of Network Id has not defined for D");
}else if(ip>=240 && ip<=255){
System.out.println("Ip belongs to Class E");
System.out.println("Number of Host Id and Number of Network Id has not defined for E");
}
System.out.print("Please Enter first 8-bit Ip Value(-999 to stop): ");
ip = sc.nextInt();
}
sc.close();
}
}
/*
Output:
Please Enter first 8-bit Ip Value(-999 to stop): 234
Ip belongs to Class D
Number of Host Id and Number of Network Id has not defined for D
Please Enter first 8-bit Ip Value(-999 to stop): 123
Ip belongs to Class A
Number of Host Id: 254, and Number of Network Id: 16777214
Please Enter first 8-bit Ip Value(-999 to stop): 567
Invalid Ip Address
Please Enter first 8-bit Ip Value(-999 to stop): -999
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.