• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to code addCitizen  RSS feed

 
Katherine Johnson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a a constructor and two methods.

public Citizen(int id, int qualif) {
        citizenId = id;
        educationalQualification = qualif;
    }


public void addCitizen(int qual) {       
//assign education level, find the next open spot and place the citizen there
       
    }

public void addCitizen() {

}

I anyone able to help me know how to complete addCitizen?
 
Pete Letkeman
Ranch Foreman
Posts: 914
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Katherine, looks like more information may be needed for this.

I have some questions that I hope you can answer so that the problem can be solved:
  • Are you able to provide any more details?
  • What does your full object class look like?
  • What properties does it have?
  • Can the addCitizen method definition change? Maybe it should not return void, but a Citizen object.

  • If this came from some other source them please quote your sources as noted here
    https://coderanch.com/wiki/659928/Quote-Sources

    By the way, if when you post code please use the code tags as noted here
    https://coderanch.com/wiki/659781/Code-Tags
     
    Katherine Johnson
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I once again forgot to add the code tags, I appogise.
     
    Pete Letkeman
    Ranch Foreman
    Posts: 914
    26
    Android Chrome IntelliJ IDE Java MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please note, that we while we do try to help out we also try not to provide complete solutions in the Beginning Java forum especially if it is a homework assignment.

    Citizen.java contents:

    Village.java contents:

    ComputeIntellect.java contents:

    One item of note/concern is

    is used a lot just like that. However that means that value is 0. I would suggest that you change that so that they have unique values and I would use the same numeric values for both ComputeIntellect and Citizen to give you something like

    In your Village class you have an array of Citizen.
    To add an object to an array you do can do something like this:

    So if you take myClass[9] = new MyClass(); as an example then you could use that in both versions of the addCitizen method providing your Citizen array was instantiated before that those methods are called.
    Does this put you on the right track with those methods?

    These methods in the Citizen.java should probably return a valid value and yes a value of 0 would work for now, but only for limited testing:
  • public static int generateEducationalQualification()
  • public static int generateId()
  • public static String convert(int i)
  • public static void resetId()

  • The distributionOfQualification method would need to be coded last after you have objects in your array.

    Let me know if you would like me to explain this in a different way or if this helps you out.

    Edit: Typo Fixed
     
    Katherine Johnson
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I do not see any option to edit my posting.
     
    Pete Letkeman
    Ranch Foreman
    Posts: 914
    26
    Android Chrome IntelliJ IDE Java MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not everyone can edit their post. To edit your post you need to have some cows. This is still subject to change.
    You can read about cows here https://coderanch.com/wiki/659926/Ranch-Cows.
     
    Norm Radder
    Rancher
    Posts: 2240
    28
     
    John Joe
    Ranch Hand
    Posts: 443
    3
    Android Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also http://www.codingforums.com/java-and-jsp/387580-how-code-addcitizen.html
     
    Katherine Johnson
    Greenhorn
    Posts: 11
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is resolved, Thank you for all these responses!
     
    Pete Letkeman
    Ranch Foreman
    Posts: 914
    26
    Android Chrome IntelliJ IDE Java MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for letting us know that you have a solution.

    Please note that it is perfectly fine to post to multiple sites.
    However if you do please provide links to those sites so that everyone is on the same page and up to date.
    Otherwise a solution could be provided on a different site while someone here is working on helping you out, which may not be the best use of their time.
     
    Fred Kleinschmidt
    Bartender
    Posts: 571
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i would think that the addCitizen() method would take a Citizen object as its single argument. I am not sure what it would mean to have an integer as the argument.
    I would also make the citizen variable an ArrayList rather than an array - makes it easier to add a citizen or delete one. Then the addCitizen method would look like:

    You might also want to do something special (perhaps throw an exception) if the input Citizen is null or if the input Citizen is already in the list.
     
    Carey Brown
    Saloon Keeper
    Posts: 3329
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Fred Kleinschmidt wrote:i would think that the addCitizen() method would take a Citizen object as its single argument. I am not sure what it would mean to have an integer as the argument.
    I would also make the citizen variable an ArrayList rather than an array - makes it easier to add a citizen or delete one. Then the addCitizen method would look like:

    You might also want to do something special (perhaps throw an exception) if the input Citizen is null or if the input Citizen is already in the list.

    ArrayList has an add() method. It does not have an append() method.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!