Question

In: Computer Science

Write a class Battery that models a rechargeable battery. A battery has a constructor public Battery(double...

Write a class Battery that models a rechargeable battery. A battery has a constructor

public 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 method

public void drain(double amount)

drains the capacity of the battery by the given amount. The method

public void charge()

charges the battery to its original capacity.

The method

public double getRemainingCapacity()

gets the remaining capacity of the battery.

Supply a Battery class that tests all methods. Make sure to print the remaining capacity.

If we create an instance of Battery class with 2000 mAh, call drain (500) and then call getRemainingCapacity(), it should print 1500mAh; now if we call charge() and then call getRemainingCapacity() it should print 2000mAh

Solutions

Expert Solution

Solution:

Code:

#include <iostream>
using namespace std;

class Battery{
  
   private:
   double capacity;
   double originalCapacity;
  
   public:
   Battery(double capacity){
       this->capacity = capacity;
       originalCapacity = capacity;
   }
  
   void drain(double amount){
       capacity -= amount;
       if(capacity < 0){
           capacity = 0;
       }
   }
  
   void charge(){
       capacity = originalCapacity;
   }
  
   double getRemainingCapacity(){
       return capacity;
   }
};

int main() {
  
   Battery b(2000);
   b.drain(500);
   cout << b.getRemainingCapacity() << "mAh" << endl;
   b.charge();
   cout << b.getRemainingCapacity() << "mAh" << endl;
   return 0;
}

Output:


Related Solutions

package hw; public class MyArrayForDouble { double[] nums; int numElements; public MyArrayForDouble() { // Constructor. automatically...
package hw; public class MyArrayForDouble { double[] nums; int numElements; public MyArrayForDouble() { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[5]; } public MyArrayForDouble(int capacity) { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[capacity]; } public MyArrayForDouble(double[] nums1) { nums = new double[nums1.length]; for(int i=0;i<nums1.length;i++) nums[i] = nums1[i]; numElements = nums1.length; } void printArray(){ // cost, times System.out.printf("printArray(%d,%d): ",numElements,nums.length); for(int i=0; i<numElements;i++) System.out.print(nums[i]+" "); System.out.println(); }...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting). Implement this class (if you wish you...
How do you write the constructor for the three private fields? public class Student implements Named...
How do you write the constructor for the three private fields? public class Student implements Named { private Person asPerson; private String major; private String universityName; @Override public String name() { return asPerson.name(); }
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
Define the following class: class XYPoint { public: // Contructors including copy and default constructor //...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor // Destructors ? If none, explain why double getX(); double getY(); // Overload Compare operators <, >, ==, >=, <=, points are compare using their distance to the origin: i.e. SQR(X^2+Y^2) private: double x, y; };
public class MyLinked {    static class Node {        public Node (double item, Node...
public class MyLinked {    static class Node {        public Node (double item, Node next) { this.item = item; this.next = next; }        public double item;        public Node next;    }    int N;    Node first;     // remove all occurrences of item from the list    public void remove (double item) {        // TODO    } Write the remove function. Do NOT add any fields to the node/list classes, do...
class DoubleLinkedList { public: //Implement ALL following methods. //Constructor. DoubleLinkedList(); //Copy constructor. DoubleLinkedList(const DoubleLinkedList & rhs);...
class DoubleLinkedList { public: //Implement ALL following methods. //Constructor. DoubleLinkedList(); //Copy constructor. DoubleLinkedList(const DoubleLinkedList & rhs); //Destructor. Clear all nodes. ~DoubleLinkedList(); // Insert function. Returns true if item is inserted, // false if the item it a duplicate value bool insert(int x); // Removes the first occurrence of x from the list, // If x is not found, the list remains unchanged. void remove(int x); //Assignment operator. const DoubleLinkedList& operator=(const DoubleLinkedList & rhs); private: struct node{ int data; node* next;...
QUESTION ONE: A company has set a goal of developing a rechargeable battery that lasts over...
QUESTION ONE: A company has set a goal of developing a rechargeable battery that lasts over 5 hours (300 minutes) in continuous use, on average. A random sample of 10 of these batteries measured the following lifespans (in minutes): 319, 294, 336, 353, 341, 329, 315, 329, 302, and 289. (a) Display the sample data in a stemplot and describe the distribution. (b) Is there convincing evidence that the company has met its goal? Provide statistical evidence to support your...
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT