The array is part of a class called CO2Data so the array is a CO2Data array. The problem is I'm trying to find the highest value in the array but my method isn't working. To get the values from the array the method must first get them from the CO2data class. So what's wrong with my method:
public static CO2Data highest (CO2Data [] arr2){
Scanner sc = null;
CO2Data highestindex = arr2[0].getTotalCO2;
for (int i = 0; i<arr2.length; i++){
arr2[i].getTotalCO2(sc.nextDouble());
if(arr2[i].getTotalCO2() > highestindex ) {
highestindex = arr2[i].getTotalCO2();
}
}
System.out.println(highestindex);
return highestindex;
}
Here's what the CO2data class looks like:
public class CO2Data {
private String country; //A private variable will prevent other users from accessng and chaning these variables.
private double totalCO2;
private double roadCO2;
private double CO2PerPerson;
private int carsPerPerson;
public CO2Data() {
country = "";//this sets the initial values for the different variables
totalCO2 = 0;
roadCO2 = 0;
CO2PerPerson = 0;
carsPerPerson = 0;
}
public String getCountry() {
return country;
}
public void setCountry(String country) { //to set the country you have to use the this command to access the private variable
this.country = country; //you have to use this.country instead of just country because the string county has been declared as a private variable.
}
public double getTotalCO2() {
return totalCO2;
}
public void setTotalCO2(double totalCO2) {
this.totalCO2 = totalCO2; //The this.item command allows you to access private variables.
}
public double getRoadCO2() {
return roadCO2;
}
public void setRoadCO2(double roadCO2) {
this.roadCO2 = roadCO2;
}
public double getCO2PerPerson() {
return CO2PerPerson;
}
public void setCO2PerPerson(double cO2PerPerson) {
this.CO2PerPerson = cO2PerPerson;
}
public int getCarsPerPerson() {
return carsPerPerson;
}
public void setCarsPerPerson(int carsPerPerson) {
this.carsPerPerson = carsPerPerson;
}
}
Jayden Bell
30-Dec-2014