SRV001
Posts: 13
Nickname: srv001
Registered: May, 2004
|
|
Re: Help with Assignment
|
Posted: May 6, 2004 6:50 AM
|
|
/* ----------------------------------------------------------- The assignment is to write a program to accept this input:
Joson 500 Millers 400 Duffy 600 Robson 250 Ashtony 180
These are the last names of 5 candidates in an election and the number of votes each recieved. I am supposed to have the following output
Candidate Votes Recieved % of Total Votes Joson 500 25.91 Millers 400 20.73 Duffy 600 31.09 Robson 250 12.95 Ashtony 180 9.33 Total 1930 The Winner of The election is Duffy. ------------------------------------------------------------ usage: java Election Joson 500 Millers 400 Duffy 600 Robson 250 Ashtony 180 ------------------------------------------------------------ Mr White - You mentioned that you were stuck and didn't know what to do next. I think that this is a common problem among new programmers. You need to slow down and not panic when faced with a problem such as this. Break it down into a series of smaller problems and it will all come together. I know that you are on the right track but stopped trying to understand the rest of the problem when you got stuck on the first part. Here is a little advice to get you going:
1. The trick to becomming a programmer is to type in code and see it work. The more you do this the better you will understand how to solve problems. Understand the basics and the rest will fall into place. Always strive to make your code better.
2. When you are tackling a problem such as this do it in increments. Don't try to solve the whole problem all at once. ie create the arrays and then use a for loop to print out the arrays like this: for (int j = 0; j < names.length; j++) { System.out.println(names[j]); }
3. If you can't figure out the next step, move ahead a few steps to what you do know, then fill in the blanks later. Write down the steps in a way that you can understand, maybe like this:
-------------------------------------------------- a. read in names (names are in position 1, 3, 5, etc.) b. read in votes (votes are in position 2, 4, 6, etc.) c. need to convert input votes from Sting to int d. calculate the percentage of votes for each candidate (notice the word each - implys a loop here) (# votes candidate received / total # votes) * 100 e. need to format the output so that there are only 2 places after the decimal f. need to find the candidate with the most number of votes (hey, I could do this comparison in the above loop) g. When all done (loop ends) print out the total number of votes h. Print out the election winner. -------------------------------------------------- Remember, write down what you know and keep revising it by adding more information as you start to understand the problem more.
Good Luck, and next time give it some real effort. */
import java.text.NumberFormat;
public class Election {
public static void main(String[] args) {
String[] names = { args[0],
args[2],
args[4],
args[6],
args[8] };
int[] votes = {
(Integer.getInteger(args[1]).intValue()),
(Integer.getInteger(args[3]).intValue()),
(Integer.getInteger(args[5]).intValue()),
(Integer.getInteger(args[7]).intValue()),
(Integer.getInteger(args[9]).intValue()) };
String winner = "";
int winnerVotes = 0;
int totalVotes = 0;
for (int j = 0; j < votes.length; j++) {
totalVotes += votes[j];
}
NumberFormat formatter =
NumberFormat.getNumberInstance();
formatter.setMinimumIntegerDigits(1);
formatter.setMaximumFractionDigits(2);
System.out.println(
"\nCandidate Votes Recieved % of Total Votes");
System.out.println(
"--------- -------------- ----------------");
for (int j = 0; j < names.length; j++) {
if (votes[j] > winnerVotes) {
winner = names[j];
winnerVotes = votes[j];
}
System.out.println(
names[j] + "\t\t"
+ (int)votes[j] + "\t\t"
+ (formatter.format(
( (double)votes[j] / totalVotes )*100)));
}
System.out.println("Total " + totalVotes);
System.out.println(
"The winner of the election is " + winner);
} //main()
} //Election
|
|