Handson Exam
Soldiers
Write a Soldier class (Soldier.java) for the following
program .
A Soldier object represents a soldier with a gun.
He has some money (1000 pesos) to buy ammunition
(also called bullets or rounds) and
a pistol with a bullet capacity of 6.
The soldier will also have supply of bullets.
(bullets that are not in the gun).
Methods to be supported are buyAmmo, reload, and fire.
At any point,
we would like to be able to find out how much
money is left,
how many bullets are left in the gun,
the level of bullet supply,
and the total number of bullets fired.
For the sample program, this output should result.
Use BlueJ so that you could test your Soldier objects visually.
Include the
SoldierSimulation program
in the BlueJ project so that you can test and compare
against the expected output
(Create a SoldierSimulation class,
and then cut and paste the code from the link).
You should be able to infer which methods
need to be present in the Soldier class from the
SoldierSimulation program.
Nevertheless, the methods are:
- buyAmmo: buy bullets at a fixed price of 25.75 per bullet.
If there isn't enough money for the bullet purchase,
reject the purchase.
The argument is an int parameter indicating the number
of bullets to be bought.
- reload: reload the gun to its bullet capacity (of 6 bullets).
- fire: fire the number of bullets indicated in the argument.
If the number of bullets to be fired exceeds the number of bullets
left in the gun, just fire all the bullets that remain in the gun.
- getCashLeft:
returns the total cash left with the soldier.
- getRoundsLeftInGun:
returns the number of bullets left in the soldier's pistol.
- getRoundsSupply:
returns the number of bullets left with the soldier
(but not in the gun).
- getBulletsFired:
returns the total number of bullets fired.
You do not have to worry about negative-valued parameters.
Zip your BlueJ project (name it HO1xxxxxx.zip where xxxxxx is your id number)
and submit through moodle