Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Votes JAVA
//'main' method must be in a class 'Rextester'. //openjdk version '11.0.5' import java.util.*; import java.lang.*; import java.io.*; import java.util.*; import java.lang.*; class Voter{ private int voterId; private String voterName; private int voterAge; private boolean isVoteCasted; private String constituency; Voter(int id,String name,int age,boolean cast,String constit) { voterId=id; voterName=name; voterAge=age; isVoteCasted=cast; constituency=constit; } public int getVoterId() { return voterId; } public String getVoterName() { return voterName; } public int getVoterAge() { return voterAge; } public boolean getIsVoteCasted() { return isVoteCasted; } public String getConstituency() { return constituency; } public void setVoterId(int id) { voterId=id; } public void setVoterName(String name) { voterName=name; } public void setVoterAge(int age) { voterAge=age; } public void setIsVoteCasted(boolean cast) { isVoteCasted=cast; } public void setConstituency(String consitut) { constituency=consitut; } } class Rextester { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); Voter[] v = new Voter[4]; for(int i=0;i<4;i++) { int id = sc.nextInt(); sc.nextLine(); String name = sc.nextLine(); int age = sc.nextInt(); boolean isVoteCasted = sc.nextBoolean(); sc.nextLine(); String constituency = sc.nextLine(); Voter x = new Voter(id,name,age,isVoteCasted,constituency); v[i] = x; } String cons = sc.nextLine(); int ans = findTotalVotesCastedByConstituency(v , cons); if(ans==0) { System.out.println("No Votes Casted"); } else { System.out.println(ans); } Voter[] ans2 = searchVoterByAge(v); if(ans2==null) { System.out.println("No such Voters"); } else { int i=0; while(ans2[i]!=null) { System.out.println(ans2[i].getVoterId()); i++; } } } public static int findTotalVotesCastedByConstituency(Voter[] v , String cons) { int sum = 0; for(int i=0;i<4;i++) { if((v[i].getConstituency()).equals(cons)) { if(v[i].getIsVoteCasted()) sum++; } } return sum; } public static Voter[] searchVoterByAge(Voter v[]) { int cnt=0; for(int i=0;i<4;i++) { if(v[i].getVoterAge()<30) { cnt++; } } if(cnt==0)return null; Voter h[]=new Voter[cnt]; int j=0; for(int i=0;i<v.length;i++) { if(v[i].getVoterAge()<30) { h[j++]=v[i]; } } Voter temp=null; for(int i=0;i<cnt-1;i++) { for(int k=i+1;k<cnt;k++) { if(h[i].getVoterId()>h[k].getVoterId()) { temp=h[i]; h[i]=h[k]; h[k]=temp; } } } return h; } } /* INput 1008 Aman 29 true cons1 1003 Saman 43 true cons1 1004 Kamal 28 false cons2 1005 Ritu 48 true cons1 cons1 question Link=> https://codebashers10.blogspot.com/2021/02/tcs-xplore-cpa-15-feb-2021-java-coding.html */
run
|
edit
|
history
|
help
0
Problem: rstring
"encryption"
String reverse
Sort an array of 0's 1's 2's 3 pointer approach
exp 1
Equal_HashCode_TEST --- imporatnt concept about hashing
do while
PE #4
inheritance
on_off