java - IP Program, setting an object to default value -
java - IP Program, setting an object to default value -
i working on assignment , running issue. trying figure out how pc3 object display default values if info passed object invalid (an invalid ip address). is, display showing ip: null , need instead phone call setdefault() method , display that.
i'm sorry long bit of code here want see going on. give thanks much help!
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, x , y values between 0 , 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 network address octet4=randval(); //cannot have69.254.255.255 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 , 255 random rnd1=new random(); int x=(int)(rnd1.nextdouble()*255); homecoming 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"; homecoming retstring; } public boolean checkvalidipclass (string x){ //system.out.println(retstring); checks create sure string made method ( int = 0; < 4; i++) { if (retstring == "a"){ if ((intparts[1] == 0) && (intparts [2] == 0) && (intparts [3] == 0)) homecoming false; if ((intparts[1] == 255) && (intparts [2] == 255) && (intparts [3] == 255)) homecoming false; } if (retstring == "b"){ if ((intparts [2] == 0) && (intparts [3] == 0)) homecoming false; if ((intparts [2] == 255) && (intparts [3] == 255)) homecoming false; } if (retstring == "c"){ if (intparts [3] == 0) homecoming false; if (intparts [3] == 255) homecoming false; } } homecoming true; } public boolean isvalidipaddress(string ip){ /*you need fill in method*/ //check verify 3 dots, values in each octet between 0 , 255 , ip address can assigned //class 1st octet 1-127 , rest cannot 0's or 255's //class b 1st octet 128-191 sec 0-255 , lastly 2 cannot 0's or 255's seek { if (ip == null || ip.isempty()) { homecoming false; } string[] parts = ip.split( "\\." ); if ( parts.length != 4 ) { homecoming false; } intparts = new int[4]; (int j = 0; j < 4; j++ ) { int = integer.parseint( parts[j] ); if ( (i < 0) || (i > 255) ) { homecoming false; } intparts[j] = i; } //ends loop storing values int array if(ip.endswith(".")) { homecoming false; } checkvalidipclass(tellcase()); homecoming true; } grab (numberformatexception nfe) { homecoming false; } } public boolean isvalidsubnetmask(string sm){ /*you need fill in method*/ //only checks default subnet masks (either 255.0.0.0 or 255.255.0.0 or 255.255.255.0). seek { if (sm == null || sm.isempty()) { homecoming false; } string[] parts = sm.split( "\\." ); if ( parts.length != 4 ) { homecoming false; } if ((sm != "255.0.0.0") && (sm != "255.255.0.0") && (sm != "255.255.255.0")) { homecoming false; } if(sm.endswith(".")) { homecoming false; } homecoming true; } grab (numberformatexception nfe) { homecoming false; } } public int stringtoint(string v){ boolean answer=true; int returnval=-1; for(int i=0; i<v.length(); i++){ //verify characters in v digits if(!character.isdigit(v.charat(i))){ //as 1 non-digit found, end loop know v not number answer=false; i=v.length()+1; } } if(answer){ //if v number, alter number returnval=integer.parseint(v); } homecoming returnval; //if -1, v not number. otherwise, v number } public string getipaddress(){ homecoming ipaddress; } public string getsubnetmask(){ homecoming subnetmask; } public string tostring(){ string returnstring="ip address: "+ipaddress+"\n"; returnstring+="subnet mask: "+subnetmask+"\n"; homecoming returnstring; } public static void main(string[] args) { //sample creation of objects of 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 appear. //sample output creation of objects of class system.out.println(pc1); system.out.println(pc2); system.out.println(pc3); //should end printing in 169.254.x.y range 1 time have created check methods. system.out.println(pc1.stringtoint("110")); //way of testing see how individual methods work system.out.println(pc1.stringtoint("a")); //prints -1 beacause not number system.out.println(pc1.stringtoint("110d")); //prints -1 because d isn't digit system.out.println(pc1.stringtoint("13e5")); //prints -1 because e isn't digit } } //end class
so phone call setdefault() instead of
system.out.println("invalid ip address: "+ip); inside setipaddress method.
also setdefault has bug, in while loop regenerate octe4 value not octed3, if octed3 not valid never terminate.
java
Comments
Post a Comment