Results 1 to 14 of 14
Like Tree2Likes
  • 2 Post By bnson

Thread: Exercice with 2 classes, one contain the data of the second in an array--How to?

  1. #1
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default Exercice with 2 classes, one contain the data of the second in an array--How to?

    Hi,

    I am new at Java and I tried an exercice, but I don't know how to start. This is the exercice:
    • Define a class Household that contains an id, annual income and number of members.
    • Define a class Township that stores a number of households in an array.
    • Write a method that count the number of households included in the twonship and print a table displaying the data read in.


    I tried like this, but I don't know if it is a good way to do:
    First a created a file Household.java with this code:
    Java Code:
    public class Household{
    	public int idNumber;
    	public int annualIncome;
    	public int numberHouseholdMembers;
    
    
    }
    Than a second file Township.java:
    Java Code:
    public class Township{
    	Township Household[];
    	
    	public void setHousehold(int id,int ai,int nhm){
    		Household hh=new Household();
    		hh.idNumber=id;
    		hh.annualIncome=ai;
    		hh.numberHouseholdMembers=nhm;
    
    	}
    In a main method I call:
    Java Code:
    Township town1=new Township();
    town1.setHousehold(1041,12180,4);
    The problem is that I don't know how to store the result in the array in my Township class or how to define the Township class...
    Last edited by bigjo; 07-18-2011 at 05:47 PM. Reason: Changes the QUOTE-tags to CODE-tags

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Why don't you add a method in your town class like addHouse where it tells your town object to create a new House object and then stores it to the array inside the Town object.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,742
    Rep Power
    19

    Default

    Create your array of a fixed size (say 3 households to start with), and add three households to that array. I'd change your setHousehold method to do this, and simply call it setHouseholds(). Inside that you can populate the array.

    Note, the syntax for your array declaration is wrong.
    Should be:
    Java Code:
    Household[] households;
    You'll also need to initialise it...but you should have notes on how to do that.

    And finally, please use code tags when posting code (see this post).

  4. #4
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    Thank you for your answers, but I have still problems. First I don't know how many households I will have. So I can't set my array in a fixed size.

    and the bigger problem for me is that I don't see how to populate my array. I tried:

    Java Code:
    	public void setHouseholds(int id,int ai,int nhm){
    		Household[id]=(id,ai,nhm);{
    Sorry but I am realy a beginner. I have read the theory, but I am for the moment not able to put it into practice. Maybe if I had an example of what you mean.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,742
    Rep Power
    19

    Default

    The problem as posted says nothing about the number of households, so I just presumed it was an exercise in getting a couple of classes to interact in a really basic way.

    As for populating the array, what happened with what you tried?
    What errors did you get?
    Stack traces?

  6. #6
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    there are errors like:
    ')' expected with a arrow on the first coma of the
    Household[id]=(id,ai,nhm);

    than
    error not a statement with an arrow on the ai.

    and so on (5 errors)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,742
    Rep Power
    19

    Default

    Do you know how to instantiate an object?
    Java Code:
    MyClass myClass = new MyClass(...some parameters...);
    In your case it would be new Household(...your parameters...).

  8. #8
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    I think it is what I dit in my first code:

    Java Code:
    public void setHouseholds(int id,int ai,int nhm){
    Household hh=new Household();
    		hh.idNumber=id;
    		hh.annualIncome=ai;
    		hh.numberHouseholdMembers=nhm;
    }
    but then I don't know how I put this data into my array.

  9. #9
    bnson is offline Artemis
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    Hi, bigjo

    I'm beginning to learn java, and your exercises a very interesting.

    I'm think you edit class Household.

    Java Code:
    public class Household {
    
        public Household(int idNumber, int annualIncome, int numberHouseholdMembers) {
        	this.idNumber = idNumber;
        	this.annualIncome = annualIncome;
        	this.numberHouseholdMembers = numberHouseholdMembers;
        }
    
    	private int idNumber;
    	private int annualIncome;
    	private int numberHouseholdMembers;    
        
    }
    And class Township you have used ArrayList can be resolve problem "I don't know how many households I will have".
    Java Code:
    ArrayList<Household> township = null;
    Now initialization arraylist.
    Java Code:
        public Township() {
        	township = new ArrayList<Household>();
        }
    And function add element of array.

    Java Code:
    	public void add_Household(int id,int ai,int nhm){
    		township.add(new Household(id, ai, nhm));
    	}
    You can download example: ExerciseTwoClasses.rar

    Have nice day,...
    bigjo and wired-in=p like this.

  10. #10
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    thank you very much bnson !

  11. #11
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    @bnson, don't spoon feed people. It only prevents them from learning, more often then not you code will just be taken and turned in as a class assignment claimed as their own. We often tell people what is wrong, and then point them in the right direction.

    @bigjo, don't just take the code he gave you. You should really write it all yourself. This is the most effective way to learn. You learn by thinking, not by copying.

    For your household problem, you don't know how many houses will exist. So you have two options. Use a larger than expected array, which is not recommended, or use an arraylist. ArrayList (Java Platform SE 6)

    I recommend using an ArrayList because of the fact that it is so flexible. You do not have very many limitations, however it does use more memory and is slower than a regular array. The good news is that as long as you are not dealing with thousands of objects in your array, an average computer will not suffer any performance issues.

    Now I encourage you to ask more questions about your own code. Seeing as you aren't really that far away from accomplishing it yourself.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  12. #12
    bnson is offline Artemis
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    Hello, Dark

    Thank your comments,

    I understand what you mean, and it is also unfortunate reality in the learning process of my country, often students at the my school not think about their assignments study and they only care about grades.

    Information technology of the country has not yet developed, I regularly have the internet to find solutions to your problems, I have difficulty in language.

    However, during that time I realized that for a new student to learn through the code and guide is also a good measure, and I like its,

    So, I think photosynthesis in ourselves is how to take advantage of what we know and understand.

    Thank you, Nice to meet you.

  13. #13
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Its rather unforunate that your country only cares about grades. Other countries however care about their students actually learning the subject matter. We help people with their code here, we don't write it for them.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  14. #14
    bnson is offline Artemis
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    Oh, I see

    And I'm not write for them, I used code for them then edit it's.
    I think that's not a bad thing.

    If they can write then they can read and understand.

    Addition, my english is not good, I think it is difficult to explain what I mean

    You see......?

Similar Threads

  1. Replies: 4
    Last Post: 04-17-2011, 12:15 PM
  2. Passing data between 3 classes
    By tocakt in forum New To Java
    Replies: 3
    Last Post: 04-11-2011, 10:50 PM
  3. Storing Mutiple Classes/Data Types in a Linked List
    By Chronoattica in forum New To Java
    Replies: 7
    Last Post: 12-28-2010, 10:03 PM
  4. How to access private data types from public classes?
    By kevzspeare in forum New To Java
    Replies: 3
    Last Post: 03-07-2009, 04:19 AM
  5. Need help passing data between classes
    By bri1547 in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 04:19 AM

Posting Permissions

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