Question

Write a Python program that creates a class which represents a candidate in an election. The...

Write a Python program that creates a class which represents a candidate in an election. The class must have the candidates first and last name, as well as the number of votes received as attributes. The class must also have a method that allows the user to set and get these attributes. Create a separate test module where instances of the class are created, and the methods are tested. Create instances of the class to represent 5 candidates in the election, then it should output each candidate’s name, the votes received by that candidate, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# This programs is written for python version greater than 3.0

class electionCandidate:
""" Class for Election Candidate """
def __init__(self, fName, lName, votesRcvd):
self.fName = fName
self.lName = lName
self.votesRcvd = votesRcvd

#Methods to set the attributes are commented as in this case constructor is used to set attributes
#def setName(self, fName, lName):
# self.fName = fName
# self.lName = lName

#def setVotesRcvd(self, votesRcvd):
# self.votesRcvd = votesRcvd
  
def getName(self):
return (self.fName+' '+self.lName)

def getVotesRcvd(self):
return (self.votesRcvd)
  
def main():
#Create instances for candidates
candidateList = []
candidateList.append(electionCandidate("fName1", "lName1", 10))
candidateList.append(electionCandidate("fName2", "lName2", 20))
candidateList.append(electionCandidate("fName3", "lName3", 75))
candidateList.append(electionCandidate("fName4", "lName4", 45))
candidateList.append(electionCandidate("fName5", "lName5", 50))

totalVotes = 0
prevVotes = 0
winner = ""
for candidate in candidateList:
totalVotes += candidate.getVotesRcvd()
if (candidate.getVotesRcvd() > prevVotes):
winner = candidate.getName()
prevVotes = candidate.getVotesRcvd()

print("Print all candidate name")
for candidate in candidateList:
print("Name:" , candidate.getName(), ",and % of Votes is:", (candidate.getVotesRcvd()/totalVotes)*100,"%")

print()
print("Winner name of election is")
print(winner)

  
# Run the main function. This should be the last statement in the file.
main()

*************************************************
Output of Program is:
Print all candidate name
Name: fName1 lName1 ,and % of Votes is: 5.0 %
Name: fName2 lName2 ,and % of Votes is: 10.0 %
Name: fName3 lName3 ,and % of Votes is: 37.5 %
Name: fName4 lName4 ,and % of Votes is: 22.5 %
Name: fName5 lName5 ,and % of Votes is: 25.0 %

Winner name of election is
fName3 lName3

Add a comment
Know the answer?
Add Answer to:
Write a Python program that creates a class which represents a candidate in an election. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Write a program for C++ )that allows the user to enter the last names of five...

    (Write a program for C++ )that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate    Votes Received    % of Total Votes Johnson            5000               25.91...

  • c++ Votes received by the candidate. Your program should also output the winner of the election....

    c++ Votes received by the candidate. Your program should also output the winner of the election. A sample output is: The Winner of the Election is Duffy. The input data should come from the file. Use 3 separate arrays to store the names, votes, and percentages before they are displayed. Include functions inputValues to input values for candidate names and votes received and place them in arrays. calcPercents which fills an array with the percentage of votes each candidate got....

  • COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize...

    COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize votes for a local election Specification: Write a program that keeps track the votes that each candidate received in a local election. The program should output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. To solve this problem, you will use one dynamic...

  • Using C language, write a program for the following: In a student representative council election, there...

    Using C language, write a program for the following: In a student representative council election, there are ten (10) candidates. A voter is required to vote a candidate of his/her choice only once. The vote is recorded as a number from 1 to 10. The number of voters are unknown beforehand, so the votes are terminated by a value of zero (0). Votes not within and not inclusive of the numbers 1 to 10 are invalid (spoilt) votes. A file,...

  • Java program Candidate Class: This class records the information for each candidate that is running for...

    Java program Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are running for the party they represent total votes a boolean value to record if they won Methods: Custom constructor that accepts first and last name, office and party Custom constructor that accepts an instance of another customer Getters for all instance variables and setters for total votes and the boolean variable toString: This method...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • It’s almost election day and the election officials need a program to help tally election results....

    It’s almost election day and the election officials need a program to help tally election results. There are two candidates for office—Polly Tichen and Ernest Orator. The program’s job is to take as input the number of votes each candidate received in each voting precinct and find the total number of votes for each. The program should print out the final tally for each candidate—both the total number of votes each received and the percent of votes each received. Clearly...

  • Write a program that supports the three phases (setup, voting and result-tallying) which sets up and...

    Write a program that supports the three phases (setup, voting and result-tallying) which sets up and executes a voting procedure as described above. In more details: You can start with the given skeleton (lab6_skeleton.cpp). There are two structures: Participant structure, which has the following members: id -- an integer variable storing a unique id of the participant (either a candidate or a voter). name -- a Cstring for storing the name of the participant.. hasVoted -- a boolean variable for...

  • Write a Python class, Circle, constructed by a radius and two methods which will compute the...

    Write a Python class, Circle, constructed by a radius and two methods which will compute the area and the perimeter of a circle. Include the constructor and other required methods to set and get class attributes. Create instances of Circle and test all the associated methods. Write a Python class, Rectangle, constructed by length and width and a method which will compute the area of a rectangle. Include the constructor and other required methods to set and get class attributes....

  • Use python to create this program. 1) Employee Class Write an Employee class that keeps data...

    Use python to create this program. 1) Employee Class Write an Employee class that keeps data attributes for the following pieces of information: Note: For all classes, the attributes must be hidden Employee Name Employee Number Hire Date Create accessors and mutators Attributes should be hidden. Create a class attribute that determines a standard pay rate adjustment 4% for raises 2) ProductionWorker Class Write a class named ProductionWorker that is a subclass of Employee that holds the following attributes .Shift...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT