Do the following problem. For assignment submission, use the format listed in th
ID: 3739517 • Letter: D
Question
Do the following problem. For assignment submission, use the format listed in the syllabus Write a program that creates a derived class called election results from a base class called voting results. The derived class should represent a candidate rather than ballot proposal. Create those attributes that would fit for a candidate rather than a ballot proposal. The derived class may have methods that are similar to base class. Look at a recent election in your hometown and output a report similar to the following: Candidate Received Candidate 1 of total Votes Candidate 2 of total Votes Candidate X Votes % of Total Votes # of votes # of votes # of votes % of total VotesExplanation / Answer
//java programme MyClass .java
//Base class for voting results
class VotingResults
{
int totalvotes=10000;
}
//class for election resilts
class ElectionResults extends VotingResults
{
String candidate;
int candidate_votes;
//constructor for class
ElectionResults(String s,int votescount)
{
candidate=s;
candidate_votes=votescount;
}
}
//Main Claas
public class MyClass {
public static void main(String args[]) {
//Object creation with two arameters as candidate and number of votes of candidate
ElectionResults C1=new ElectionResults("Candidate1",2000);
ElectionResults C2=new ElectionResults("Candidate2",3000);
ElectionResults C3=new ElectionResults("Candidate1",4000);
//calculation of total votes polled
int totalvotesreceived=C1.candidate_votes+C2.candidate_votes+C3.candidate_votes;
//Display output (deriving values from base class and derived class)
System.out.println("Total votes "+C1.totalvotes+",Total Votes Received "+totalvotesreceived+" "+"Votes Not polloed "+(C1.totalvotes-totalvotesreceived));
System.out.println("Candidate Recevied "+"votes " +"% of Total votes ");
System.out.println(C1.candidate+" "+C1.candidate_votes+" " +(C1.candidate_votes*100/C1.totalvotes)+"% ");
System.out.println(C2.candidate+" "+C2.candidate_votes+" " +(C2.candidate_votes*100/C2.totalvotes)+"% ");
System.out.println(C3.candidate+" "+C3.candidate_votes+" " +(C3.candidate_votes*100/C3.totalvotes)+"% ");
}
}
Output :
Total votes 10000,Total Votes Received 9000
Votes Not polloed 1000
Candidate Recevied votes % of Total votes
Candidate1 2000 20%
Candidate2 3000 30%
Candidate1 4000 40%
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.