Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

There are some constants: public static final int NUM_CHARGER_SETTINGS = 4; Number of external charger settings. Settings are numbered between 0 inclusive and 4 exclusive.

There are some constants: public static final int NUM_CHARGER_SETTINGS = 4; Number of external charger settings. Settings are numbered between 0 inclusive and 4 exclusive. public static final double CHARGE_RATE = 2.0; A constant used in calculating the charge rate of the external charger. public static final double DEFAULT_CAMERA_POWER_CONSUMPTION = 1.0; Default power consumption of the camera at the start of simulation.

There is one public constructor: public CameraBattery(double batteryStartingCharge, double batteryCapacity) Constructs a new camera battery simulation. The battery should start disconnected from both the camera and the external charger. The starting battery charge and maximum charge capacity are given. If the starting charge exceeds the batteries capacity, the batteries charge is set to its capacity. The external charger starts at setting 0. There are the following public methods: public void buttonPress() Indicates the user has pressed the setting button one time on the external charger. The charge setting increments by one or if already at the maximum setting wraps around to setting 0. public double cameraCharge(double minutes) Charges the battery connected to the camera (assuming it is connected) for a given number of minutes. The amount of charging in milliamp-hours (mAh) is the number minutes times the CHARGE_RATE constant. However, charging cannot exceed the capacity of the battery connected to the camera, or no charging if the battery is not connected. The method returns the actual amount the battery has been charged. public double drain(double minutes) Drains the battery connected to the camera (assuming it is connected) for a given number of minutes. The amount of drain in milliamp-hours (mAh) is the number of minutes times the cameras power consumption. However, the amount cannot exceed the amount of charge contained in the battery assuming it is connected to the camera, or no amount drain if the battery is not connected. The method returns the actual amount drain from the battery. public double externalCharge(double minutes) Charges the battery connected to the external charger (assuming it is connected) for a given number of minutes. The amount of charging in milliamp-hours (mAh) is the number minutes times the charger setting (a number between 0 inclusive and NUM_CHARGER_SETTINGS exclusive) the CHARGE_RATE constant. However, charging cannot exceed the capacity of the battery connected to the camera, or no charging if the battery is not connected. The method returns the actual amount the battery has been charged.

public void resetBatteryMonitor() Reset the battery monitoring system by setting the total battery drain count back to 0. public double getBatteryCapacity() Get the battery's capacity. public double getBatteryCharge() Get the battery's current charge. public double getCameraCharge() Get the current charge of the camera's battery. public double getCameraPowerConsumption() Get the power consumption of the camera. public int getChargerSetting() Get the external charger setting. public double getTotalDrain() Get the total amount of power drained from the battery since the last time the battery monitor was started or reset. public void moveBatteryExternal() Move the battery to the external charger. Updates any variables as needed to represent the move. public void moveBatteryCamera() Move the battery to the camera. Updates any variables as needed to represent the move. public void removeBattery() Remove the battery from either the camera or external charger. Updates any variables as needed to represent the removal. public void setCameraPowerConsumption(double cameraPowerConsumption) Set the power consumption of the camera.

Where's the main() method?? There isn't one! Like most Java classes, this isn't a complete program and you can't "run" it by itself. It's just a single class, that is, the definition for a type of object that might be part of a larger system. To try out your class, you can write a test class with a main method like the examples below in the getting started section. There is also a specchecker (see below) that will perform a lot of functional tests, but when you are developing and debugging your code at first you'll always want to have some simple test cases of your own, as in the getting started section below. Suggestions for getting started Smart developers don't try to write all the code and then try to find dozens of errors all at once; they work incrementally and test every new feature as it's written. Since this is our first assignment, here is an example of some incremental steps you could take in writing this class. 0. Be sure you have done and understood Lab 2. 1. Create a new, empty project and then add a package called hw1. Be sure to choose "Don't Create" at the dialog that asks whether you want to create module-info.java. 2. Create the CameraBattery class in the hw1 package and put in stubs for all the required methods, the constructor, and the constants. Remember that everything listed is declared public. For methods that are required to return a value, for now, just put in a "dummy" return statement that returns zero or false. There should be no compile errors. WARNING: be careful about COPY AND PASTE from this pdf! This may lead to insidious bugs caused by invisible characters that are sometimes generated by sophisticated document formats. 3. Briefly javadoc the class, constructor, and methods. This is a required part of the assignment anyway, and doing it now will help clarify for you what each method is supposed to do before you begin the actual implementation. (Copying phrases from the method descriptions here or in the Javadoc is acceptable, however, see waring above.) 4. Look at each method. Mentally classify it as either an accessor (returns some information without modifying the object) or a mutator (modifies the object, usually returning void). The accessors will give you a lot of hints about what instance variables you need. 5. You could start by deciding how to keep track of the cameras charge. The presence of a method getCameraCharge suggests an instance variable might be useful to keep track of the charge. Keep in mind the specification states the battery starts disconnected from the camera,

