Question

Python ------------------------- ### Description In this exercise, you will add to your `Battery` class a method...

Python
-------------------------

### Description

In this exercise, you will add to your `Battery` class
a method to drain the battery.

### Class Name

`Battery`

### Method

`drain()`

### Parameters

* `self` : the `Battery` object to use
* `milliamps` : a number, the amount of charge to remove from the battery.

### Action

Removes `milliamps` from the charge of the object. If the
new charge is less than 0, then set the charge to 0.

Only do this action of `milliamps` is positive, and
the `Battery` isn't already at 0 charge.

### Return Value

`True` if a change occurred, `False` otherwise.

class Battery:
def __init__(self, capacity):
self.capacity = capacity
  
def getCapacity(self):
return self.capacity
  
def getCharge(self):
return self.capacity
  
def drain(self, milliamps):
self.milliamps = milliamps
if self.capacity < 0:
return False
else:
return True

Homework Answers

Answer #1

I have completed your class, there was one variable missing, charge , which I added. I have also added some statements to test


class Battery:
  def __init__(self, capacity, charge):
    self.capacity = capacity
    self.charge = charge
  
  def getCapacity(self):
    return self.capacity
  
  def getCharge(self):
    return self.charge
  
  def drain(self, milliamps):
    if milliamps > 0 and self.charge != 0:
      self.charge = self.charge - milliamps
      if self.charge < 0:
        self.charge = 0
      return True
    else:
      return False


b = Battery(20, 18)
print("Current Charge:", b.charge)
b.drain(10)
print("After draining 10 milliamps, Charge:", b.charge)
Know the answer?
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
Similar Questions
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None:...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None: """This method adds new value to the tree, maintaining BST property. Duplicates must be allowed and placed in the right subtree.""" Example #1: tree = BST() print(tree) tree.add(10) tree.add(15) tree.add(5) print(tree) tree.add(15) tree.add(15) print(tree) tree.add(5) print(tree) Output: TREE in order { } TREE in order { 5, 10, 15 } TREE in order { 5, 10, 15, 15, 15 } TREE in order {...
Please answer the following as soon as possible. Thank you. Add the top method in class...
Please answer the following as soon as possible. Thank you. Add the top method in class Stack to the following python code which returns the top item of the stack. Test it. Design top() method using a single queue as an instance variable, and only constant additional local memory within the method bodies. python code: class Stack: def __init__(self): self.q = Queue() def is_empty(self): return self.q.is_empty() def push(self, data): self.q.enqueue(data) def pop(self): for _ in range(self.q.get_size() - 1): dequeued =...
I need some tests for this python class. some tests for some of the functions in...
I need some tests for this python class. some tests for some of the functions in this class like. def __init__ def __eq__ def __lt__ def __hash__ I am using Pycharm and useing Pytest to write some tests but have no idea how to write one --------------------------------------------------------------------------------------------------- class Movie: def __set_title_internal(self, title: str): if title.strip() == "" or type(title) is not str: self.__title = None else: self.__title = title.strip() def __set_release_year_internal(self, release_year: int): if release_year >= 1900 and type(release_year) is...
Hello, I created a class in Python and am struggling to figure out how to complete...
Hello, I created a class in Python and am struggling to figure out how to complete the last part. How can you verify that a date is before in a pythonic way? I am struggling with the operands and what to check first. Thank you! class Date(object): "Represents a Calendar date" def __init__(self, day=0, month=0, year=0): "Initialize" self.day = day self.month = month self.year = year def __str__(self): "Return a printable string representing the date: m/d/y" return f'{self.month}/{self.day}/{self.year}' def before(self,...
"""linkedQueue.py implements LinkedQueue""" class Node(object): def __init__(self, e, nextNode): self.item, self.next = (e, nextNode) def __str__(self):...
"""linkedQueue.py implements LinkedQueue""" class Node(object): def __init__(self, e, nextNode): self.item, self.next = (e, nextNode) def __str__(self): return str(self.item)    class LinkedQueue(object): def __init__(self): #O(1) self._front, self._rear =(None, None) self._size = 0    def enqueue(self, e): # add e to the rear of the queue. newTail = Node(e, None) if self.isEmpty(): self._front = newTail else: self._rear.next = newTail self._rear = newTail self._size += 1 def front(self): # raise exception if it is empty, otherwise return front element if self.isEmpty(): raise Exception("call...
Python: Complete the function create_bfs_graph using python. Do not use any libraries def create_bfs_graph(): """ Initializes...
Python: Complete the function create_bfs_graph using python. Do not use any libraries def create_bfs_graph(): """ Initializes the undirected graph from the lecture    Uses the add_edge method    Returns Graph object of the lecture graph Example use: >>> ex_graph = create_bfs_graph() >>> [x in ex_graph.children_of('Jared') for x in ['John', 'Helena', 'Donald', 'Paul']] [False, False, True, True] >>> ex_graph = create_bfs_graph() >>> [x in ex_graph.children_of('Helena') for x in ['John', 'Helena', 'Donald', 'Paul']] [True, False, False, True] """ # DON'T CHANGE ANYTHING...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if...
Python Problem 1 Create a function, biggest_ip_sum, outside of the ServerClass class, that: 1. Takes two...
Python Problem 1 Create a function, biggest_ip_sum, outside of the ServerClass class, that: 1. Takes two ServerClass objects 2. Sums the octets of the IP together (example 127.0.0.1 = 127+0+0+1 = 128) 3. Returns the larger number Example: server_one = ServerClass("127.0.0.1") server_two = ServerClass("192.168.0.1") result = biggest_ip_sum(server_one, server_two) print(result) In the example above, result will be 361. _________________________________ Problem 1 Here is the code to start with class ServerClass: """ Server class for representing and manipulating servers. """ def __init__(self,...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will look like: Enter number of players: 2 Player 1: 7S 5D - 12 points Player 2: 4H JC - 14 points Dealer: 10D Player 1, do you want to hit? [y / n]: y Player 1: 7S 5D 8H - 20 points Player 1, do you want to hit? [y / n]: n Player 2, do you want to hit? [y / n]: y...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...