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...
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 #:        ...
For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
By using Python: a. Make a class called Book. The __init__() method for Book should store...
By using Python: a. Make a class called Book. The __init__() method for Book should store two attributes: a title and an author. Make a method called book_info() that prints these two pieces of information, and a method called book_available() that prints a message indicating that the book is available in the library. b. Create an instance called book1 from your class. Print the two attributes individually, and then call both methods. c. Create three different instances from the class,...
Write a program IN PYTHON of the JUPYTER NOOTBOOK that keeps getting a set of numbers...
Write a program IN PYTHON of the JUPYTER NOOTBOOK that keeps getting a set of numbers from user until the user enters "done". Then shows the count, total, and average of the entered numbers. This should the answer when finished Enter a number: 55 Enter a number: 90 Enter a number: 12 Enter a number: done You entered 3 numbers, total is 157, average is 52.33333
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT