Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

anyone help me this assignment please. this java Tips from the experts: How to waste a lot of time on this assignment 1. Start the

anyone help me this assignment please. this java

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Tips from the experts: How to waste a lot of time on this assignment 1. Start the assignment the night it's due. That way, if you have questions, the TAs will be too busy to help you and you can spend the time tearing your hair out over some trivial detail. 2. Don't bother reading the rest of this document, or even the specification, especially not the "Getting started" section. Documentation is for losers. Try to write lots of code before you figure out what it's supposed to do. 3. Don't test your code. It's such fun to remain in suspense until it's graded! Overview The purpose of this assignment is to give you some practice with the process of implementing a class from a specification and testing whether your implementation conforms to the specification. You'll also get practice using variables and modular arithmetic. For this assignment you will implement one class, called CameraBattery, that models a removable and rechargeable camera battery. The battery can be charged both directly by the camera when connect to a USB port and by an external "wall wart" battery charger. However, the battery can only be in one place at a time, either connected to the camera, connected to the external charger, or disconnect for any device. The battery has a maximum capacity to which it can be charged. The rate of charge when connect to the camera is fixed, while the rate of charge when connected to the external charger is determined by the charger setting. The charger setting is a number between 0 inclusive and NUM_CHARGER_SETTINGS exclusive. The charger setting is set by the user repeatedly pressing a single settings button. Each press the setting increases by one. However, when the maximum setting is reached the next button press puts it back to setting 0 . The formulas that guide the rate of charging and discharging are as follows: increase in charge for camera charging = minutes CHARGE_RATE increase in charge for extrenal charging = minutes setting CHARGE_RATE decrease in charge for camrea power drain = minutes camera power consumption Special Assignment Requirements You should not use any conditional statements (i.e. "if" statements), loops, or anything else we haven't covered, for this assignment. The purpose of this assignment is for you to practice using variables and the math tools we have covered in various ways. Yes, it is possible to solve this assignment very neatly with just what we have learned, and the challenge of doing so will make you a better programmer! You will be penalized slightly for using conditional statements, keep in mind that if you really can't figure it out, it's better to turn in something working than nothing at all. Hints: To solve this assignment you will need to declare instance and local variables, that part of the program design is up to you. There will be a couple of places where you need to choose the larger or smaller of two numbers, which can be done with the methods Math max() or Math.min (). For the wrapping behavior of the settings button, you can use the mod (z) operator. To keep the camera or external charger from charging when the battery is removed, just set the charge capacity of the camera or charger to 0 . Specification The specification for this assignment includes this pdf along with any "official" clarifications announced on Canvas. There are some constants: \[ \text { public static final int NUM_CHARGER_SETTINGS }=4 \text {; } \] Number of external charger settings. Settings are numbered between 0 inclusive and 4 exclusive. \[ \text { public static final double CHARGE_RATE }=2.0 \text {; } \] A constant used in calculating the charge rate of the external charger. \[ \text { public static final double DEFAULT_CAMERA_POWER_CONSUMPTION }=1.0 \text {; } \] Default power consumption of the camera at the start of simulation. There is one public constructor: publicCameraBattery(doublebatteryStartingCharge,doublebatteryCapacity) 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 camera's 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 camera's initial charge should be 0 . In a separate class (and file), 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 camera's 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 battery's (and camera's) charge must be decreased by the given number of minutes times the camera's 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"); 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 battery's 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"); 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. 11. Now finish the last remaining methods. The method externalcharge is similar to cameracharge. There a several methods that are straight forward accessors (getters) which only return the value of an instance variable. 11. At some point, download the SpecChecker, import it into your project as you did in lab 1 and run it. Always start reading error messages from the top. If you have a missing or extra public method, if the method names or declarations are incorrect, or if something is really wrong like the class having the incorrect name or package, any such errors will appear first in the output and will usually say "Class does not conform to specification." Always fix these first. The SpecChecker You can find the SpecChecker on Canvas. Import and run the SpecChecker just as you practiced in Lab 1. It will run a number of functional tests and then bring up a dialog offering to create a zip file to submit. Remember that error messages will appear in the console output. There are many test cases so there may be an overwhelming number of error messages. Always start reading the errors at the top and make incremental corrections in the code to fix them. When you are happy with your results, click "Yes" at the dialog to create the zip file. See the document "SpecChecker HOWTO", link \#10 on our Canvas front page, if you are not sure what to do. Roughly 15% of the points will be for documentation and code style. Here are some general requirements and guidelines: - Each class, method, constructor and instance variable, whether public or private, must have a meaningful javadoc comment. The javadoc for the class itself can be very brief, but must include the eauthor tag. The javadoc for methods must include eparam and ereturn tags as appropriate. - Try to briefly state what each method does in your own words. However, there is no rule against copying the descriptions from the online documentation. However: do not literally copy and paste from this pdf! This leads to all kinds of weird bugs due to the potential for sophisticated document formats like Word and pdf to contain invisible characters. - Run the javadoc tool and see what your documentation looks like! (You do not have to turn in the generated html, but at least it provides some satisfaction :) - All variable names must be meaningful (i.e., named for the value they store). Your code should not be producing console output. You may add println statements when debugging, but you need to remove them before submitting the code. Internal (//-style) comments are normally used inside of method bodies to explain how something works, while the Javadoc comments explain what a method does. (A good rule of thumb is: if you had to think for a few minutes to figure out how something works, you should probably include an internal comment explaining how it works.) - Internal comments always precede the code they describe and are indented to the same level. In a simple homework like this one, as long as your code is straightforward and you use meaningful variable names, your code will probably not need any internal comments. - Use a consistent style for indentation and formatting. - Note that you can set up Eclipse with the formatting style you prefer and then use Ctrl-Shift-F to format your code. To play with the formatting preferences, go to Window- > Preferences- > Java> Code Style->Formatter and click the New button to create your own "profile" for formatting. What to turn in Note: You will need to complete the "Academic Dishonesty policy questionnaire," found on the Assignments page on Canvas, before the submission link will be visible to you. Please submit, on Canvas, the zip file that is created by the SpecChecker. The file will be named sUBMIT_THIS_hw1 . zip. and it will be located in whatever directory you selected when you ran the SpecChecker. It should contain one directory, hw1, which in turn contains one file, CameraBattery . java. Please LOOK at the file you upload and make sure it is the right one! Submit the zip file to Canvas using the Assignment 1 submission link and VERIFY that your submission was successful. If you are not sure how to do this, see the document "Assignment Submission HOWTO", linked on the Course Information page on Canvas. We recommend that you submit the zip file as created by the specchecker. If necessary for some reason, you can create a zip file yourself. The zip file must contain the directory hw1, which in turn should contain the files CameraBattery.java. You can accomplish this by zipping up the sre directory of your project. Do not zip up the entire project. The file must be a zip file, so be sure you are using the Windows or Mac zip utility, and NOT a third-party installation of WinRAR, 7-zip, or Winzip

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

Students also viewed these Databases questions