The Artima Developer Community
Sponsored Link

Java Answers Forum
Help with Assignment

2 replies on 1 page. Most recent reply: May 6, 2004 6:50 AM by SRV001

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 2 replies on 1 page
Tyler White

Posts: 1
Nickname: ty2004
Registered: Apr, 2004

Help with Assignment Posted: Apr 19, 2004 1:27 PM
Reply to this message Reply
Advertisement
I am in my first CIS class in college, my teacher is Indian and extremely hard to understand, my book is written in a foriegn lanquage also, so I really need some help!!
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 19300
The Winner of The election is Duffy.

I know that I have to tokenize the input and seperate the names into a string array, put the votes into another array, then I am not sure. I have made it this far in this class without help, any information that you can provide would be most helpful. I am also wondering about how to display the winner of the election, How do you display a corrisponding index of parallel arrays? Hope someone can walk me through this, thanks.


Sanjay Perera

Posts: 42
Nickname: angel6
Registered: Apr, 2004

Re: Help with Assignment Posted: Apr 30, 2004 6:55 AM
Reply to this message Reply
Hi,
You haven't said what you want. An application or an applet? Some how why use parallel arrays? Use 2D array instead!
(E.g. If it is an applet, use 2 textboxes and when users presses Enter key, take those 2 value into the 2D array!)
I hope my answer helped you little bit (Next time don't say you need a program, just say APPLICATION or an APPLET.
All the best,
Sanjay

SRV001

Posts: 13
Nickname: srv001
Registered: May, 2004

Re: Help with Assignment Posted: May 6, 2004 6:50 AM
Reply to this message Reply
/*
-----------------------------------------------------------
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

Flat View: This topic has 2 replies on 1 page
Topic: Switichin On\Off keyboard's LED display Previous Topic   Next Topic Topic: Greatest Common Divisor Program Help

Sponsored Links



Google
  Web Artima.com   

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