Hi everybody. I am writing a program that calculates the amount of Carbon Dioxide produced in a year from waste by a few selected households and comparing how recycling can reduce the CO2 footprint. However, I believe the code is correct; I am just not getting the expected output. So, something must be hosed; however, I can't figure it out. For anybody that might know the problem, I have pasted the two classes below. Thanks

/**
 * This class instantiates shapes objects with four private instance variables.
 * It contains two mutator methods to calculate the area of a triangle and the
 * hypoteneuse of a triangle.  There are getter methods for each private instance
 * variable.
 * 
 * @author John D. Barry
 * @version 03/20/09
 */
//import java.util.ArrayList;
class CO2FromWaste
{
    //declaration of private instance variables
    private int NumPeople;
    private boolean Paper, Plastic, Glass, Cans;
    private double grossWasteEmission, wasteReduction, netWasteReduction;
    
    
    //constructor for ojbects of type CO2FromWaste
    CO2FromWaste(int numPeople, boolean paper, boolean plastic, boolean glass, boolean cans)
    {
        NumPeople = numPeople;
        Paper = paper;
        Plastic = plastic;
        Glass = glass;
        Cans = cans;
        grossWasteEmission = 0.0;
        wasteReduction = 0.0;
        netWasteReduction = 0.0;
    }
    
    //mutator method to calculate the gross waste emission
    public void calcGrossWasteEmission()
    {
        grossWasteEmission = NumPeople * 1018;        
    }
    
    //mutator method to calculate the waste reduction
    public void calcWasteReduction()
    {
        if(Paper == true)
        {
            wasteReduction = NumPeople * 184;
        }
        
        else if(Paper == true && Plastic == true)
        {
            wasteReduction = (NumPeople * 184) + (NumPeople * 25.6);
        }
        
        else if(Paper == true && Plastic == true && Glass == true)
        {
            wasteReduction = (NumPeople * 184) + (NumPeople * 25.6) + (NumPeople  * 46.6);
        }
        
        else if(Paper == true && Plastic == true && Glass == true && Cans == true)
        {
            wasteReduction = (NumPeople * 184) + (NumPeople * 25.6) + (NumPeople * 46.6) + (NumPeople  * 165.8);
        }
    
    }
    
    //mutator method to calculate the net waste reduction
    public void calcNetWasteReduction()
    {
        netWasteReduction = grossWasteEmission - wasteReduction;
    }
    
    //getter method to get the value of the gross waste emission
    public double getGrossWasteEmission()
    {
        return grossWasteEmission;
    }
    
    //getter method to get the value of the waste reduction
    public double getWasteReduction()
    {
        return wasteReduction;
    }
    
    //getter method to get the value of the net waste reduction
    public double getNetWasteReduction()
    {
        return netWasteReduction;
    }
    
    public int getNumPeople()
    {
        return NumPeople;
    }

        
    
}
/**
 * This class tests the CO2FromWaste class. 
 * An ArrayList of shapes objects is created to hold the four private instance variables.
 * The add() method is used to add the objects to the ArrayList as they are instantitated.
 * 
 * 
 * A for loop is used to call the calcTriArea() and calcHypoteneuse() methods on each object in the ArrayList.
 * A second for loop is used to print the values of the instance variables for each object.
 * 
 * @author John D. Barry 
 * @version 03/20/09
 */
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));

        CO2FromWaste wasteRecord;                   //creates a new dataRecord object of type ShapesV11
        
        for(int index = 0; index < waste.size(); index++)
        {
            wasteRecord = waste.get(index);
            wasteRecord.calcGrossWasteEmission();
            wasteRecord.calcWasteReduction();
            wasteRecord.calcNetWasteReduction();
        }
                
        
        //call methods
        for(int index = 0; index < waste.size(); index++)
        {
            wasteRecord = waste.get(index);
            
            System.out.printf("%12d %9.2f %15.2f %12.2f%n",wasteRecord.getNumPeople(),wasteRecord.getGrossWasteEmission(),wasteRecord.getWasteReduction(),wasteRecord.getNetWasteReduction());


        }
    }
}

For instance for the first line, I should get the following output:


People Total Emissions Reduction Net Emission
1 1018 422 596

...and I am getting this:

People Total Emissions Reduction Net Emission
1 1018 184 834

I am not going to work through your proggie (I'm lazy)
Insert intermediate result prints after each calc step and step through the routine and watch what happens to your numbers.

1. Inside calcWasteReduction() check all 4 boolean conditions for 16 cases.
2. Better way is check for 4 boolean conditions (for 4 cases). If true, succesive add coefficients to the local varible. At end multiply this with NumPeople.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.