-3

I have a question on how to calculate with java. In my case, I think I am doing the calculations right, but when I run it, I get a few 0s and some absurd number. How can I fix this? Any help will be greatly appreciated. Below is my code:

public class CO2Footprint {

    private int NumberOfPeople, CO2PerPerson, lightBulbs, months;
    private double TotalEmission, ReductionPerPaper, ReductionPerPlastic, 
            ReductionPerGlass, ReductionPerCan, TotalReduction, 
            FamilyNetEmission, annualGasoline, electricityBill
            , lightBulbsRecycled, annualFuelUse, averageMonthlyCostElec, 
            emissionFactor, monthlyCostSeptElec, monthlyCostOct, monthlyCostNov,
            averagePricePerKilowatt, totalWaste, elecPrice, NetEmission;
    private boolean paper, plastic, glass, can;

    CO2Footprint(int numPeople, boolean bpaper, boolean bplastic, boolean 
            bglass, boolean bcans, double annuallGasoline
    , double electricityyPrice, int lighttBulbs){
        NumberOfPeople = 0;
        paper = bpaper;
        plastic = bplastic;
        glass = bglass;
        can = bcans;
        ReductionPerPaper = 184;
        CO2PerPerson = 1018;
        ReductionPerPlastic = 25.6;
        ReductionPerGlass = 46.6;
        ReductionPerCan = 165.8;
        TotalEmission = 0;
        annualGasoline = 16.5;
        electricityBill = 0;
        lightBulbs = 10;
        lightBulbsRecycled = 0;
        annualFuelUse = 0;
        averageMonthlyCostElec = 0;
        emissionFactor = 1.37;
        monthlyCostSeptElec = 551.51;
        monthlyCostOct = 392.84;
        monthlyCostNov = 445.42;
        months = 12;
        averagePricePerKilowatt = 4.83;
        totalWaste = 0;
        NetEmission = 0.0;
    }

    // method for calculating total emission
    public double totalEmission(){
        TotalEmission = NumberOfPeople * CO2PerPerson;
        return TotalEmission;
    }

    // method for calculating CO2 reduction
    public double reduction(){
        TotalReduction = 0;
        ReductionPerPaper = 0;
        ReductionPerPlastic = 0;
        ReductionPerGlass = 0;
        ReductionPerCan = 0;
        if(paper = true){
            ReductionPerPaper = 184;
        }else{
            ReductionPerPaper = 0;
        }
        if(plastic = true){
            ReductionPerPlastic = 25.6;
        }else{
            ReductionPerPlastic = 0;
        }
        if(glass = true){
            ReductionPerGlass = 46.6;
        }else{
            ReductionPerGlass = 0;
        }
        if(can = true){
            ReductionPerCan = 165.8;
        }else{
            ReductionPerCan = 0;
        }
        TotalReduction = ReductionPerPaper + ReductionPerPlastic + 
                ReductionPerGlass + ReductionPerCan + (lightBulbs * 1.37 * 73);
        return TotalReduction;
    }

    public double calcAnnualFuelUseGallonCO2(){
        annualFuelUse = annualGasoline * 12 * 12;
        return annualFuelUse;
    }

    public double calcElectricityBill(){
        return (averageMonthlyCostElec / averagePricePerKilowatt) * emissionFactor * months;
    }

    public double electrictyPrice(){
        return averagePricePerKilowatt;
    }

    public double calcWaste(){
        TotalReduction = 0;
        ReductionPerPaper = 0;
        ReductionPerPlastic = 0;
        ReductionPerGlass = 0;
        ReductionPerCan = 0;
        if(paper = true){
            ReductionPerPaper = 184;
        }else{
            ReductionPerPaper = 0;
        }
        if(plastic = true){
            ReductionPerPlastic = 25.6;
        }else{
            ReductionPerPlastic = 0;
        }
        if(glass = true){
            ReductionPerGlass = 46.6;
        }else{
            ReductionPerGlass = 0;
        }
        if(can = true){
            ReductionPerCan = 165.8;
        }else{
            ReductionPerCan = 0;
        }
        totalWaste = ReductionPerPaper + ReductionPerPlastic + 
                ReductionPerGlass + ReductionPerCan;
        return totalWaste;
    }

    public double calcBulbsEmissionReduction(){
        return lightBulbs * 1.37 * 73;
    }

    public double calcNetEmission(){
        NetEmission = TotalEmission - CO2PerPerson;
        return NetEmission;
    }


    }

If it helps, below is the code for how I called the methods:


    import java.util.ArrayList;

    public class CO2FootprintTester {

    public static void main(String args[]){

        ArrayList<CO2Footprint> CO2 = new ArrayList<CO2Footprint>();

        CO2.add(new CO2Footprint(1, true, true, true, true, 16.5, 4.83, 10));

        CO2Footprint data;

        for(int i = 0; i < CO2.size(); i++){
            data = CO2.get(i);
            data.calcAnnualFuelUseGallonCO2();
            data.calcBulbsEmissionReduction();
            data.calcElectricityBill();
            data.calcWaste();
            data.electrictyPrice();
            data.reduction();
            data.totalEmission();
        }

        // create table
        System.out.println("___________________________________________________"
                + "_____________________________________________________________________________");
        System.out.printf("%65s%44s%n", "        Pounds of CO2 Emitted From:        |",
                "            Pounds of CO2 Reduced From:            ");
        System.out.println("___________________________________________________"
                + "_____________________________________________________________________________");
        System.out.printf("%1s%25s%25s%25s%25s%n%n", "Gas", "Electricity", 
                "Waste", "Recycling", "New Bulbs");

        // call methods
        for(int i = 0; i < CO2.size(); i++){
            data = CO2.get(i);
            System.out.printf("%5.5f%15.5f%15.5f%15.5f%15.5f", data.calcAnnualFuelUseGallonCO2(), data.calcElectricityBill(), 
                    data.totalEmission(), data.calcNetEmission(), data.calcBulbsEmissionReduction());//, 
                    //data.calcElectricityBill(), data.totalEmission(), data.calcNetEmission(), 
                    //data.calcBulbsEmissionReduction());
        }
    }

    }
Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • 1
    first of all, please start your variable names with lowercase letter. – Baby Jan 02 '14 at 03:05
  • How to calculate what??? How in the world are we supposed to know what you are trying to do, and what the correct values are? – OldProgrammer Jan 02 '14 at 03:07
  • 1
    Also as a java programmer you should learn how to debug your code yourself, you should learn how to use IDE's debug facilities. That will take you longer in career life. – Pradeep Simha Jan 02 '14 at 03:09

1 Answers1

0

Seems like you are facing the problem of using double data type precision. double data type is sometimes troublesome in precision calculations like monetary values etc.

It is recommended to use BigDecimal to ensure precision while arithmetic operations.

More information can be found at this post

shazin
  • 21,379
  • 3
  • 54
  • 71