CS 261 Assignment #0 (Optional)
Try to finish by January 23rd
Introduction:
This first assignment will give you a chance to refresh your Java programming fundamentals, and get more practice writing clean, correct, well-documented code. Your task, to be completed individually, is to write a program that analyzes power-usage patterns of a freezer, based on a file containing power-consumption data points. The specifics are spelled out in the next section.
The Assignment
I've got a freezer that's been behaving mysteriously of late. It works just fine most the time, but every few months it trips the breaker and loses power. If we don't notice it in time, the freezer thaws out and we lose a bunch of food. I considered buying a new freezer, but they're expensive and I'm cheap, so I came up with a high-tech warning system instead: I have a program watching the power usage, and it sends me a text message if it doesn't see the freezer using power for a while.
Before coming up with the warning system, I needed to know if it was even feasible to tell if the freezer was working based on the power data I had available. How often does it run? How long does it run once it comes on? Could I tell the freezer's power consumption from other items on the same circuit? The graph below shows the power usage on the freezer's circuit on a recent afternoon as an example.
The tall spikes show when the freezer's using power. (The Y axis shows power in Watts, and it draws about 100 Watts when it's running.) It turns out there's only one other item on that circuit — some lights that, when turned on, use about 25 Watts. That's good news! Whether the lights are on or not, it's possible to detect the freezer running. (During the second peak the lights got turned on about 10 minutes after the freezer started running, and the lights were on already before the last two freezer runs shown.)
I've saved some data from the monitoring system, at one-minute intervals, and your job is to write a program that will report some basic stats on the freezer's power usage: How many minutes (on average) does it run each time it comes on? How many minutes are there between runs on average? And how much total power does it use over the monitored period?
Start by downloading and unzipping FreezerAsmt.zip. The project has a bit of starter code to help you get started. This project is short enough that it's fine with me if all of the code ends up in the main method, though you should feel free to break it into smaller pieces if you prefer. Note that the project folder also contains a file called data.txt
holding a sample power-usage data file. Your finished program should prompt the user for the name of a data file, analyze the data in the file, then print a report. The output from my solution is shown below:
Please enter the filename to be analyzed
data.txt
Thanks. Analyzing data in data.txt
Analyzed 2432 minutes of power data:
The freezer ran 36 times, for a total of 1.0466966716666664 kWh
The average run lasted 16.666666666666668 minutes
The average interval between runs was 50.888888888888886
Here are some more specific requirements:
- Your program should not assume that the file exists.
- If the file does exist, your program should produce appropriate output even if the file is empty, or if the freezer never runs.
- If the data shows at least one freezer run, you should report the total power usage (in kilowatt hours), the length of the average run (in minutes), and the length of the average interval between freezer runs (in minutes).
- Power use during periods where the freezer isn't running but the lights are on (e.g. 2pm in the graph above) should not be included in the freezer's total power usage. It's OK to include the power from the lights if they're on at the same time as the freezer though (e.g. 1:45pm in the graph above).
- A freezer run should only be counted if you observe both the beginning and the end of the run. For example, if there was an additional "partial" freezer run going on when
data.txt
ended, the proper count would still be 36 runs. It's ok if you include power usage from incomplete runs in the total.
The requirements ask that you report total power consumption in kiloWatt-hours. That's the standard unit for power consumption (the amount of power a 1000 Watt load would use in an hour), but will require a bit of math in our case. If the freezer ran for 15 minutes, and used 100 Watts during each of those minutes, that would be a total of 1500 Watt-minutes. You'll need to adjust that to convert from minutes to hours, then divide by 1000 to turn Watt-hours into kiloWatt-hours. I'll trust you can get the units to come out right, but feel free to ask for help if you're not sure what to do there.
Style Guide
Before you submit your assignment, go through the checklist below and make sure your code conforms to the style requirements.
- No unused variables or commented-out code is left in the class
- All instance variables (fields) are used in more than one method (if not, make them local variables)
- Javadoc comment at the top of each class (
/** ... */
)
- All methods have Javadoc comments
- Specific values have been replaced with constants (i.e. no "magic numbers")
- Proper capitalization of variable, method, and class names
- Use of white space to separate different sections of your code
- Consistent and appropriate indentation
Feel free to define additional classes to help organize the project if you see fit. At the very least, you should define one or more methods to help with the computations, and call those methods from the body of the main method.
Grading
If it were being graded, this assignment would be graded out of a total of 60 points:
- [10] Uses specified input and outputs
- [10] Uses File and Scanner objects properly
- [20] Calculates results properly
- [10] Handles error cases properly
- [10] Style and commenting
Extras
Looking for an extra challenge? Consider one or more of these optional extras. There's no extra credit, but they'd be good practice:
- Calculate stats on the power used by the lights, as well as the freezer.
- Get more precise about the power used by the freezer: If the lights are on as well, subtract off their contribution.
- Ignore power usage from any partial freezer runs at the beginning or end of the file.
- Look into ways to tidy up the formatting of the numeric results so that they have a smaller and consistent number of digits after the decimal point.
Submitting
If you want feedback, email me your FreezerAnalysis.java
file and I'll take a look at it.
Brad Richards, 2023