Reputation: 53
This program needs to iterate through an array list to add all the integers together, but I get an "int cannot be dereferenced" error. I think that ints can't be used in array lists but thats how it is in the guide program and I am not sure what to do. The first method needs to calculate the total amount of people then multiply it by average emissions which is 1018. Please help me.
This code was give to me already and when they give you code you don't change what they give you, you just add in what is missing.
Method Class
// This class instantiates CO2FromWaste(8.11) objects with 8 private instance variables.
// It contains three mutator methods to calculate the pounds of CO2: in total emissions,
// emission reductions, and net emissions. It contains getter methods for each private
// instance variables. Private instance variables include myNumPeople, myPaper, myPlastic,
// myGlass, myCans, myEmissions, myReduction, and myNetEmissions.
//
public class CO2FromWaste
{
private int myNumPeople;
private boolean myPaper, myPlastic, myGlass, myCans;
private double myEmissions, myReduction, myNetEmissions;
//
// Constructor for objects of type CO2FromWaste
// @param numPeople number of people in a household
// @param paper whether or not paper is recycled
// @param plastic whether or not plastic is recycled
// @param glass whether or not glass is recycled
// @param cans whether or not cans are recycled
CO2FromWaste(int numPeople, boolean paper, boolean plastic, boolean glass, boolean cans)
{
myNumPeople = numPeople;
myPaper = paper;
myPlastic = plastic;
myGlass = glass;
myCans = cans;
myEmissions = 0.0;
myReduction = 0.0;
myNetEmissions = 0.0;
}
//
// Mutator method to calculate the total emissions, without any recycling (no parameters)
This is where the first issue comes in.
public void calcGrossWasteEmission()
{
double sum = 0;
for(int i = 0; i< myNumPeople.size(); i++)
{
sum += myNumpeople.get(i);
}
myEmissions = sum 1018;
}
//
// Mutator method to calculate the emission reduction from recycling (no parameters)
public void calcWasteReduction()
{
if(myPaper)
{
myReduction += (184.0 myNumPeople);
}
// fill in rest of method here //
}
//
// Mutator method to calculate the net emissions (no paramters)
public void calcNetWasteReduction()
{
// fill in rest of method here //
}
//
// Getter method to return the number of people (no paramters)
public int getNumPeople()
{
return myNumPeople;
}
//
// Getter method to return paper's recycled status (true or false) (no paramters)
public boolean getPaper()
{
return myPaper;
}
//
// Getter method to return glass's recycled status (true or false) (no paramters)
public boolean getGlass()
{
return myGlass;
}
//
// Getter method to return plastic's recycled status (true or false) (no paramters)
public boolean getPlastic()
{
return myPlastic;
}
//
// Getter method to return cans' recycled status (true or false) (no paramters)
public boolean getCans()
{
return myCans;
}
//
// Getter method to return the total emissions (no paramters)
public double getEmissions()
{
return myEmissions;
}
//
// Getter method to return the reduction amount (no paramters)
public double getReduction()
{
return myReduction;
}
//
// Getter method to return the net emissions (no paramters)
public double getNetEmissions()
{
return myNetEmissions;
}
}
Tester Class
/**
* @purpose: Calculate the CO2 from household waste 8.11
*
* @author:
* @version:
*/
import java.util.ArrayList;
public class CO2FromWasteTester
{
public static void main(String[] args)
{
ArrayList<CO2FromWaste> cO2 = new ArrayList<CO2FromWaste>();
// adding households
cO2.add(new CO2FromWaste(1,true,true,true,true));
cO2.add(new CO2FromWaste(3,true,false,true,true));
cO2.add(new CO2FromWaste(4,false,false,false,false));
cO2.add(new CO2FromWaste(1,true,true,true,true));
cO2.add(new CO2FromWaste(1,true,true,true,true));
for(CO2FromWaste dataRecord : cO2)
{
dataRecord.calcGrossWasteEmission();
dataRecord.calcWasteReduction();
dataRecord.calcNetWasteReduction();
}
System.out.println("| | | | Pounds of CO2 |");
System.out.println("| | | Household Waste Recycled | Total | | Net |");
System.out.println("| Index | People | Paper | Plastic | Glass | Cans | Emission | Reduction | Emission |");
System.out.println("|-------|--------|----------|----------|---------|---------|------------|-------------|------------|");
CO2FromWaste dataRecord;
for(int index = 0; index < cO2.size(); index ++)
{
dataRecord = cO2.get(index);
System.out.printf("| %1d | %2d | %-5b | %-5b | %-5b | %-5b | %8.2f | %7.2f | %8.2f |%n", index, dataRecord.getNumPeople(), dataRecord.getPaper(), dataRecord.getPlastic(), dataRecord.getGlass(), dataRecord.getCans(), dataRecord.getEmissions(), dataRecord.getReduction(), dataRecord.getNetEmissions());
}
}
}
Upvotes: 0
Views: 102
Reputation: 734
int
and boolean
are primitive data types; they're not objects, so they can't go in a List
. Instead, use Integer
and Boolean
.
EDIT: Eran is right too.
Upvotes: 1
Reputation: 393936
myNumPeople
is an int
, but you are trying to use it as if it's a List
(myNumpeople.get(i)
, myNumpeople.size()
, etc...).
You should change the type of myNumPeople
.
Upvotes: 1