Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Creating Candidate List in Java

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Currently I am working on a project for the study Java programming.
I tried a lot of things, but I get errors when I want to add the names of candidates or political parties.
Can you guys maybe help me out with this?

I have to make the following project:

There is a Voting class that has at least the following data:

a two-dimensional array
the electoral list.
The Voting class has at least the following methods:

RandomVoting () a vote is taken exactly 50 times, by randomly choosing a party and randomly choosing a candidate from that party.
ShowVoting displays the list in the output (can be done with System.out.println)
run: Seven rows are displayed. The first column contains the sequence number.

0 1 1 The second column contains the lot (3 lots).
1 2 4 The third column lists the candidate from that party.
2 3 2
3 2 3
4 3 3
5 3 5
6 2 4

addVotesToe: In this method, the candidate's vote in the party concerned is increased by one.
The electoral list has a method to return the list of parties and candidates.
In the test class, this list is written to the output.

Results of the vote:

Pvda
Jan 2
Piet 1
Klaas 1
Marie 2
Gerry 1

total number of votes = 7

CDA
Lies 3
Marieke 6
Joop 7
Hannie 9

total number of votes = 25

VVD
Lubbers 2
Cook 2
Hans 2
Paula 3
Sissy 4
Martie 5

total number of votes = 18

I have made the class so far like this:



I get the sequence numbers in the table, but not the names of the candidates and the politic parties.
Any advice how I can add this to the 2d Array and to make the code run without errors?
 
Saloon Keeper
Posts: 7386
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pllease UseCodeTags(<--link) when posting code), it adds line numbers and preserves  formatting. I added it for you this time.
 
Carey Brown
Saloon Keeper
Posts: 7386
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You started out with an object oriented approach by creating a class to hold party an candidate, but then you ignored that when you went to write your main() method and chose a multi-dimensional array instead. The array is not object oriented and quickly become unmanageable. Here's a more object oriented approach. You may need to add a vote count to you class fields.
 
Marshal
Posts: 70625
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AH: welcome to the Ranch

Carey Brown wrote:You started out with an object oriented approach . . . when you went to write your main() method and chose a multi-dimensional array instead. . . .

Unfortunately, that appears to be part of the requirements. I don't like assignments that specify implementation details like that.
 
Bartender
Posts: 4103
156
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ashana

this is how I read the assignment.

I think that your Vote-class ("Stemming") is superfluous, given that you also have that 2D-array in your Voting class.

You need a Party class, that contains its name and a List<String> candidates. That class should have an addCandidate method and sufficient getters. In the Voting class, have a List<Party> parties.

Now, that 2d array can be tailor-made as follows: (shows that using Lists is far more convinient)

and then initialize further with:

Now you can generate sufficient random numbers to fill the array, and then transform it to a result-report. Make sure that the random numbers all are within the boundaries.


PS: Lubbers was from the CDA, not the VVD    
PS2: I have no idea where that sequence number is good for

 
Ashana Hurkens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@piet souris,

Thanks for the apply.

I was afraid that it was too much code lines in the program I made so far.

So actually I would be better if I made an ArrayList in my program?
That would give me the opportunity to show the parties and the names in table form?

I was first thinking of using a JTable in Ntebeans, but I think that is not allowed in the assignment,

Reply on PS1 don’t know very much about politics πŸ˜…πŸ˜…
Reply on PS2 neither do I


 
Piet Souris
Bartender
Posts: 4103
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ashana,

that "Stemming"class is a lot nicer than that 2D array, but then again, that 2D is mandatory.

Well, as I said, I would have a Party class with a List of members, and a List<Party>, to have a flexible way of organizing your data. So, yes.

Then the aspect of presenting the results of the election. Using the array you can create all the output that you want. For instance: say you want to show the votes for each party, having a 2D array. A way is:

But you can also do it with a JTable, or in any other way that you can think of.

So, my advice is to have a few methods like "printResultOfAllParties()", "printResultsOfCanditatesForParty(Party party)", and the like.

And sorry for my late replay!
 
Ashana Hurkens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet Souris,

