Question
***NO IF STATEMENTS CAN BE USED *** JAVA NEED HELP WITH QUESTION 7 TEST 5 AND QUESTION 9 ONLY There are some constants: public static
***NO IF STATEMENTS CAN BE USED *** JAVA
NEED HELP WITH QUESTION 7 TEST 5 AND QUESTION 9 ONLY
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.=
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"); 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");
***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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started