which means the cameras initial charge should be 0. In a separate class (and file), write a simple test to see if this much is working as it should: public static void main(String args[]) { CameraBattery cb = new CameraBattery(1000, 2000); System.out.println("Test 1:"); System.out.println("Battery charge is " + cb.getBatteryCharge() + " expected 1000.0"); System.out.println("Camera charge is " + cb.getCameraCharge() + " expected 0.0"); } (Tip: you can find code for the simple test case above, along with the others discussed in this section, in the class SimpleTests.java linked from the assignment page on Canvas.) 6. A call to moveBatteryCamera, models the battery begin connected to the camera. The cameras charge should be the same as the batteries charge. Think about how the move should update the instance variables you created in the previous steps. A test of the method might look like the following: // battery is connected to the camera for next tests cb.moveBatteryCamera(); System.out.println(); System.out.println("Test 2:"); System.out.println("Battery charge is " + cb.getBatteryCharge() + " expected 1000.0"); System.out.println("Camera charge is " + cb.getCameraCharge() + " expected 1000.0"); 7. Now you can start working on the drain method. This method models the camera draining the battery. The batterys (and cameras) charge must be decreased by the given number of minutes times the cameras power consumption. However, the charge cannot be decreased below 0. Think about the math library operation that can be used prevent a value going below a threshold amount (in this case 0). The total drain should also be updated to reflect how much the battery has been drained since it was created or last reset. // before calling drain System.out.println("Total drain is " + cb.getTotalDrain() + " expected 0.0"); System.out.println("Camera power consumption is " + cb.getCameraPowerConsumption() + " expected " + 1.0); double drained = cb.drain(100.0); System.out.println(); System.out.println("Test 4:"); System.out.println("Battery drained by " + drained + " expected 100.0"); System.out.println("Battery charge is " + cb.getBatteryCharge() + " expected 900.0");

System.out.println("Camera charge is " + cb.getCameraCharge() + " expected 900.0"); System.out.println("Total drain is " + cb.getTotalDrain() + " expected 100.0"); drained = cb.drain(1000.0); System.out.println(); System.out.println("Test 5:"); System.out.println("Battery drained by " + drained + " expected 900.0"); System.out.println("Battery charge is " + cb.getBatteryCharge() + " expected 0.0"); System.out.println("Camera charge is " + cb.getCameraCharge() + " expected 0.0"); System.out.println("Total drain is " + cb.getTotalDrain() + " expected 1000.0"); 8. The implementation of cameraCharge is very similar to drain. The battery (and the camera) is charged by the minutes times the CHARGE_RATE constant. However, the charge cannot exceed the batterys total capacity. double charged = cb.cameraCharge(50.0); System.out.println(); System.out.println("Test 6:"); System.out.println("Battery charged by " + charged + " expected 100.0"); System.out.println("Battery charge is " + cb.getBatteryCharge() + " expected 100.0"); System.out.println("Camera charge is " + cb.getCameraCharge() + " expected 100.0"); 9. You also need to implement the removeBattery and moveBatteryExternal methods. They are similar to moveBatteryCamera. // battery is removed for the next couple tests cb.removeBattery(); drained = cb.drain(50.0); System.out.println(); System.out.println("Test 7:"); System.out.println("Battery drained by " + drained + " expected 0.0"); System.out.println("Battery charge is " + cb.getBatteryCharge() + " expected 100.0"); System.out.println("Camera charge is " + cb.getCameraCharge() + " expected 0.0"); System.out.println("Total drain is " + cb.getTotalDrain() + " expected 1000.0"); cb.cameraCharge(50.0); System.out.println(); System.out.println("Test 8:"); System.out.println("Battery charge is " + cb.getBatteryCharge() + " expected 100.0"); System.out.println("Camera charge is " + cb.getCameraCharge() + " expected 0.0");

System.out.println("Total drain is " + cb.getTotalDrain() + " expected 1000.0"); // battery is moved to the external charger for the next few tests cb.moveBatteryExternal(); cb.externalCharge(50.0); System.out.println(); System.out.println("Test 9:"); System.out.println("External charger setting is " + cb.getChargerSetting() + " exptected 0"); System.out.println("Battery charge is " + cb.getBatteryCharge() + " expected 100.0"); System.out.println("Camera charge is " + cb.getCameraCharge() + " expected 0.0"); System.out.println("Total drain is " + cb.getTotalDrain() + " expected 1000.0"); cb.buttonPress(); cb.buttonPress(); cb.externalCharge(50.0); System.out.println(); System.out.println("Test 10:"); System.out.println("External charger setting is " + cb.getChargerSetting() + " exptected 2"); System.out.println("Battery charge is " + cb.getBatteryCharge() + " expected 300.0"); System.out.println("Camera charge is " + cb.getCameraCharge() + " expected 0.0"); System.out.println("Total drain is " + cb.getTotalDrain() + " expected 1000.0"); 10. Next is the buttonPress method. This method increments the setting by one each time it is called. The settings are 0, 1, 2, and 3. When setting 3 is reached, the setting returns back to 0. Again, think of the mathematical operations we have learned that could be used to implement this functionality. cb.buttonPress(); cb.buttonPress(); cb.externalCharge(50.0); System.out.println(); System.out.println("Test 10:"); System.out.println("External charger setting is " + cb.getChargerSetting() + " exptected 2"); System.out.println("Battery charge is " + cb.getBatteryCharge() + " expected 300.0"); System.out.println("Camera charge is " + cb.getCameraCharge() + " expected 0.0"); System.out.println("Total drain is " + cb.getTotalDrain() + " expected 1000.0"); cb.buttonPress(); cb.buttonPress(); cb.externalCharge(50.0);

System.out.println(); System.out.println("Test 11:"); System.out.println("External charger setting is " + cb.getChargerSetting() + " exptected 0"); System.out.println("Battery charge is " + cb.getBatteryCharge() + " expected 300.0"); System.out.println("Camera charge is " + cb.getCameraCharge() + " expected 0.0"); System.out.println("Total drain is " + cb.getTotalDrain() + " expected 1300.0");

***NO IF STATEMENTS CAN BE USED*** JAVA

NEED HELP WITH QUESTION 7 TEST 5 AND QUESTION 9 ONLY

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started