Results 1 to 14 of 14
- 07-18-2011, 05:05 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
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:
Than a second file Township.java:Java Code:public class Household{ public int idNumber; public int annualIncome; public int numberHouseholdMembers; }
In a main method I call: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; }
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...Java Code:Township town1=new Township(); town1.setHousehold(1041,12180,4);
Last edited by bigjo; 07-18-2011 at 05:47 PM. Reason: Changes the QUOTE-tags to CODE-tags
- 07-18-2011, 05:20 PM #2
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.
- 07-18-2011, 05:22 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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:
You'll also need to initialise it...but you should have notes on how to do that.Java Code:Household[] households;
And finally, please use code tags when posting code (see this post).
- 07-18-2011, 05:45 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
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:
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.Java Code:public void setHouseholds(int id,int ai,int nhm){ Household[id]=(id,ai,nhm);{
- 07-18-2011, 05:47 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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?
- 07-18-2011, 06:08 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
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)
- 07-18-2011, 06:12 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Do you know how to instantiate an object?
In your case it would be new Household(...your parameters...).Java Code:MyClass myClass = new MyClass(...some parameters...);
- 07-18-2011, 06:51 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
I think it is what I dit in my first code:
but then I don't know how I put this data into my array.Java Code:public void setHouseholds(int id,int ai,int nhm){ Household hh=new Household(); hh.idNumber=id; hh.annualIncome=ai; hh.numberHouseholdMembers=nhm; }
- 07-18-2011, 07:57 PM #9
Artemis
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
Hi, bigjo
I'm beginning to learn java, and your exercises a very interesting.
I'm think you edit class Household.
And class Township you have used ArrayList can be resolve problem "I don't know how many households I will have".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; }
Now initialization arraylist.Java Code:ArrayList<Household> township = null;
And function add element of array.Java Code:public Township() { township = new ArrayList<Household>(); }
You can download example: ExerciseTwoClasses.rarJava Code:public void add_Household(int id,int ai,int nhm){ township.add(new Household(id, ai, nhm)); }
Have nice day,...
- 07-18-2011, 08:40 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
thank you very much bnson !
- 07-18-2011, 09:07 PM #11
@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.
- 07-18-2011, 10:28 PM #12
Artemis
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
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.
- 07-19-2011, 04:22 PM #13
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.
- 07-20-2011, 07:45 PM #14
Artemis
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
How do I control access to data in a class from specific other classes?
By Skiller in forum New To JavaReplies: 4Last Post: 04-17-2011, 12:15 PM -
Passing data between 3 classes
By tocakt in forum New To JavaReplies: 3Last Post: 04-11-2011, 10:50 PM -
Storing Mutiple Classes/Data Types in a Linked List
By Chronoattica in forum New To JavaReplies: 7Last Post: 12-28-2010, 10:03 PM -
How to access private data types from public classes?
By kevzspeare in forum New To JavaReplies: 3Last Post: 03-07-2009, 04:19 AM -
Need help passing data between classes
By bri1547 in forum New To JavaReplies: 3Last Post: 07-21-2008, 04:19 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks