Question

Using Python3: Create and test a class called water_bottle. This class should have a constructor (init...

Using Python3:

Create and test a class called water_bottle.

This class should have a constructor (init method) that accepts a size value (the number of ounces the water bottle will hold)

This class must also have the following methods: 1) drink (which reduces the amount of fluid by 1 ounce) 2) sip (which reduces the amount of fluid by 0.5 ounces) 3) gulp (which reduces the amount of fluid by 2 ounces) 4) fill (which fills the bottle to it's maximum capacity) 5) empty (which empties the bottle) 6) A __str__ method so that the water_bottle object can be printed (it should simple return the number of ounces remaining in the water bottle)

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

Program:

class water_bottle:
    #Constructor that accepts the amount of water the bottle can hold
    def __init__(self, size):
        #store the value in bottleCapacity and remainingWater
        self.bottleCapacity = size
        self.remainingWater = self.bottleCapacity

    #drink method that reduces the water in the bottle by 1 ounce
    def drink(self):
        self.remainingWater = self.remainingWater - 1

    #sip method that reduces the water in the bottle by 0.5 ounce
    def sip(self):
        self.remainingWater = self.remainingWater - 0.5

    #gulp method that reduces the water in the bottle by 2 ounce
    def gulp(self):
        self.remainingWater = self.remainingWater - 2

    #fill method that fill sets the water to the bottleCapacity
    def fill(self):
        self.remainingWater = self.bottleCapacity

    #empty method that empties the water in the bottle
    def empty(self):
        self.remainingWater = 0

    #method that displays the water remaining in the bottle
    def __str__(self):
        print("The number of ounces of water in the water bottle: " + str(self.remainingWater))

#create an object to the water_bottle class
myWaterBottle = water_bottle(15)
#call the __str__ method
myWaterBottle.__str__()
print("\nCalling the drink() method..")
#call the drink method
myWaterBottle.drink()
#call the __str__ method
myWaterBottle.__str__()
print("\nCalling the sip() method..")
#call the sip method
myWaterBottle.sip()
#call the __str__ method
myWaterBottle.__str__()
print("\nCalling the gulp method..")
#call the gulp method
myWaterBottle.gulp()
#call the __str__ method
myWaterBottle.__str__()
print("\nCalling the empty() method..")
#call the empty method
myWaterBottle.empty()
#call the __str__ method
myWaterBottle.__str__()
print("\nCalling the fill() method..")
#call the fill method
myWaterBottle.fill()
#call the __str__ method
myWaterBottle.__str__()

Output:

Code Screenshot:

Add a comment
Know the answer?
Add Answer to:
Using Python3: Create and test a class called water_bottle. This class should have a constructor (init...
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 class named Month. The class should have an int field named monthNumber that holds...

    Write a class named Month. The class should have an int field named monthNumber that holds the number of the month. For example. January would be 1. February would be 2. and so forth. In addition, provide the following methods A no-arg constructor that sets the monthNumber field to 1 A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument. If a value less than...

  • Code should be in C# Create a class called SavingsAccount.  Use a static variable called annualInterestRate to...

    Code should be in C# Create a class called SavingsAccount.  Use a static variable called annualInterestRate to store the annual interest rate for all account holders.  Each object of the class contains a private instance variable savingsBalance, indicating the amount the saver currently has on deposit. Provide method CalculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12 – this interest should be added to savingsBalance.  Provide static method setAnnualInterestRate to set the annualInterestRate to a new value....

  • Create a java class for an object called Student which contains the following private attributes: a...

    Create a java class for an object called Student which contains the following private attributes: a given name (String), a surname (family name) (String), a student ID number (an 8 digit number, String or int) which does not begin with 0. There should be a constructor that accepts two name parameters (given and family names) and an overloaded constructor accepting two name parameters and a student number. The constructor taking two parameters should assign a random number of 8 digits....

  • Programming Problem: define a C++ Bucket class with the following methods: Bucket() constructor that takes two...

    Programming Problem: define a C++ Bucket class with the following methods: Bucket() constructor that takes two parameters, a string name and an int parameter, the bucket capacity print) takes no paramers. Prints the bucket name, capacity and contents. Returns void. fill) that takes no parameters, fills the bucket to capacity. Returns void. empty ) that takes no parameters, empties the bucket. Returns void pourinto) takes one parameter, a reference to a Bucket object, returns an int that is the number...

  • Implement a class named PlayStack. An instance of this class should have a property cards, which...

    Implement a class named PlayStack. An instance of this class should have a property cards, which is a list of ordered cards that behaves like a stack. This property should not be directly accessible from outside the class. An instance of this class should behave like a stack, but it is a special stack as you will see below. You can only add cards from one end. Implement a method peekValue() which returns the value of the card on top...

  • Question set 1 (Java) object oriented: 1.Write a class called Student. The class should be able...

    Question set 1 (Java) object oriented: 1.Write a class called Student. The class should be able to store information regarding the name, age, gpa, and phone number of a Student object. Please write all the setter and getter methods. Finally, write a Demo class to demonstrate the use of the class by creating two different objects. 2.Use the same Student class from the previous problem. This time,you will write a different Demo class, in which you will create three Student...

  • Create a class called Reverse that reverses an array of integers. The class should have two...

    Create a class called Reverse that reverses an array of integers. The class should have two method: - public static void reverse(int [] array) – Which calls the private recursive method, passing the array parameter and which two array elements should be swapped. - private static void reverseRecursive(int [] array, int startIndex, int endIndex) – Which swaps the two elements of the array parameter pointed to by the startIndex and endIndex parameters. The method should be called again, this time,...

  • Application should be in C# programming language Create a class called SavingsAccount.  ...

    Application should be in C# programming language Create a class called SavingsAccount.  Use a static variable called annualInterestRate to store the annual interest rate for all account holders.  Each object of the class contains a private instance variable savingsBalance, indicating the amount the saver currently has on deposit. Provide method CalculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12 – this interest should be added to savingsBalance.  Provide static method setAnnualInterestRate to set the...

  • The program needs to be in python : First, create a BankAccount class. Your class should...

    The program needs to be in python : First, create a BankAccount class. Your class should support the following methods: class BankAccount (object): """Bank Account protected by a pin number.""" def (self, pin) : init "n"Initial account balance is 0 and pin is 'pin'."" self.balance - 0 self.pin pin def deposit (self, pin, amount): """Increment account balance by amount and return new balance.""" def withdraw (self, pin, amount): """Decrement account balance by amount and return amount withdrawn.""" def get balance...

  • Course Class Create a class called Course. It will not have a main method; it is...

    Course Class Create a class called Course. It will not have a main method; it is a business class. Put in your documentation comments at the top. Be sure to include your name. Course must have the following instance variables named as shown in the table: Variable name Description crn A unique number given each semester to a section (stands for Course Registration Number) subject A 4-letter abbreviation for the course (e.g., ITEC, PHED, CHEM) number A 4-digit number associated...

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