Thanks for your reply, I dont mind the lateness of the reply πŸ˜‰

The line of coding your mentioned before is very helpfull.
Maybe that line is way better than the lines I used in my code to create the 2d array.
And perhaps a ArrayList can help me organize the data of the candidates and the parties in a list.

I will try some things out in my program, maybe I made it too difficult for myself.

 
Ashana Hurkens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

In the past days I tried a lot of things to solve the code including the code lines you guys mentioned in this group.
But I keep getting these errors in my code/code does not work the way it should be.
I read the assignment again and I believe how to get the code work:

- The assignment tells that there is There is an electoral list with parties.
A party has a name and is listed in sequence (ie has a number) on a directory.
A party has candidates.
The candidates have a name and are listed in order < sound like an ArrayList of each party and the candidates that are in that party.

There is a Voting class that has at least the following data:

a two-dimensional array < needs seven rows and three columns. First row contains a number (looks like the numbers mentioned in my first message).
the selection list. < list with the numbers, parties and candidates.
The Voting class has at least the following methods:

RandomVoting () there is a vote exactly 50 times, by randomly choosing a party and randomly choosing a candidate from that party.
ShowVoting displays the list in the output (can be done with System.out.println).

Forgive me if I get it wrong, but I think this should make the code work. By importing the ArrayList into the 2d Array, adding the methods in the code and then write the code into a testclass.
It is very confusing this assignment, but hopefully I can soon finish this assignment.

 
Piet Souris
Bartender
Posts: 4103
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is my idea too. I already implemented it, only confusing is this 2D-array, the should have three columns, and seven rows. Now I thought that that array was meant to record the votings, but then I do not know why there would be seven rows, when there are 50 votes. Is there any way you could ask someone for clarification? (well, this site is a good place, one would think, but that has to be someone else).
 
Ashana Hurkens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello every,

I am sorry for my very late reaction about this topic.
I have asked the teacher of this course for help, but unfortunately I didn't receive a answer for the questions I had about this project.
He first told me I had to make three classes for each Party and the Candidates of that Party.
I did this and sended this to the teacher. A week later I received his answer. He wanted to see the source code and the whole project.
So I read the assignment many times and looked in the book OCA oracle Certified Associate Java SE 8 Programmer 1 Study Guide written by Jeanne Boyarsky and Scott Selikoff and I think I found the solution to make the program work and show the voting list:

One of the requirements of the assignment was to create a 2d Array with 7 rows and 3 columns.
In order to get these, I used a nested for loop already for the first column (which according to the assignment the numbers should be in).
I applied this nested loop in my program and it worked; I got the numbers just where I want them.
I also added the constructor and the getters and setters in my code.

The next days I am going to figure out how to add the names of the Parties and the Names of the Candidates in the list.
Also Iam looking at the other methods they require for this assignment.

I believe that I have to use loops to add the Parties and the Candidates to the 2d Array.

If you guys have any other ideas about this topic, please share them.
Maybe it can help me to understand the Java language even more
 
Piet Souris
Bartender
Posts: 4103
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ashana,

re-reading the topic, I think I know what your teacher meant with that 2D array. Now, I don't know why I was focussed on those 7 rows, but now it seems that in your opening post that 7 rows were only the first 7 votes, out of 50. So, I think that the 2D array whould be dimensioned as: votes[50][3], big enough for 50 votes.

Thenn the three columns contain: the vote number, the number of the party chosen, and the number of the candidate for that party. As you can see in that example in your opening post, that first column is actually not needed, since vote[i][0] = i, but alas.

So, have a Party class, containing a List<Person> candidates.

Then the Voting class has a List<Party> parties, and when you create 50 votes, then vote number i will be stored in vots[i][0]. Then select a random number in the range 0...parties.size() p, and a random number in the range 0...parties[p].gtCandidateList().size c, to select the candidate of that party. Then we pt it in the votes-array:

And then it is just a matter of having a nice way to present the outcome. I think I gave an eample of this in one of my replies.
 
Hey cool! They got a blimp! But I have a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic