Results 1 to 9 of 9
  1. #1
    flasche is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Looping through an objects vector

    Hello.


    I'm having some issues, trying to solve this problem in java. I want to print some election results, and i have to loop through a vector of objects and retrieve the partial sums of each party's seats for each constituency and the national results for each party. For now i can print the results per contituency, but i'm having problems in getting the national results. Like, adding the seats for labour party in Constituency A and B and C, etc, and print the sum. And do the same for conservative party.
    This is what i have. Any hint? Thanks!

    Java Code:
     while (i < h.geral.size()) {
    
            show += "Constituency - "
                    + ((Party) h.geral.elementAt(i)).getConstituency() + "\n"
                    + "Party -  "
                    + ((Party) h.geral.elementAt(i)).getParty() + "\n"
                    + "Seats - "
                    + ((Party) h.geral.elementAt(i)).getNum_seats()
                    + "\n" + "__________\n";
            i++;
        }
    
        m.messageBox(show);
    Last edited by flasche; 11-11-2014 at 02:53 PM.

  2. #2
    flasche is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Re: Looping through an objects vector

    Any hint?

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Looping through an objects vector

    First, you should place your code between code tags [code][/code]. You also need to provide more information. It is not clear how the data structures are allocated. Also, unless you are using threads, you should being using ArrayList instead of Vector. But that is not that important at this time.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Looping through an objects vector

    You'll need to produce a set of totals for whatever it is you are summarising.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    flasche is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Re: Looping through an objects vector

    So I need to create a set and get in the class Party to summarize the number of seats ?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Looping through an objects vector

    Possibly.
    Remember, we can't see your Party class and so have no idea whatsoever what is currently in it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Looping through an objects vector

    I don't know because I don't know what the Party class looks like. Nor do I fully understand what getConstituency() returns. But I can offer this advice. Vector is a generic type which means it should be declared as such.
    Java Code:
    Vector<Party> parties = new Vector<>();
    Then since Vector implements interable, you can do this:

    Java Code:
    for (Party p : parties) {  // now use p to call getConstituency(), getParty(), etc. 
        
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    flasche is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Re: Looping through an objects vector

    I've already done that.
    Suppose i have the number of seats of labour and conservative parties for each constituency.
    What i need is to sum the results of labour party in constituency A, B, C, etc and get the total in the end, and the, do the same for conservative party.
    I changed the code a bit, as the classes names weren't clear enough.

    Java Code:
     public static void main(String[] args) {
    		Hondt h = new Hondt();
    		Msgbox m = new Msgbox();
    		m.messageBox("ELECTIONS SIMULATOR D'HONDT");
    
    		m.messageBox("To exit, press '-1'");
    
    		h.readingVotes();
    
    		/***********************************************************************
    		 * print the content of vector calculateSeats which already contains the
    		 * party it's number of seats per constituency.
    		 **********************************************************************/
    		String info = "";
    
    		for (Seats p : h.calculateSeats) {
    			info += String.format("Constituency - %s  - %7s - %2d%n", p.getConstituency(),
    					p.getParty(), p.getNum_seats());
    
    		}
    
    		m.messageBox(info);
    
    		System.exit(0);
    	}
    Class Quocient:

    Java Code:
     public class Quocient {
    
    	private String constituency;
    	private String party;
    	private int quocient;
    
    	public String getConstituency() {
    		return constituency;
    	}
    
    	public void setConstituency(String constituency) {
    		this.constituency= constituency;
    	}
    
    	public String getParty() {
    		return party;
    	}
    
    	public void setParty(String party) {
    		this.party= party;
    	}
    
    	public int getQuocient() {
    		return quocient;
    	}
    
    	public int setQuocient(int quocient) {
    		return this.quocient = quocient;
    	}
    
    }
    Class Seats:

    Java Code:
     public class Seats {
    
    	private int num_seats;
    	private String party;
    	private String constituency;
    
    	public Seats() {
    	}
    
    	public int getNum_seats() {
    		return num_seats++;
    	}
    
    	public void setNum_mandatos(int num_seats) {
    		this.num_seats = num_seats++;
    	}
    
    	public String getParty() {
    		return party;
    	}
    
    	public void setParty(String party) {
    		this.party = party;
    	}
    
    	public String getConstituency() {
    		return constituency;
    	}
    
    	public void setConstituency(String constituency) {
    		this.constituency= constituency;
    	}
    
    }
    I have a vector that receives Quocient Objects ( dividing the votes by 1,2,3...,N = the total of seats available)

    Java Code:
    			for (int j = 1; j <= nSeats; j++) {
    					q = new Quocientes();
    					q.setCirculo(nameC);
    					q.setPartido(nameP);
    					q.setQuocient(nVotes / j);
    					insert(q);
    				}
    Then i use a loop tho retrieve the biggest quotients and put them in Seats vector based on the party's name increasing the num_Seats
    Java Code:
    for (int x = 1; x <= nSeats; x++) {
    			q = remove();
    
    			for (int y = 0; y < calculateSeats.size(); y++) {
    				if (((Seats) calculateSeats.elementAt(y)).getParty() == q
    						.getParty()) {
    
    					((Seats) calculateSeats.elementAt(y)).getNum_seats();
    				}
    			}
    I use a kind of heap method to make the sort, insertions and removes in the vectors.
    I know it's a kinda messy.

    Regards,
    Last edited by flasche; 11-11-2014 at 06:52 PM.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Looping through an objects vector

    Then you need to do as Tolls suggested. Just get whatever it is from the appropriate method or class and compute a total. There is not much more I can say.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Problem with Vector of Objects
    By JavaClueless in forum New To Java
    Replies: 2
    Last Post: 11-07-2010, 03:46 AM
  2. Vector objects
    By Dolphin in forum Java Software
    Replies: 0
    Last Post: 03-24-2009, 08:45 AM
  3. Vector objects
    By Dolphin in forum New To Java
    Replies: 0
    Last Post: 03-24-2009, 08:36 AM
  4. Retrieving out objects from Vector
    By tuckker in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 05:52 AM
  5. Vector containing heterogeneous objects
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-28-2007, 10:19 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
  •