CIS 435 Lab 3

Points: 25 Due Date: Friday January 10, 2003

You will take some existing code and add new features to it. You will also add some new testing of the code. The starting program for this is the flashlight code we went over in class. You should use the code in this zip file rather than the code in examples. This includes the .class files that I built. Ignore the makefile and run files.

I also have this zip file that contains the class files for a solution to the problem. You should be able to run the program using

		java Flashlight

First

First, add a new bulb type called LED. It's rating is 10 times the rating for the TUNGSTEN bulb.

Second

Add a new data field to the Battery class. It is called power and is a double. The initial power value for a battery is its amperage times the voltage and divided by 5. There is no actual electrical justification for this, its just something you have to figure out.

Add a method that takes no arguments and returns nothing. It reduces the power in the battery by 10% of its current value. So the first time it is called, it reduces the power from 100 to 90. Once the value has gone below 1.0, set it to 0.

You will also need a method to get the current power level.

Third

You will add a new class called Wires. The represents the connections between the other parts of the flashlight. There are two kinds of wires, copper and aluminum. You will need constructors, constants and methods to get the status of the wires object. You will need a method called checkwires. It returns true or false. It will first check a random number to tell if the wires are good or bad and return the result. To generate a random number, you can do this:
double r = Math.random();
if(r < 0.5) 
// whatever you will do if the wires are good
else
// whatever you will do if the wires are bad

You will have to add a Wires object to the Light class.

Fourth

Now for the testing. The Flashlight class has the main() for the program. Right now, it just creates a Light object and toggles the switch. I want you to change the pressSwitch() method in the Light class. Every time it is called, it will use the following methods to update the state of the flashlight. It will call checkBulb() in the Bulb to update the lifetime. Pass any value you want as the argument. Check the return code to see if the bulb failed.

It will call adjustpower() on each of the batteries. It will then check the power levels on each battery. If any of them are 0, the light fails.

Lastly, it will call check checkWires() on the Wires object To see if the wires failed.

If any of these checks fail, then the method (pressSwitch()) returns false. It should also print a message telling what failed.

Back in the main(), you will create a light, like it already does. Then you will press the switch on the light until it fails. Count how many times it took until it failed.

Extra Credit:

You can do the following for an extra 5 points. Create an array of lights in the main() with different settings. You should create at least 5 lights. Then test them all to failure as above, reporting the results. Be sure to print the settings for the light along with the number of times you pressed the button.

Deliverables:

You should turn in a listing of the program. Staple the cover sheet to that.

Notes

Think about how you will do this so you can ask questions.
ASK if you have any questions. Use the existing code as a model for new classes. Remember to update comments and printouts if things are changed.