Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: using a seperate class(newb)

  1. #1
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default using a seperate class(newb)

    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);
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: using a seperate class(newb)

    Where is the logic that checks the validity of a Digit?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: using a seperate class(newb)

    isvalid()?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: using a seperate class(newb)

    And it works, right? Then I wouldn't be too worried about it, unless your instructions include other stuff you haven't done.

    You could also move the logic into the Digit class, for example in another isValid() function that checks a single Digit's validity. Then from the IP class isValid() function, just call the isValid() function of each Digit.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Time4Java (May 20th, 2014)

  6. #5
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: using a seperate class(newb)

    Allright thanks yeah it works/all instructions done.

Similar Threads

  1. Java Newb...forum newb also
    By ElTucan831 in forum Member Introductions
    Replies: 2
    Last Post: December 22nd, 2012, 06:08 PM
  2. toString not printing everything in the Array which is in a seperate class
    By taz_1891 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 30th, 2012, 11:51 AM
  3. WindowBuilder - How do I open a seperate class window by a button?
    By tizen in forum Java Theory & Questions
    Replies: 1
    Last Post: July 29th, 2012, 05:11 PM
  4. Switching between frames from a seperate class
    By kurt-hardy in forum AWT / Java Swing
    Replies: 4
    Last Post: February 14th, 2011, 04:19 AM
  5. [SOLVED] newb help..
    By Hallowed in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 05:15 PM

Tags for this Thread