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: 3856146 • 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

Here you have added 3 Clouds with the code

/* sky.add(strat);
sky.add(cumu);
sky.add(cirr);

*/

Which are stored at 0 , 1 , 2 index of the arraylist.

But, With this for loop

/*

for(int i=0;i<100;i++)
   {
   meanHeight=meanHeight+clouds.get(i).getHeight();

}

*/

you are calling it 100 times. Since there are only three clouds added and there is no value at index 3 of Cloud array list. It is showing such error of array out of bound.

Array out of bound means you are trying to access an element of the array at an index which actually does not exsits or which is not present in the array.

If you will run the for loop 3 times That is

for(int i=0;i<=2;i++)

it will not show any error.