java - Why won't my method returns divide properly? -


so i'm having trouble printresults method in testcandidate class. seems run expected, except column displaying percent of total votes each candidate received. reason, operation printing 0's. (for record, when use word "operation," i'm referring list[i].getvotes / gettotal(list) operation. clarify).

i've tried various combinations of parenthesizing list[i].getvotesmethod , gettotal(list) method no avail. i've tried performing operation outside of print statement , printing variable operation have been assigned to.

the strange thing is, when take either list[i].getvotes or gettotal(list) out , leave one, both print fine. not , can't figure out why.

so, in summary, why won't list[i].getvotes / gettotal(list) divide properly? (please not try question asks for, i'd learn , fix own mistakes , best optimize code as possible cannot past 1 whatever reason). thank you!

here's candidate class:

public class candidate {            private int votes;     private string candidatename;      public candidate(string name, int numvotes)     {         votes = numvotes;         candidatename = name;            }     public string getname()     {         return candidatename;      }     public void setname(string name)     {         candidatename = name;     }     public int getvotes()     {         return votes;     }     public void setvotes(int numvotes)     {         votes = numvotes;     }            public string tostring()     {         return candidatename + " received:\t" + votes + " votes.";      } } 

here's testcandidate class:

 public class testcandidate      {               public static void main(string[] args)         {                                        candidate[] election = new candidate[5];          election[0] = new candidate("john smith", 5000);         election[1] = new candidate("mary miller", 4000);         election[2] = new candidate("michael duffy", 6000);         election[3] = new candidate("tim robinson", 2500);         election[4] = new candidate("joe ashtony", 1800);           printvotes(election);       system.out.println();       printresults(election);       system.out.println();         system.out.println("the total amount of votes cast was: " + gettotal(election) + ".");                                           }     public static <e> void printvotes(e[] array)    //prints candidates , amount of votes      {                                                           //each received         for(e element : array)         {             system.out.printf("%s ", element);             system.out.println();         }     }     public static int gettotal(candidate[] list)    //adds votes each candidate , prints     {                                                           //total amount         int totalvotes = 0;         for(int = 0; < list.length; i++)         {             totalvotes += list[i].getvotes();         }         return totalvotes;     }       public static void printresults(candidate[] list)  //prints table containing candidate's name, amount of votes, ,     {                                                  //percentage of total votes earned       system.out.println("candidate: \t votes recieved: \t percent of total votes:");             for(int x = 0; x < list.length; x++)       {                                 system.out.println(list[x].getname() + " \t " + list[x].getvotes() + " \t \t \t " + list[x].getvotes()/gettotal(list) + "%");                       }    }                     } 

and here's current output:

john smith received:    5000 votes.  mary miller received:   4000 votes.  michael duffy received: 6000 votes.  tim robinson received:  2500 votes.  joe ashtony received:   1800 votes.   candidate:   votes recieved:     percent of total votes: john smith       5000            0% mary miller      4000            0% michael duffy    6000            0% tim robinson     2500            0% joe ashtony      1800            0%  total amount of votes cast was: 19300. 

to percentages show correctly use following line of code:

(double)100*list[x].getvotes()/gettotal(list) 

this happens because java integers division gives these zeros see in results (0%):


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -