The Artima Developer Community
Sponsored Link

Java Answers Forum
check out ma java pseudo code! help me on the ARRAY part

1 reply on 1 page. Most recent reply: Jun 8, 2004 2:50 AM by Philip Foulkes

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
dechef

Posts: 1
Nickname: dechef
Registered: Jun, 2004

check out ma java pseudo code! help me on the ARRAY part Posted: Jun 8, 2004 1:01 AM
Reply to this message Reply
Advertisement
in a certain election ,there are seven candidates .each voter is allowed one vote for the candidate of his /her choice .The vote is recorded as a number from 1 to 7.(i.e a vote of j indictes that you are voting for candidate j where j=1,2,3......7).The number of voters is unknown beforehand but the votes are terminated by a vote of 0.Any vote ,which is not a number from 1 to 7, is an invlid vote . write a java program that reads the names of the 7 candidates ,reads the votes one at a time and evaluates the results of the election. your output should specify the total number of votes ,the number of valid and invalid votes and the winners of the election e.g When the program is run and the following data is supplied:
MA pseudo code
ma input
1.initialise vote and count
2.use for loop and arrays
3.if vote not (1-7);println invalid
4.if vote =0; then terminate the program
ma output
1.no.of votes
2.no. of valid and invalid votes
3.winner(s) the election


import java.io.*;
import java.util.ArrayList;
public class amone
{
public static void main(String[] args)throws IOException
{
int j,vote;

ArrayList array = new ArrayList();
while (user still wants input) {
get int for vote and put it into a variable userVote;
if (j!=0)
array.add(new Integer(userVote));
}

}
}


Philip Foulkes

Posts: 19
Nickname: frodo
Registered: May, 2004

Re: check out ma java pseudo code! help me on the ARRAY part Posted: Jun 8, 2004 2:50 AM
Reply to this message Reply
Hi.

The way that I would do this problem is to construct an array of size 7, and initialise the whole array to zero.

int candidates = new int[7];
//then initialise all elements to zero

If the user had to say vote for candidate 2, then I would increment the second element in the array:
candidates[IO.readInt() – 1]++;

The candidate with the highest votes at the end is the successful one.

Flat View: This topic has 1 reply on 1 page
Topic: Struts & POI version problem Previous Topic   Next Topic Topic: Handling exception

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use