我正在做作业,遇到了问题。我试图弄清楚如果传递给该对象的信息无效(无效的IP地址)如何使PC3对象显示默认值。照原样,显示器只是显示IP:null,我需要获取它来调用setDefault()
方法并显示它。
很抱歉,这里有很多代码,但我希望您看到正在发生的事情。
非常感谢您的帮助!
import java.util.Random;
public class IPAddress
{
private String IPAddress;
private String subnetMask;
private int intParts[];
private String retString;
IPAddress(){ //sets default IP address in 169.254.X.Y, where X and Y are values between 0 and 255
setDefault();
}
IPAddress(String ip, String sm){
setIPAddress(ip);
setSubnetMask(sm);
}
public void setDefault(){
String temp="169.254.";
int octet3=randVal();
int octet4=randVal();
while((octet3==0 && octet4==0) || (octet3==255 && octet4==255)){ //cannot have 169.254.0.0 as it is a network address
octet4=randVal(); //cannot have69.254.255.255 as it is a broadcast address
}
temp=temp+octet3+"."+octet4;
setIPAddress(temp);
setSubnetMask("255.255.0.0");
}
public void setIPAddress(String ip){
if(isValidIPAddress(ip)){
IPAddress=ip;
}
else{
System.out.println("Invalid IP Address: "+ip);
}
}
public void setSubnetMask(String sm){
if(isValidSubnetMask(sm)){
subnetMask=sm;
}
else{
System.out.println("Invalid Subnet Mask: "+sm);
}
}
public int randVal(){
//value between 0 and 255
Random rnd1=new Random();
int x=(int)(rnd1.nextDouble()*255);
return x;
}
public String tellCase(){
//String retString = "";
if ((intParts[0] > 0) && (intParts[0] <= 128))
retString = "A";
else if ((intParts[0] >= 127) && (intParts[0] <= 191))
retString = "B";
else if ((intParts[0] >=192) && (intParts[0] <= 223))
retString = "C";
else if ((intParts[0] >= 224) && (intParts[0] <= 239))
retString = "D";
return retString;
}
public boolean checkValidIPClass (String x){
//System.out.println(retString); checks to make sure the string made it to the method
for ( int i = 0; i < 4; i++) {
if (retString == "A"){
if ((intParts[1] == 0) && (intParts [2] == 0) && (intParts [3] == 0))
return false;
if ((intParts[1] == 255) && (intParts [2] == 255) && (intParts [3] == 255))
return false;
}
if (retString == "B"){
if ((intParts [2] == 0) && (intParts [3] == 0))
return false;
if ((intParts [2] == 255) && (intParts [3] == 255))
return false;
}
if (retString == "C"){
if (intParts [3] == 0)
return false;
if (intParts [3] == 255)
return false;
}
}
return true;
}
public boolean isValidIPAddress(String ip){ /*YOU NEED TO FILL IN THE METHOD*/
//check to verify only 3 dots, the values in each octet are between 0 and 255 and is an IP address which can be assigned
//Class a 1st octet 1-127 and the rest cannot be 0's or 255's
//Class b 1st octet 128-191 second 0-255 and last two cannot be all 0's or 255's
try {
if (ip == null || ip.isEmpty()) {
return false;
}
String[] parts = ip.split( "\\." );
if ( parts.length != 4 ) {
return false;
}
intParts = new int[4];
for (int j = 0; j < 4; j++ ) {
int i = Integer.parseInt( parts[j] );
if ( (i < 0) || (i > 255) ) {
return false;
}
intParts[j] = i;
} //ends for loop for storing values into int array
if(ip.endsWith(".")) {
return false;
}
checkValidIPClass(tellCase());
return true;
} catch (NumberFormatException nfe) {
return false;
}
}
public boolean isValidSubnetMask(String sm){ /*YOU NEED TO FILL IN THE METHOD*/
//only checks for default subnet masks (either 255.0.0.0 or 255.255.0.0 or 255.255.255.0).
try {
if (sm == null || sm.isEmpty()) {
return false;
}
String[] parts = sm.split( "\\." );
if ( parts.length != 4 ) {
return false;
}
if ((sm != "255.0.0.0") && (sm != "255.255.0.0") && (sm != "255.255.255.0")) {
return false;
}
if(sm.endsWith(".")) {
return false;
}
return true;
} catch (NumberFormatException nfe) {
return false;
}
}
public int stringToInt(String v){
boolean answer=true;
int returnVal=-1;
for(int i=0; i<v.length(); i++){ //verify all characters in v are digits
if(!Character.isDigit(v.charAt(i))){ //as soon as 1 non-digit is found, end the loop as we know v is not a number
answer=false;
i=v.length()+1;
}
}
if(answer){ //if v is a number, then change it to its number
returnVal=Integer.parseInt(v);
}
return returnVal; //if this is -1, then v is not a number. otherwise, v is a number
}
public String getIPAddress(){
return IPAddress;
}
public String getSubnetMask(){
return subnetMask;
}
public String toString(){
String returnString="IP Address: "+IPAddress+"\n";
returnString+="Subnet Mask: "+subnetMask+"\n";
return returnString;
}
public static void main(String[] args)
{
//sample creation of objects of the class
IPAddress PC1=new IPAddress(); //uses default constructor
IPAddress PC2=new IPAddress("165.0.0.0", "255.255.0.0"); //uses alternate constructor (overloaded)
IPAddress PC3=new IPAddress("122..15.12.1", "255.0.0.0"); //uses alternate constructor (overloaded) - should cause errors messages to appear.
//sample output from creation of objects of the class
System.out.println(PC1);
System.out.println(PC2);
System.out.println(PC3); //should end up printing something in 169.254.X.Y range once you have created the check methods.
System.out.println(PC1.stringToInt("110")); //way of testing to see how individual methods work
System.out.println(PC1.stringToInt("a")); //prints -1 beacause a is not a number
System.out.println(PC1.stringToInt("110d")); //prints -1 because d isn't a digit
System.out.println(PC1.stringToInt("13e5")); //prints -1 because e isn't a digit
}
} //end class
最佳答案
因此,呼叫setDefault()
而不是
System.out.println("Invalid IP Address: "+ip);
在您的
setIPAddress
方法中。另外,您的setDefault有一个错误,
在while循环中,仅重新生成octe4值,而不重新生成octed3,因此,如果octed3无效,它将永远不会终止。