Results 1 to 9 of 9
  1. #1
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default help with a few basic methods

    Hello, I got another class assignment this week and Im trying to make it work.
    It is a little application that should establish or disjoin dancing couples and create dancing lessons.

    Java Code:
    public class Dancer
    {
        private Dancer partner;
        private boolean female;
        private int number;
    
        /**
         * @param number Dancer's number
         * @param isFemale Dancer's genre
         */
        public Dancer(int number, boolean isFemale) {
            this.number = number;
            female = isFemale;
        }
        
        /**
         * @return true if the dancer has a partner
         */
        public boolean hasPartner() {
            return partner != null;
        }
        
        /**
         * @return true if the dancer is female
         */
        public boolean isFemale() {
            return female;
        }    
    
        /**
         * @return true if the dancer is male
         */
        public boolean isMale() {
            return !female;
        }  
        
        /**
         * @return dancer's number
         */
        public int getNumber() {
            return number;
        }
    
        /**
         * Breaks the dancing pair, i.e. unsets partner of this dancer and vice versa.
         * 
         * @return false if this dancer had no partner.
         */
        public boolean unsetPartner() {
            if (!hasPartner()) return false;
            partner.partner = null;
            partner = null;
            return true;
        }
    }
    Java Code:
    public class DancerException extends Exception
    {
        public DancerException() {
            super();
        }
        
        public DancerException(String message) {
            super(message);
        }
    }
    Now I am suppossed to write method setPartner, here is what I have so far and I really aint sure about it:

    Java Code:
        /**
        * Creates dancing pair. This method assignes the given dancer as a partner 
        * of this dancer and vice versa.
        * 
        * @param partner Person to be assigned as our partner in dancing pair
        * @throws NullPointerException if the partner parametr is null
        * @throws DancingException if either this dancer or the given partner are alredy in a pair 
        *        (they already have a partner)
        * @throws DancingException if this dancer and the given partner are of the same genre
        */  
        
        public void setPartner(Dancer partner) throws NullPointerException, DancerException {
            if(partner == null) throw new NullPointerException("partner");
            
            try {
                this.partner = partner;
                partner.partner = partner;
            } catch (Exception e) {
                if(partner != null )throw new DancerException("Dancer already has a partern.");
                if(partner.female == partner.partner.female) throw new DancerException("Dancers have the same gender.");
            }
    }

    Finally I should create a class for dancing lessons that consist of dancers.

    Java Code:
    public class DancingLesson
    {
        private Dancer[] dancers;
        
        public DancingLesson(Dancer[] dancers) {
            if(dancers == null) throw new NullPointerException("dancers");
            this.dancers = new Dancer[dancers.length];
            for(int i = 0; i < dancers.length; i++) {
                if(dancers[i] == null) throw new NullPointerException("dancers [" + i + "]");
                else this.dancers[i] = dancers[i];
            }
        }
        
        public Dancer getDancer(int number) {
            Dancer result = null;
            for(int i = 0; i < this.dancers.length; i++) {
                if(this.dancers[i].getNumber() == number) result = dancers[i];
            }
            return result;
        }
        
        public boolean pair(int ladie, int gentleman) {
            // if they are the same gender
            if(getDancer(ladie).isMale() == getDancer(gentleman).isMale()) return false;
            
            // if the number does not exist
            for(int i = 0; i < dancers.length; i++) {
                if( (ladie != dancers[i].getNumber()) && (gentleman != dancers[i].getNumber()) ) return false;
            }
            
            // if one of them already has a pair
            if( (getDancer(ladie).hasPartner() == true) || (getDancer(ladie).hasPartner() == true) ) return false;
            
            setPartner(getDancer(ladie));        
            return true;
        
        public boolean disjoin(int dancer) {
            // TODO
        }
    }
    The problem is line 34 - the compiler says "cannot find symbol - method setPartner(Dancer)" although the method is in the same package.

    Thank you for any tips or advice.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: help with a few basic methods

    There is no setPartner method in the DancingLesson class.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: help with a few basic methods

    That should not be a problem considering they are in the same package, am I right?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: help with a few basic methods

    No.
    Java does not work like that.
    You either have to give it an object to work against (eg someList.get(0)), or, for a static method, a class (eg Math.random()).
    Inside a method, a call without an associated object is treated as calling a method on 'this' (ie the current object).

    So in your case that call to setPartner is translated as this.setPartner().
    And 'this' (ie the DancingLesson object) has no setPartner method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: help with a few basic methods

    I tried to create an object to work against:
    Java Code:
        public boolean pair(int ladie, int gentleman) {
            Dancer man = new Dancer(gentleman, false);
            Dancer woman = new Dancer(ladie, true);
            
            // if they are the same gender
            if(getDancer(ladie).isMale() == getDancer(gentleman).isMale()) return false;
            
            // if the number does not exist
            for(int i = 0; i < dancers.length; i++) {
                if( (ladie != dancers[i].getNumber()) && (gentleman != dancers[i].getNumber()) ) return false;
                if(ladie == dancers[i].getNumber()) woman = dancers[i];
                if(gentleman == dancers[i].getNumber()) man = dancers[i];
            }
            
            // if one of them already has a pair
            if( (getDancer(ladie).hasPartner() == true) || (getDancer(ladie).hasPartner() == true) ) return false;
            
            setPartner(man);
    
            return true;
        }
    Still the same error, what am I doing wrong?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: help with a few basic methods

    Java Code:
    setPartner(man);
    Here you are not using any object to call this method, so the compiler assumes you mean 'this.setPartner()'.
    Since 'this' is the current object, which is a DancingLesson object, then it gives you the error, since DancingLesson has no setPartner method.

    I have no idea what class has that method, so I cannot say what should appear there instead.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: help with a few basic methods

    Oh, sorry about that. The setPartner method is a part of the Dancer class.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: help with a few basic methods

    So, since you are passing the 'man' in as a parameter, I'm going to guess that this should be called on the 'woman'.
    No point making the man partner himself after all.
    Java Code:
    woman.setPartner(man);
    Looking in the setPartner it is on the right track, but you can get rid of the try/catch. It's not doing anything, and is actually preventing those lines in the catch block from executing, and you want them to as they check the data is valid. In fact they should probably be before you set the 'partner' values, as you only want to set them if those check are correct.
    The other thing is you have to use 'this' in there each time you are referring to the 'partner' variable that is part of the object, and not the parameter. That is the only way you can tell the compiler which one you mean. You do it once already, but you need to ensure you do it elsewhere in that method as well.
    For example:
    Java Code:
    this.partner = partner;
    partner.partner = partner;
    Is setting the current objects partner to the parameter (which is great), but then setting the parameters 'partner' variable to itself...which is wrong.
    The result of this is that woman.setPartner(man) will make the man.partner == man.
    It should read:
    Java Code:
    this.partner = partner;
    partner.partner = this;
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: help with a few basic methods

    Ok, I finally got it right. Thank you very much for your help.
    I corrected the setPartner method like this:

    Java Code:
        public void setPartner(Dancer partner) throws NullPointerException, DancerException {
            if(partner == null) throw new NullPointerException("partner");
            
            if( (partner.hasPartner() != false) || (this.hasPartner() != false) )
                throw new DancerException("Dancer already has a partner.");
            
            if(this.isMale() == partner.isMale()) throw new DancerException("Dancers have the same gender.");
               
            this.partner = partner;
            partner.partner = this; 
        }
    And I deal with those exceptions in the other method like this:

    Java Code:
        public boolean pair(int ladie, int gentleman) {  
            try {
                getDancer(ladie).setPartner(getDancer(gentleman));
            } catch (NullPointerException e) {
                return false;
            } catch (DancerException e) {
                e.getMessage();
                return false;
            }
            return true;
        }
    Thanks again.
    Last edited by dawnMist; 11-27-2012 at 09:55 PM.

Similar Threads

  1. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  3. Replies: 1
    Last Post: 10-23-2011, 12:37 PM
  4. need help learning methods and fixing my basic program
    By shazakala in forum New To Java
    Replies: 4
    Last Post: 03-21-2011, 09:12 AM
  5. some basic advice on using methods please
    By sonny in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 12:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •