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

  • Home
  • SEARCH
  • 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 8458989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:13:35+00:00 2026-06-10T13:13:35+00:00

The Setup My Java code contains two objects: a Group and a Person. Each

  • 0

The Setup

My Java code contains two objects: a Group and a Person. Each Group can contain references to multiple Person’s, but each Person can only belong to one Group.

The colour of each Person’s sweatshirt is derived from their Group’s colour. (Note: If they don’t belong to a Group, their sweater is gray.)

When I draw a Person on-screen, I take in the Person themselves and a Color.

The Code

Person

public class Person {
    Color sweatshirtColor = Color.gray;
    Group belongsToGroup = null;

    public void setGroup(Group g) { belongsToGroup = g; }
}

Group

public class Group {
    Color color = Color.red;
    List<Person> people = new ArrayList<Person>();
    // ... setters for adding and getting people
}

Draw

public void draw(Person dudette, Color shirtColor) { //... }

The Problem
Group and Person both reference each other – I’m afraid that one might get out of sync with the other. My draw code has to be fixed (because it’s external), but is there anything obvious I’m doing wrong in this design?

EDIT: One more note – Groups can change throughout the program. A Person’s Group depends on their location so they are constantly switching Groups.

  • 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-06-10T13:13:36+00:00Added an answer on June 10, 2026 at 1:13 pm

    You could introduce setters to each class that will allow either entity to modify the other and itself so that they never get out of synch. E.g.

    class Group {
        List<Person> people;
        void removePerson(Person p) {
            if(people.remove(p))
                p.removeFromGroup();
        }
    }
    
    class Person {
        Group grp;
        void removeFromGroup() {
            grp.removePerson(this);
            grp = null;
        }
    }
    

    You can do the same for adding group members.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you blend Java and native OpenGL calls. For instance having the setup happen
I have a class that contains two arraylists which I'm trying to store objects
I have a multi-module Maven setup with Java code. My unit tests, in one
i am writing java code to create a tabs.i have done that.but now i
I've set up the following code running with Java: BitSet bitSet = BitSet.valueOf(new byte[]
I need to setup NACHOS java version in Linux and run some simple sample
After migrating my whole setup from Java 1.5 to 1.6 (J2EE, Tomcat) a couple
I have to setup a basic JAVA application including a GUI based on swing.
Have anyone successfully managed to setup a combined Java/C++ project for Eclipse? What I
I was wondering if it's possible to have a java package setup using a

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.