PackageUNIT4;Importcom.sun.java_cup.internal.version; Public classVoter {Private Static intMax_count =100; Private Static intcount; Private StaticVoter[] Voters=NewVoter[max_count]; PrivateString name; PublicVoter (String name) { This. name=name; } PublicString GetName () {returnname; } Public voidvotefor () {if(count==max_count) {System.out.println ("Voting is over"); return ; } if(Isexisted ( This) {System.out.println ("No duplicate votes allowed!"); }Else{Votersadd ( This); System.out.println ("Thank you for voting."); } } Public Static Booleanisexisted (Voter obj) { for(inti=0;i<count;i++){ if(Voters[i].getname () = =Obj.getname ()) { return true; } } return false; } Public Static voidvotersadd (Voter obj) {voters[count++]=obj; } Public Static voidPrintvoteresult () {System.out.println ("The current number of votes is:" +count); System.out.println ("Voting voters are as follows:"); for(inti=0;i<count;i++) {System.out.println (Voters[i].getname ()); } } Public Static voidMain (string[] args) {Voter Majun=NewVoter ("Ma June")); Voter Fly=NewVoter ("flying"); Voter Mike=NewVoter ("Mike"); Voter Majian=NewVoter ("Ma Jian"); Majun.votefor (); Fly.votefor (); Mike.votefor (); Mike.votefor (); Majian.votefor (); Voter.printvoteresult (); } }
Output Result:
Thank you for voting Thank you vote Thank you vote not allowed to repeat the vote ! Thank you for voting. The current number of votes is:4 The voters voted as follows: Ma June fly Mike Ma Jian
Static keyword Synthesis small application