Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 980173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:20:15+00:00 2026-05-16T04:20:15+00:00

I am trying to write public instance method createParther() that returns an instance of

  • 0

I am trying to write public instance method createParther() that returns an instance of Couple. The method does its job by following these rules: one dancer in the couple must be from aList and the other bList, one dancer in the couple must be male and the other female. Neither dancer in the couple should be partnered already. If it is not possible to create a couple from amongst the unpartnered dancers, then null should be returned. If an instance of Couple is successfully created, both dancers involved in the couple should have their partnered instance variables set to true. I have attempted to list all the aList and bList to together, but then I didn’t know to check the requirements as per above. Can anyone demostrate how this could be acheieved? This is not assignment.

public class FoxDancing
{
 private List<Couple> coupleList;
 private List<Dancer> aList;
 private List<Dancer> bList;

public FoxDancing()
{
   couplesList = new ArrayList<Parthers>();
   aList = new ArrayList<Dancer>();
   bList = new ArrayList<Dancer>();
}

public void fieldLists()
{
   this.addX("Simon","AList",'m');
   this.addX("Jason","AList",'m');
   this.addX("Ian","AList",'m');
   this.addX("Susan","BList",'f');
   this.addX("Helena","BList",'f');
   this.addX("Gina","BList",'f');
}
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:20:16+00:00Added an answer on May 16, 2026 at 4:20 am

    It looks like you’d want to have a helper method like this:

    Dancer findUnpartnered(List<Dancer> list) {
       for (Dancer d : list) {
          if (d.isUnpartnered()) {
              return d;
          }
       }
       return null;
    }
    

    This uses a “foreach loop” for a concise, readable iteration over all Dancer in the List<Dancer>.

    Then you can write something like this:

    Couple createCouple() {
       Dancer a = findUnpartnered(aList);
       Dancer b = findUnpartnered(bList);
       if (a == null || b == null) {
          return null;
       } else {
          a.setPartnered(true);
          b.setPartnered(true);
          return new Couple(a, b);
       }
    }
    

    While this should work, note that findUnpartnered is a O(N) linear search. If the list is of any considerable length, consider having alternative data structures, e.g. a Set<Dancer> that partitions the list into unpartnered and partnered subsets.


    On generics invariance

    You’ve written the following:

    // snippet from original code
    private List<Couple> coupleList;
    //...
    couplesList = new ArrayList<Parthers>(); // DOES NOT COMPILE!
    

    This will not compile. A List<Partner> (assuming this is what you meant to write) is NOT a List<Couple>. Perhaps you want a new ArrayList<Couple>, or if Partner is a subtype of Couple, then perhaps you want a List<? extends Couple> coupleList.

    Related questions

    • What is the difference between <E extends Number> and <Number>?

    See also

    • Java Tutorials/Generics and Subtyping and More Fun With Wildcards
    • Angelika Langer’s Java Generics FAQ – What is a bounded wildcard?

    On enum

    You’ve written the following:

    // snippet from original code
    this.addX("Simon","AList",'m');
    this.addX("Susan","BList",'f');
    // potentially "bad" use of String and char constants!
    

    I don’t know much about Foxtrot dancing, but if there’s a conceptual A-list and B-list , then you may consider using an enum instead of String markers "AList" and "BList". Similarly, if there can only be male or female dancers, then enum would be much better than 'm' and 'f'.

    enum FoxTrotter { A, B; }
    enum Sex { MALE, FEMALE; }
    

    See also

    • Java Language Guide/Enums

    Related questions

    • Enumerations: Why? When?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a method that does the following: Replace every occurrence of
I'm trying to write a method like this: public static T Test<T>() { if
Trying to write a couple of functions that will encrypt or decrypt a file
I'm trying to write an extension method that will give me the MemberInfo representing
I'm trying to write a generic method to serialize an object that inherits from
I'm trying to write a test for this class its called Receiver : public
I'm trying to write a method that will get a private field in a
I'm trying to write an extension method that, given a value, will return The
I'm trying to write an extension method for System.Web.UI.Control which will search its ControlCollection
I am trying to write a unit test for the following class: @Transactional public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.