Write a class Battery that models a rechargeable battery.
Capacity: double
String company;
· having no parameter – for setting values to zero or null.
· having two parameters for assigning values to both data members.
· Overload the above constructor and use this keyword to set the values of data members
· Provide getters and setters for data member(s).
· 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.
· A method toString to print object data.
1. Inside main, create 3 objects.
a. Set values of first object using constructor.
b. Take data from user and set the values for the 2 objects and display values.
2. Call all the above methods (drain, charge) and display updated values.
public class Battery
{
private final int Maximumlimit = 100;
private double fullycharged;
private double B_capacity;
public Battery(double capacity)
{
if(capacity <= Maximumlimit)
{
B_capacity = capacity;
fullycharged = capacity;
}
else
{
throw new IllegalArgumentException("Error! Battery capacity out of range: " + this.B_capacity + " expected range 0 <= batteryCapacity < " + MAX_BATTERY_LIMIT);
}
}
public void drain(double amount)
{
B_capacity = B_capacity - amount;
if(B_capacity < 0)
B_capacity = 0;
}
public void charge()
{
B_capacity = fullycharged;
}
public double getRemainingCapacity()
{
return B_capacity;
}
}
Comments
Leave a comment