Results 1 to 3 of 3
  1. #1
    LJ1423 is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default Need help getting started on a program

    Hello all,

    Im new to Java programming and new to the forum.

    I have a practice program in preparation for an exam and I need some help. My main problem when Im programming is usually the logic behind what needs to be done and how to start it.

    Here is the task:

    Basically I have a txt file with the following information:
    (state, name of candidate1, pledged delegates for candidate1, candidate 2, pledged delegates for candidate 2).

    The file is 44 rows and 5 columns. I have to prepare a report with the following information:

    1.Read and display the contents of the file.
    I have completed this part

    2.Compute and display the total delegates per candidate.


    3. Determine and list the states won by each candidate based on the delegates.

    4.Determine and display any states where there were ties.

    5.Compute the number of delegates each candidate needs to become the democraetic nominee for president

    6. arrange each candidate's delegates in order from highest to lowest delegate count and display the state and number of delegates. Be sure to label the information by candidate.

    7.Determine and display the candidate with the most delegates.


    If i could just get a simple jumpstart on how to tackle each of these i would greatly appreciate it

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    There are a couple approaches I will suggest. The one I like more is to use a class which encapsulates the important data(in this case, state, candidate1, pledge for 1, candidate 2, and pledges for 2) Then you can store each of them in an array of the object type and loop through doing what is necessary. The other, more difficult and clumsy approach is to use a 2d array. Using 2d arrays however can become easily hard to manage.

    Try implementing one of these approaches and show where you are getting stuck and ask specific questions. Post any code in code tags like this
    [co de]
    YOUR CODE HERE, dont include a space in the code tag as i have above
    [/code]

    Finally, start small, think about, implement and test one thing at a time, when it's finished, move on to the next step.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    total delegates per candidate
    this is a simple calculation of:
    double dcRatio = total number of delegates / total number of candidates;

    Determine and list the states won by each candidate based on the delegates.
    whoever has the majority of delegates wins the state, therefore each candidate must have their own List which states can be added to
    e.g.
    Java Code:
    class Candidate {
        java.util.List<State> statesWon = new java.util.ArrayList<State>();
    }
    and then you would have a method to add states to the statesWon List if the candidate won those states like this:
    Java Code:
    if (thisCandidate has majority) {
        thisCandidate.statesWon.add(thisState);
    }

    Determine and display any states where there were ties.
    you can add a clause to the method above which calculates who won which state. If there was a tie add it to another List OR add it to both/all candidates who were tied and then determine if there was a tie depending on if more than 1 candidate has the same state on their statesWon List.

    Compute the number of delegates each candidate needs to become the democraetic nominee for president
    each state needs its own field of states needed to become a nominee i.e.
    Java Code:
    class State {
        int minStatesForNominee;
        //constructor
        State(int minStates) {
            minStatesForNominee = minStates;
        }
    }

    arrange each candidate's delegates in order from highest to lowest delegate count and display the state and number of delegates. Be sure to label the information by candidate.

    if you store the candidates delegates as a list, you can use the sort method to do this easily.

    Java Code:
    class Candidate {
        java.util.List<Delegate> listOfDelegates = new java.util.ArrayList<Delegate>();
        Candidate(Delegate... delegates) {
            listOfDelegates.addAll(java.util.Arrays.asList(delegates));
        }
        public java.util.List<Delegate> getDelegates() {
            return listOfDelegates;
        }
    }

    Compile a list of candidates i.e.
    java.util.List<Candidate> ListOfCandidates = new java.util.ArrayList<Candidate>();
    //code to add all Candidates known to the list

    Then use Collections.sort to sort the list of candidates by the delegate count
    Java Code:
    java.util.Collections.sort(ListOfCandidates, new Comparator() {
        public int compare(Object o1, Object o2) {
            Candidate c1 = (Candidate) o1;
            Candidate c2 = (Candidate) o2;
            int result;
            if (c1.getDelegates().size() < c2.getDelegates().size()) {
                result = 1;
            } else if (c1.getDelegates().size() > c2.getDelegates().size()) {
                result = -1;
            } else {
                result = 0;
            }
            return result;
        }
    });

    Determine and display the candidate with the most delegates.
    once your list is sorted from highest to lowest delegates, the candidate with the highest delegates would be:
    Java Code:
    Candidate withHighestDelegates = ListOfCandidates.get(0);
    i.e. the first item in the list
    Last edited by ozzyman; 04-09-2011 at 12:07 PM.

Similar Threads

  1. Getting Started Help (:
    By Jcbconway in forum AWT / Swing
    Replies: 3
    Last Post: 10-10-2010, 08:52 AM
  2. Autostart if program isn't started
    By Kruptein in forum AWT / Swing
    Replies: 0
    Last Post: 08-07-2009, 08:03 PM
  3. Replies: 3
    Last Post: 12-31-2008, 06:57 AM
  4. getting started
    By ash in forum Advanced Java
    Replies: 1
    Last Post: 07-23-2008, 11:31 AM
  5. How do I get started?
    By programmer-in-training in forum New To Java
    Replies: 4
    Last Post: 01-11-2008, 08:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •