Question

In: Computer Science

Using Python 3, Write a class called GolfScore that keeps track of the score for a...

Using Python 3,

Write a class called GolfScore that keeps track of the score for a person playing a round of golf. We will limit the round to 9 holes, just to make testing easier. As a reminder, the holes on the golf course are numbered 1 – 9.

Create a class level variable named NUM_OF_HOLES set to 9.

Methods to be written:

The constructor – sets the score for all holes to -1

addScore (hole, score) – this method sets the score for the specified hole. This method should not accept any value if the number of the hole is greater than NUM_OF_HOLES (the round is finished, why would be allow any more scores to be entered?) or less than 1.

getScore (hole) – returns the score from the specified hole. Returns -1 if the hole has not been golfed/entered or if the specified hole is not in the valid range of 1 through 9.

getTotalScore() – returns the total score of the round so far.

getNumHolesPlayed() – returns the number of complete holes so far.

winning(otherGolfer) – compares “your” score to the total of the other golfer. Return True if your score is less than the score of the other golfer. Otherwise return False. Remember, in golf a small score is desired. You can assume that the other golfer than entered the same number of scores.

losing(otherGolfer) – compares “your” score to the total of the other golfer. Return True if your score is greater than the score of the other golfer. Otherwise return False. You can assume that the other golfer than entered the same number of scores.

continueRound() – returns True is the number of rounds completed/entered is less than the variable NUM_OF_HOLES. Otherwise returns False.

avgScore() – returns the average of the holes played. For example, if 6 holes have been played and the total score so far is 45, this method would return 7.5.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#code

#required class
class GolfScore:
    #class variable for storing number of holes
   
NUM_OF_HOLES=9
   
    #constructor
   
def __init__(self):
        #initializing a list containing NUM_OF_HOLES number of values, all initialized to -1
       
self.__scores=[-1 for i in range(self.NUM_OF_HOLES)]
   
    #adds a Score to a hole
   
def addScore(self, hole, score):
        #checking if hole value is valid and has not set before
       
if hole>=1 and hole<=self.NUM_OF_HOLES and self.__scores[hole-1]==-1:
            #setting score to specified hole
           
self.__scores[hole-1]=score
   
    #returns the score for a hole
   
def getScore(self, hole):
        #validating hole
       
if hole>=1 and hole<=self.NUM_OF_HOLES:
            #returning the value
           
return self.__scores[hole-1]
        return -1 #invalid hole
   
    #returns the total score
  
def getTotalScore(self):
        s=0
        #summing up all scores that are not -1
       
for i in self.__scores:
            if i!=-1:
                s+=i
        return s
   
    #returns the number of holes played
   
def getNumHolesPlayed(self):
        c = 0
        #counting the number of holes that has score not equal to -1
       
for i in self.__scores:
            if i != -1:
                c += 1
        return c
   
    #returns True if score < other golfer's score
   
def winning(self,otherGolfer):
        return self.getTotalScore()<otherGolfer.getTotalScore()

    # returns True if score > other golfer's score
   
def losing(self,otherGolfer):
        return self.getTotalScore()>otherGolfer.getTotalScore()
   
    #returns True if there are more holes to play
   
def continueRound(self):
        return self.getNumHolesPlayed()<self.NUM_OF_HOLES
   
    #returns the average score
   
def avgScore(self):
        avg=0
        #finding average only if NumHolesPlayed is greater than 0
       
if self.getNumHolesPlayed()>0:
            avg=self.getTotalScore()/self.getNumHolesPlayed()
        return avg


Related Solutions

Write C++ a program that shows a class called gamma that keeps track of how many...
Write C++ a program that shows a class called gamma that keeps track of how many objects of itself there are. Each gamma object has its own identification called ID. The ID number is set equal to total of current gamma objects when an object is created. Test you class by using the main function below. int main()    {    gamma g1;    gamma::showtotal();    gamma g2, g3;    gamma::showtotal();    g1.showid();    g2.showid();    g3.showid();    cout <<...
Write a class called Animal that contains a static variable called count to keep track of...
Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
Write a python program using the following requirements: Create a class called Sentence which has a...
Write a python program using the following requirements: Create a class called Sentence which has a constructor that takes a sentence string as input. The default value for the constructor should be an empty string The sentence must be a private attribute in the class contains the following class methods: get_all_words — Returns all the words in the sentence as a list get_word — Returns only the word at a particular index in the sentence Arguments: index set_word — Changes...
create a program that asks user math questions and keeps track of answers... Python Allow the...
create a program that asks user math questions and keeps track of answers... Python Allow the user to decide whether or not to keep playing after each math challenge. Ensure the user’s answer to each math problem is greater than or equal to zero. Keep track of how many math problems have been asked and how many have been answered correctly. When finished, inform the user how they did by displaying the total number of math problems, the number they...
The following class keeps track of how many flavors are at an ice-cream place, and the...
The following class keeps track of how many flavors are at an ice-cream place, and the number of calories of each flavor. The calories of each flavor ice-cream are positive values of type unsigned int and are stored as a dynamically allocated array. The first data member is a pointer that will point to the first element of the array. This array will be of an arbitrary size. The default size of the array shall be 25, but may be...
Using Python 3 Write a function reads the file called simpleinterest.txt. Each row in simpleinterest.txt is...
Using Python 3 Write a function reads the file called simpleinterest.txt. Each row in simpleinterest.txt is a comma seperated list of values in the order of PV, FV, n, r. For each row, there is one value missing. Write an output file that fills in the missing value for each row.
Write a C++ program for Euclids Algorithm that keeps track of the number of iterations (%...
Write a C++ program for Euclids Algorithm that keeps track of the number of iterations (% & loop) 1. Euclid’s Algorithm An alternative of the Euclidean algorithm for finding greatest common divisors (GCD) is repeatedly performing the modulo operation on two numbers until the remainder is 0. Here is the pseudocode for find the GCD of two positive numbers a and b using the Euclidean algorithm :while b ≠ 0 temp = b b = a mod t a =...
Hello! Working with python This program asks user math questions and keeps track of answers... Allow...
Hello! Working with python This program asks user math questions and keeps track of answers... Allow the user to decide whether or not to keep playing after each math challenge. Ensure the user’s answer to each math problem is greater than or equal to zero. Keep track of how many math problems have been asked and how many have been answered correctly. When finished, inform the user how they did by displaying the total number of math problems, the number...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
3. write a program that uses a class called "garment" that is derived from the class...
3. write a program that uses a class called "garment" that is derived from the class "fabric" and display some values of measurement. 20 pts. Based on write a program that uses the class "fabric" to display the square footage of a piece of large fabric.           class fabric            {                private:                    int length;                    int width;                    int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT