-3

Write a Battery class that models a rechargeable battery. A battery has a constructor public Battery(float capacity) where capacity is a value measure in milliampere hours. A typical AA battery has a capacity of 2000 to 3000 mAh.

The method public void drain(float 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 float getRemainingCapacity() returns the remaining capacity of the battery. Create a class BatteryTest that exercises the methods of your Battery class.

I need help writing a test for the class. I wrote the code for the class, but I can't figure out how to write a test for it.

public class Battery 

{

float fullCharge = 3000;
float batteryCapacity;

Battery() 
   {
    fullCharge = 3000;
    }

public Battery(float capacity) 
    {

    batteryCapacity = capacity;
    fullCharge = capacity;

     }

public void charge()
    {
    batteryCapacity = fullCharge;
    }

   public void drain(float amount) 
   {
    batteryCapacity = batteryCapacity - amount;
    }

   public float getRemainingCapacity()
   {
    return batteryCapacity;
   }
}

So, I tried writing a test and I got:

 public class BatteryTest
{
 public static void main( String[] args )
 {
  Battery battery = new Battery(2500);

  battery.drain(2500);
  battery.charge();

  float capacity = battery.getRemainingCapacity();


  }

}

But, I'm not really sure. I'm fairly new to this.

Grey
  • 49
  • 1
  • 7
  • 1
    Try. Something. First. Anything. – Hovercraft Full Of Eels Sep 21 '15 at 02:18
  • Do you mean something like public static void main(String[] args){ Battery battery=new Battery(1.0); ... } ? – ggrr Sep 21 '15 at 02:19
  • Yes, a test like public static void... I'm fairly new to this and I can write the code for a class, such as I did, but I simply can't figure out how I would write the test for this. I understand it would be something like public class BatteryTest { public static void main( String[] args ) { for the beginning I don't know how to implement the methods into the test. – Grey Sep 21 '15 at 02:27
  • Please read the getting started information: http://stackoverflow.com/tour – Marcello B. Sep 21 '15 at 02:29
  • Maybe good approach for your problem is using unit testing like Junit. http://stackoverflow.com/questions/8751553/how-to-write-a-unit-test should helps you a bit – irvana Sep 21 '15 at 02:48
  • @HovercraftFullOfEels Eels! Why'd it have to be eels! – Sipty Jan 20 '16 at 13:09

1 Answers1

0

A proper test of a class showcases the use of its methods, and demonstrates how those methods handle any boundary cases.
What you have already is very good. I would suggest outputting (System.out.println) the battery's capacity after each method (including the constructor) to show that it has in fact been set to the expected value.
These methods do not have any argument validation, therefore there are no boundary cases to test.

If you were writing a UnitTest, each method of the Battery class would have its own test-method in a test-class that followed these 3 steps:

  1. Setup the data
  2. Invoke the method to be tested
  3. Assert that the actual value is the same as the expected one
DarkSigma
  • 416
  • 2
  • 9