Hello, my assignment was to create two classes, digit, and IP,
the function of digit is beeing well.. a digit, and ip needs to check if the ip is valid(digits are >= 0, <=255.
I got it working, however i'm wondering if it could be improved, since i need to type digit1.digit to access them...
code follows below.
Fyi, I asked a couple of questions on stackoverflow, following their rules, they insta banned me for life...
hope this isn't alike
public class IP { private Digit digit1; private Digit digit2; private Digit digit3; private Digit digit4; public IP() { } public void setIPAddress(int digitA,int digitB, int digitC, int digitD) { digit1 = new Digit(digitA); digit2 = new Digit(digitB); digit3 = new Digit(digitC); digit4 = new Digit(digitD); System.out.println(digit1.digit + "." + digit2.digit + "." + digit3.digit + "." + digit4.digit); } public Digit getDigit1() { return digit1; } public void getIPAddress() { } public boolean isValid() { Boolean isValid = false; if(digit1.digit >= 0 && digit2.digit >= 0 && digit3.digit >= 0 && digit4.digit >= 0) { if(digit1.digit <= 255 && digit2.digit <= 255 && digit3.digit <= 255 && digit4.digit <= 255) { System.out.println("IP Adress " + digit1.digit + "." + digit2.digit + "." +digit3.digit + "." + digit4.digit + " is valid."); isValid = true; } else{System.out.println("Invalid adress");} } return isValid; } }
public class Digit { public int digit; public Digit(int newDigit) { digit = newDigit; } public void setDigit(int digit) { this.digit = digit; } public int getDigit() { return digit; } public void g() { System.out.println("bla " + digit); } }