Results 1 to 9 of 9
Thread: help with a few basic methods
- 11-27-2012, 03:58 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
help with a few basic methods
Hello, I got another class assignment this week and I´m 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; } }Now I am suppossed to write method setPartner, here is what I have so far and I really ain´t sure about it:Java Code:public class DancerException extends Exception { public DancerException() { super(); } public DancerException(String message) { super(message); } }
}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.
The problem is line 34 - the compiler says "cannot find symbol - method setPartner(Dancer)" although the method is in the same package.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 } }
Thank you for any tips or advice.
- 11-27-2012, 04:15 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 11-27-2012, 04:26 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
Re: help with a few basic methods
That should not be a problem considering they are in the same package, am I right?
- 11-27-2012, 04:31 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 11-27-2012, 04:46 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
Re: help with a few basic methods
I tried to create an object to work against:
Still the same error, what am I doing wrong?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; }
- 11-27-2012, 04:58 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: help with a few basic methods
Here you are not using any object to call this method, so the compiler assumes you mean 'this.setPartner()'.Java Code:setPartner(man);
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.
- 11-27-2012, 05:21 PM #7
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
Re: help with a few basic methods
Oh, sorry about that. The setPartner method is a part of the Dancer class.
- 11-27-2012, 05:47 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
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.Java Code:woman.setPartner(man);
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:
Is setting the current objects partner to the parameter (which is great), but then setting the parameters 'partner' variable to itself...which is wrong.Java Code:this.partner = partner; partner.partner = partner;
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.
- 11-27-2012, 07:26 PM #9
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
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:
And I deal with those exceptions in the other 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; }
Thanks again.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; }Last edited by dawnMist; 11-27-2012 at 09:55 PM.
Similar Threads
-
Why and where abstract methods & classes and static methods are used?
By ajaysharma in forum Advanced JavaReplies: 2Last Post: 07-12-2012, 11:04 PM -
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
difference between static methods and instantce methods?
By venkatch in forum New To JavaReplies: 1Last Post: 10-23-2011, 12:37 PM -
need help learning methods and fixing my basic program
By shazakala in forum New To JavaReplies: 4Last Post: 03-21-2011, 09:12 AM -
some basic advice on using methods please
By sonny in forum New To JavaReplies: 2Last Post: 03-03-2010, 12:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks