Chaps, I have just read about java interfaces and there is a nice exercise in my book that I would like to attempt, so I was wondering if you could help me to identify the best way to go about it. Here's the exercise:
"Create 3 small classes unrelated by inheritance - classes Building
, Car
and Bicycle
. Give each class some unique appropriate attributes and behaviours that it doesn't have in common with other classes. Write an interface CarbonFootprint
with a getCarbonFootprint
method. Have each of your classes implement that interface so that its getCarbonFootprint
method calculates an appropriate carbon foorptint for that class (check out a few sites that explains how to calculate carbon footprint).
Write an application that creates objects oh each of the 3 classes, places references to those objects in ArrayLists<CarbonFootprint>
, then iterates through the ArrayLists
, polymorphycally invoking each object's getCarbonFootprint
method. For each object print some identifying information and the object's carbon footprint".
Now, first of all, let me say that I don't really care about the accuracy of the carbon footprint calculation, because I am not going to build an application that somebody will actually use (there are plenty of online calculators for that), this is just an exercise aimed at helping me to understand interfaces a bit more and the usage of ArrayLists<T>
which I have never used.
I had a look at how the footprint is calculated and these are the formulas I was thinking to use:
For a car:
carbon footprint = ((averageYearlyMiles * AverageMPG) * 9kg/gallon) because allegedly one gallon of gas emits 9kg of co2. (source: http://www.motherearthnews.com/nature-and-environment/carbon-footprint-calculator.aspx#axzz2WOzrsqNS)
For a house:
carbon footprint = monthly electricity kWh * 12 source http://www.howany.com/how-to-calculate-carbon-footprint/
For a bike
carbon footprint = miles * 34 (takes 34 calories to bike a mile apparently) source: http://twtitw.firebus.com/node/100
Obviously if you have any suggestion as to how to calculate that, let me know.
The questions I have are:
1) is it worth doing some pseudocode? I mean in this program I will only create some objects and then calculate the carbon footprint.
2)what sort of relevant attribute and behaviours do you think each of the classes should have? I was thinking that there won't be many. For example:
Car:
-relevant behaviours? Can't think of any
attributes:
-miles in a year
-MPG
-kg of co2 (constant amounting to 9)
Bike:
-relevant behaviours? Can't think of any
attributes:
-miles in a year
-calories per mile (constant amounting to 34)
house
-relevant behaviours? Can't think of any
attributes:
-monthly Kwh
-months (constant amounting to 12)
Is there anything else I need?
thanks