I have an assignment to calculate the amount of carbon dioxide produced in a year from a household and compare how recycling can reduce its CO2 Footprint. There are two classes in the program, CO2FromWaste, and CO2FromWasteTester.
The first class, CO2FromWaste is:
public class CO2FromWaste
{
//declaration of private instance variables
private int numPeople;
private boolean Paper, Plastic, Glass, Cans;
private double grossWasteEmission, wasteReduction, netWasteReduction;
//constructor
CO2FromWaste(int people, boolean paper, boolean plastic, boolean glass, boolean cans){
numPeople = people;
Paper = paper;
Plastic = plastic;
Glass = glass;
Cans = cans;
grossWasteEmission = 0.0;
wasteReduction = 0.0;
netWasteReduction = 0.0;
}
public void calcGrossWasteEmission(){
grossWasteEmission = numPeople * 1018;
}
public double getGrossWasteEmission(){
return grossWasteEmission;
}
public void calcWasteReduction(){
if (Paper == true){
wasteReduction = numPeople * 184;
}
if (Plastic == true){
wasteReduction += (numPeople * 25.6);
}
if (Glass == true){
wasteReduction+= (numPeople*46.6);
}
if (Cans == true){
wasteReduction+=(numPeople*165.8);
}
}
public double getWasteReduction()
{
return wasteReduction;
}
public void calcNetWasteReduction(){
netWasteReduction = grossWasteEmission = wasteReduction;
}
public double getNetWasteReduction(){
return netWasteReduction;
}
public int getNumPeople(){
return numPeople;
}
public boolean getPaper(){
return Paper;
}
public boolean getPlastic(){
return Plastic;
}
public boolean getGlass(){
return Glass;
}
public boolean getCans(){
return Cans;
}
}
The second class, CO2FromWasteTester is:
import java.util.ArrayList;
public class CO2FromWasteTester
{
public static void main(String[]args){
ArrayList<CO2FromWaste> waste = new ArrayList<CO2FromWaste>();
waste.add(new CO2FromWaste(1, true, true, true, true));
waste.add(new CO2FromWaste(3, true, false, true, true));
waste.add(new CO2FromWaste(4, false, false, false, false));
waste.add(new CO2FromWaste(1, true, true, true, true));
waste.add(new CO2FromWaste(1, true, true, true, true));
CO2FromWaste wasteRecord;
for (int index = 0; index < waste.size(); index++){
wasteRecord = waste.get(index);
wasteRecord.calcGrossWasteEmission();
wasteRecord.calcWasteReduction();
wasteRecord.calcNetWasteReduction();
}
System.out.println(" Household Waste Recycled Total Pounds of CO2 Net");
System.out.println(" Index People Paper Plastic Glass Cans Emission Reduction Emission ");
for (int index = 0; index < waste.size(); index ++)
{
wasteRecord = waste.get(index);
System.out.printf("%3d %9d %10s %10s %10s %10s %12.2f %10.2f %10.2f%n",index,wasteRecord.getNumPeople(),wasteRecord.getPaper(), wasteRecord.getPlastic(), wasteRecord.getGlass(),wasteRecord.getCans(),wasteRecord.getGrossWasteEmission(),wasteRecord.getWasteReduction(),wasteRecord.getNetWasteReduction());
}
}
}
The output should read like a table, with the correct data under the headers. For the first line, the output should be
0 1 true true true true 1018.00 422.00 596.00
but it reads
0 1 true true true true 422.00 422.00 422.00
There is something wrong starting the the gross emission part, and that part should be fairly simple because all it needs to do is to multiply the number of people given by 1018. I am not sure what to do from here and would appreciate some help.
This is the problem:
public void calcNetWasteReduction(){
netWasteReduction = grossWasteEmission = wasteReduction;
}
That's equivalent to:
public void calcNetWasteReduction(){
grossWasteEmission = wasteReduction;
netWasteReduction = grossWasteEmission;
}
In other words, it's modifying grossWasteEmission
when it shouldn't be. I suspect you wanted:
public void calcNetWasteReduction(){
netWasteReduction = grossWasteEmission - wasteReduction;
}
In other words, making the second =
a -
instead.
It's not clear why you have the three separate methods at all, to be honest - why not perform the calculations in the constructor? Or in the getter
methods (removing the intermediate fields altogether)?
Additionally, consider making an enum of waste reduction types (PAPER, GLASS, PLASTIC) and taking an EnumSet
of them. If you do keep them as individual parameters, I would strongly advise you to change the names to be more conventional Java (paper
instead of Paper
etc) and use if (paper)
rather than if (paper == true)
etc. In general, avoid explicit checking against Boolean constants.
See more on this question at Stackoverflow