Can someone please add conditional statements for each to check to see these thr
ID: 3695015 • Letter: C
Question
Can someone please add conditional statements for each to check to see these three things :
1. if the number entered is a negative number (less than 0)
2. is a floating point number (or decimal)
3. is the number greater than 255 (for Class E)
and if so to print out an error for each case for the following Java code below:
import java.util.Scanner; public class IpClassIdentifier { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here double InvalidInputException; 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(); } }
Explanation / Answer
Answer for Question:
This below code is updated with handling negtive or greater than 255 and floating pointers numbers are invalid.
import java.util.Scanner;
import java.lang.Math;
public class IpClassIdentifier
{ /** * @param args the command line arguments */
public static void main(String[] args)
{
// TODO code application logic here double InvalidInputException;
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 == x==Math.round(x))
{
System.out.println("Invalid Ip Address");
}
else if(ip == x==Math.ceil(x))
{
System.out.println("Invalid Ip Address");
}
else 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();
}
}