Programming Project 9.22.5: •. Write a class Battery that models a rechargeable battery. A battery has a constructor Battery(double capacity) where capacity is a value measured in milliampere hours. A typical AA battery has a capacity of 2000 to 3000 mAh. The member function void drain(double amount) drains the capacity of the battery by the given amount. The member function void charge() charges the battery to its original capacity. The member function double get_remaining_capacity() gets the remaining capacity of the battery.

Report
Question

Please briefly explain why you feel this question should be reported.

Report
Cancel

Programming Project 9.22.5: •. Write a class Battery that models a rechargeable battery. A battery has a constructor Battery(double capacity) where capacity is a value measured in milliampere hours. A typical AA battery has a capacity of 2000 to 3000 mAh. The member function void drain(double amount) drains the capacity of the battery by the given amount. The member function void charge() charges the battery to its original capacity. The member function double get_remaining_capacity() gets the remaining capacity of the battery.

MathJax Example

Answer ( 1 )

  1. Please briefly explain why you feel this answer should be reported.

    Report
    Cancel
    /* #include 
    using namespace std;
    
    // Battery class to represent a battery with a certain capacity
    class Battery {
        // Private variables to store the battery's capacity and current balance
      private:
          double capacity;  // Maximum capacity of the battery
          double balance;   // Current remaining capacity of the battery
      
      public:
          // Argumented constructor to initialize the battery with a given capacity
          Battery(double cap) {
              capacity = cap;  // Set the maximum capacity
              balance = cap;   // Initialize the balance to the maximum capacity
          }
          
          // Function to drain the battery by a specified amount
          void drain(double amount) {
              balance -= amount;  // Reduce the balance by the drain amount
              if (balance < 0) {  // Ensure the balance doesn't go below zero
                  balance = 0;
              }
          }
          
          // Function to charge the battery to its full capacity
          void charge() {
              balance = capacity;  // Reset the balance to the maximum capacity
          }
          
          // Function to return the current remaining capacity of the battery
          double get_remaining_capacity() {
              return balance;  // Return the current balance
          }
    };
    
    int main() {
        // Create a Battery object with an initial capacity of 2500
        Battery battery(2500);
    
        // Display the initial remaining capacity of the battery
        cout << "Capacity is " << battery.get_remaining_capacity() << endl;
    
        // Drain the battery by 350 units
        battery.drain(350);
        // Display the remaining capacity after draining
        cout << "Capacity is " << battery.get_remaining_capacity() << endl;
    
        // Charge the battery to its full capacity
        battery.charge();
        // Display the remaining capacity after charging
        cout << "Capacity is " << battery.get_remaining_capacity() << endl;
    
        return 0;  // End of the program
    } */

Leave an answer

Browse

By answering, you agree to the Terms of Service and Privacy Policy.