Results 1 to 15 of 15
  1. #1
    liakos is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default Problem with classes

    i have a composition off two classes A,B. I want to change the code so i can't create objects A in main only through B can anyone help ?????

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    Why do you think you want to do this?

    You could make A a private inner class of B?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    liakos is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    its a project for my school and i want the composition to remain after the transform

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    I have no idea what that means. Have you tried my suggestion?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    liakos is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    yes i know i tried that is there another way ot do this

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    What was the problem with that method? What about your requirements did it not satisfy?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    liakos is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    package askisisi1;
    class Member
    {
    private String fname;
    private int age;
    private int gender;

    public Member(String fn,int a,int g)
    {
    fname=fn;
    age=a;
    gender=g;
    }

    public int getAge()
    {
    return age;
    }

    }

    class Family
    {
    private String lname;
    private int nextChild;
    private Member father;
    private Member mother;
    private Member children[];
    private Member createMember(String n, int age, int gd)
    {
    if((age >= 0) && (gd >= 1) && (gd <= 2))
    {
    return new Member(n, age, gd);
    }
    else
    {
    System.out.println("Error in Member creation !");
    return null;
    }
    }

    public Family(String ln, int maxChildren)
    {
    lname = ln;
    children = new Member [maxChildren];
    }

    public void setFather (String n, int age, int gd)
    {
    father = createMember(n, age, gd);
    }
    public void setMother(String n, int age, int gd)
    {
    mother = createMember(n, age, gd);
    }
    public void addChild(String n, int age, int gd)
    {
    children[nextChild++] = createMember(n, age, gd);
    }
    public float avgAge()
    {
    float avg = 0;
    int memberNo = 0;
    if (father != null)
    {
    memberNo++;
    avg += father.getAge();
    }
    if(mother != null)
    {
    memberNo++;
    avg += mother.getAge();
    }
    for (int i = 0; i < nextChild; i++)
    {
    memberNo++;
    avg += children[i].getAge();
    }
    return (memberNo > 0) ? avg/memberNo : 0 ;
    }
    }


    public class Main
    {
    public static void main(String[] args)
    {
    Family f=new Family("makis",5);
    //f.setFather("sjdhgjkshg",55,0);
    Member m=new Member("hgfjyh",45,1);
    System.out.println(m.getAge());

    }

    }


    i have this code and i want to change it so i can't create objects off class member into main and the composition i have to remain...

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    Again, you say what you want to do, but no why you want to do it. There is almost definitely a better way to accomplish your goal.

    You could also try using packages, I suppose.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    liakos is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    the code i sent before i didn't write it my self its part of school project so i would like to change it so then i create an object Family and then to create objects member. and not to have an object meber that doesn't belong to a Family object

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    ...yes, and I've given you two suggestions about how to do that. You've simply denied them without an explanation as to why they aren't adequate.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    I'm assuming you are not using Kevin's suggestions because of this:
    Quote Originally Posted by liakos View Post
    i want the composition to remain
    Can you explain what do you mean by this?

  12. #12
    liakos is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    i want the two class to be separate....and member constructor must called only from class family...because i want every member to participate in a family always...(never alone)

  13. #13
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Make the Member constructor require a Family object
    as an argument... and throw exception if that Family object
    is null?
    Last edited by cultclassic; 03-09-2011 at 10:57 PM.

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    Quote Originally Posted by cultclassic View Post
    Make the Member constructor require a Family object
    as an argument... and throw exception if that Family object
    is null?
    I'm not sure that would satisfy the OP's requirement that a Member should not be instantiable from main- because there is nothing stopping main from instantiating a Family Object and passing that in.

    That's why I've been asking the OP to be more specific about his requirements, or why the suggestions he has already received are inadequate. Chances are he's simply approaching the problem from the wrong angle.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    I'm not sure that would satisfy the OP's requirement that a Member should not be instantiable from main- because there is nothing stopping main from instantiating a Family Object and passing that in.

    That's why I've been asking the OP to be more specific about his requirements, or why the suggestions he has already received are inadequate. Chances are he's simply approaching the problem from the wrong angle.
    I thought of that.
    and member constructor must called only from class family...because i want every member to participate in a family always...
    I think his main requirement is the second one of the above. If not I was going to point him to method modifiers.. and packages, like you suggested.

Similar Threads

  1. problem with finding imported classes
    By frx08 in forum Java Applets
    Replies: 1
    Last Post: 01-19-2011, 11:51 AM
  2. inner classes problem
    By smallmos1 in forum New To Java
    Replies: 7
    Last Post: 11-18-2010, 03:07 PM
  3. Problem with multiple string in classes
    By sjaakie in forum New To Java
    Replies: 3
    Last Post: 10-10-2010, 02:48 PM
  4. problem with using classes and output
    By sjaakie in forum New To Java
    Replies: 3
    Last Post: 10-10-2010, 01:56 PM
  5. Passing values between classes problem.
    By alin_ms in forum New To Java
    Replies: 8
    Last Post: 12-12-2008, 06:49 PM

Posting Permissions

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