Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey guys! can you please help me fix the index out of bound issue here? Error Ex

ID: 3856145 • Letter: H

Question

Hey guys! can you please help me fix the index out of bound issue here?

Error
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

   at java.util.ArrayList.rangeCheck(ArrayList.java:653)

   at java.util.ArrayList.get(ArrayList.java:429)

   at weather.Sky.getMeanHeight(Sky.java:40)

   at weather.Sky.main(Sky.java:61)

Code

package weather;
import java.util.ArrayList;

/*
This class holds lots of cloud instances of various types, and computes the average cloud height. It needs:
1) A private ArrayList<Cloud> called clouds.
2) A ctor that creates the clouds array list with an initial capacity of 100. Look up
the javadoc API page for ArrayList to see what ArrayList ctor to call. If you don’t
know how to do this, ask on Piazza. DON’T ask Piazza for the answer, just ask
how to find the javadoc API page.
3) A public method called add(Cloud c) that takes a cloud and adds it to the array
list. The return type of this method should be boolean, and the method should
always return true. That’s a little weird but it will make more sense later.
4) A public float method called getMeanHeight(), which returns the average height
of all the clouds in the array list.
*/

public class Sky {

// a private ArrayList of type Cloud named clouds ****
   private ArrayList<Cloud> clouds;

   // a constructor that creates the clouds array list with
// initial capacity of 100 ****

public Sky() {
   clouds = new ArrayList<Cloud>(100);
   // clouds.ensureCapacity(100);
   }

   public boolean add(Cloud c){
clouds.add(c);
return true;
}

public float getMeanHeight(){
   float meanHeight=0;
   for(int i=0;i<100;i++)
   {
   meanHeight=meanHeight+clouds.get(i).getHeight(); // ERROR Line 40***********************
   }

       meanHeight=meanHeight/100;
return meanHeight;
}

public static void main(String[] args) {

StratusCloud strat = new StratusCloud(100, 1000);
if (!strat.rain().startsWith("It is raining"))
System.out.println("Bad StratusCloud::rain");
CumulusCloud cumu = new CumulusCloud(200, 2000);
if (!cumu.rain().startsWith("It is raining"))
System.out.println("Bad CumulusCloud::rain");
CirrusCloud cirr = new CirrusCloud(300, 3000);
if (!cirr.rain().startsWith("I cannot make");
   System.out.println("Bad CirrusCloud::rain");
Sky sky = new Sky();
sky.add(strat);
sky.add(cumu);
sky.add(cirr);
float mean = sky.getMeanHeight(); // ERROR line 61*********************
if (mean < 1799 || mean > 1801)

               System.out.println("Bad mean height: expected 1800, saw " + mean);

       System.out.println("Everything (else) is ok");

}
}

Explanation / Answer


Answer :

Issue :
First step of getting this IndexOutOfBoundsException is because of "for loop with elements condition 100".
But after seen your code, I found that you were add only three objects to that ArrayList. which is causing the issue.
For loop will run up to 100 elemets, meaning index - > 99. So it will start at 0,1,2 and after the we don't have any
elements in that list and it is sowing IndexOutOfBoundsException at index 3.
As per the logic of ArrayList if you menetione the capacity at top level, meaning is only capacity, It doesn't mean like we have
100 elements in that list.
Usually ArrayList initial capacity was 10,If you trying to 11th element to that list, It will automatically increases the List capacity
to another 10.
Solution:
1. We need to modify the for loop condition to handle only three objects.
for (int i = 0; i < 3; i++) {
meanHeight = meanHeight + clouds.get(i).getHeight();
}
2. If you still want to continue the with the existing for loop means.
you need add 100 elements to taht ArrayList.
Example Code Using Your Code Logic:

======== TestSkyCode.java
import java.util.ArrayList;

public class TestSkyCode {
private ArrayList<Cloud> clouds;
public TestSkyCode() {
clouds = new ArrayList<Cloud>(100);
Cloud cloud = new Cloud(); // Just added sample data to list
cloud.setHeight(new Float(4.1));
clouds.add(cloud);
}
public float getMeanHeight() {
float meanHeight = 0;
for (int i = 0; i < 1; i++) {
meanHeight = meanHeight + clouds.get(i).getHeight();
}
meanHeight = meanHeight / 100;
return meanHeight;
}
public static void main(String ag[]){
TestSkyCode testSkyCode = new TestSkyCode();
System.out.println("Out Put : "+testSkyCode.getMeanHeight());
}
}
=======Cloud.java
public class Cloud {
private float height;
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
}


====== Out put

Out Put : 0.040999997