Question

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

in java


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

Homework Answers

Answer #1
class Battery {

    private double initialCapacity;
    private double remainingCapacity;

    public Battery(double capacity) {
        this.initialCapacity = capacity;
        remainingCapacity = capacity;
    }

    public void drain(double amount) {
        remainingCapacity -= amount;
        if (remainingCapacity < 0)
            remainingCapacity = 0;
    }

    public void charge() {
        remainingCapacity = initialCapacity;
    }

    public double getRemainingCapacity() {
        return remainingCapacity;
    }
}

class BatteryTest {

    public static void main(String[] args) {
        Battery battery = new Battery(2000);
        battery.drain(500);
        System.out.println("Remaining capacity is " + battery.getRemainingCapacity());
    }
}

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
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its...
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its variables (firstName and lastName). It should contain an abstract method called payPrint. Below is the source code for the Employee9C superclass: public class Employee9C {    //declaring instance variables private String firstName; private String lastName; //declaring & initializing static int variable to keep running total of the number of paychecks calculated static int counter = 0;    //constructor to set instance variables public Employee9C(String...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
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...
Write a pseudocode for the following java programs: public class Item {    private String name;...
Write a pseudocode for the following java programs: public class Item {    private String name;    private double cost;    public Item(String name, double cost) {        this.name = name;        this.cost = cost;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getCost() {        return cost;    }    public void setCost(double cost) {...
Write code for a JAVA class for a "Car". The Car class has 2 properties: modelType...
Write code for a JAVA class for a "Car". The Car class has 2 properties: modelType and currentSpeed. Make sure to write code for instance variables (the properties), getters/setters and a default constructor, overloaded constructor. ALSO, include one method called "accelerate" that increases the speed by "1.0" miles per hour
in java Implement a class Balloon. A balloon starts out with radius 0. Supply a method...
in java Implement a class Balloon. A balloon starts out with radius 0. Supply a method public void inflate(double amount) that increases the radius by the given amount. Supply a method public double getVolume() that returns the current volume of the balloon; volume of a balloon is represented as: V = (4/3) * PI * r^3 Use Math.PI for the value of π. To compute the cube of a value r, you can use Math.pow (https://www.tutorialspoint.com/java/lang/math_pow.htm) or r*r*r Write a...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
We encourage you to work in pairs for this challenge to create a Student class with...
We encourage you to work in pairs for this challenge to create a Student class with constructors. First, brainstorm in pairs to do the Object-Oriented Design for a Student class. What data should we store about Students? Come up with at least 4 different instance variables. What are the data types for the instance variables? Write a Student class below that has your 4 instance variables and write at least 3 different constructors: one that has no parameters and initializes...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial capacity of the ArrayDeque. * * DO NOT MODIFY THIS VARIABLE. */ public static final int INITIAL_CAPACITY = 11; // Do not add new instance variables or modify existing ones. private T[] backingArray; private int front; private int size; Q1: write a method that constructs a new arrayDeque called "public ArrayDeque()" Q2: write a method called "public void addFirst(T data)" that adds an element...