title: using instanceof to count in java
body: <pre><code>import java.util.ArrayList;
  
  public class Folder extends AbstractFile
  {
  //Replace previous ArrayLists with a single
  //ArrayList of AbstractFile references
  private ArrayList&lt;AbstractFile&gt; files = new ArrayList();
         AbstractFile abstractfile;
  /**
   * Constructor for objects of class Folder
   */
  public Folder(String name)
  {
      super();
      this.name = name;
  }
  // replace previous add methods
  // with a single add(AbstractFile fileObject) method
  public boolean add(AbstractFile fileObject)
  {
      return files.add(fileObject);
  }
  @Override
  public int size()
  {
      int size =0; // size holds the running total
      for (AbstractFile file : files){ // for each AbsFile ref
         size+=file.size(); //call size() and update the running total
      }
      return size; // return the final value
  }
  
  
  @Override
  public int getNumFiles(){
      int numFiles = 0;
      for(AbstractFile file: files)
      {
          numFiles += file.getNumFiles();
  
  
      }
  
      return numFiles;// default value
  }
  @Override
  public int getNumFolders(){
      int numFolders = 0;
  
      // for(AbstractFile file: files)
      // {
          // if(file instanceof Folder)
          // {
          // numFolders += file.getNumFolders();
      // }
  
      // }
     // return numFolders;// default value
      for (Object e : files)
      {
          if (e instanceof Folder)
          {
               numFolders += e.getNumFolders();
          }
      }
        return numFolders;// default value
  }
  @Override
  public AbstractFile find(String name){
      //TODO Later - not in mini assignment
      return null;
  }
  }
  </code></pre>
  
  <p>AbstractFile Class</p>
  
  <pre><code>public abstract class AbstractFile
  {
  // instance variables -
  String name;
  
  public abstract int size();
  public abstract int getNumFiles();
  public abstract int getNumFolders();
  public abstract AbstractFile find(String name);
  
  public String getName(){
      return name;
  }
  
  }
  </code></pre>
  
  <p>FileSystem Class- for testing    </p>
  
  <pre><code>public class FileSystem
  {
      public static void main(String[] args)
      {
          FileSystem fileSystem = new FileSystem();
          fileSystem.fileTest1();
  
      }
  
      public void fileTest1(){
  
          Folder documents = new Folder("Documents");
          Folder music = new Folder("Music");
          Folder photos = new Folder("Photos");
          documents.add(music);
          documents.add(photos);
          File assign1  = new File("assign1.doc");
          documents.add(assign1);
          Folder dylan = new Folder("Dylan");
          music.add(dylan);
          Folder band = new Folder("The Band");
          music.add(band);
          File family = new File("family.jpg");
          photos.add(family);
          File tambourine = new File("tambourine.mp3");
          dylan.add(tambourine);
          File dixie = new File("dixie.mp3");
          band.add(dixie);
          File weight = new File("weight.mp3");
          band.add(weight);
  
          String contents1 = ("Hey, mister, can you tell me "); 
          String contents2 = ("Hey Mr Tambourine Man");
          String contents3 = ("The night they drove old dixie down");
          String contents4 = ("fee fi fo fum");
  
          weight.setContents(contents1); // add contents to each File
          tambourine.setContents(contents2);
          dixie.setContents(contents3);
          assign1.setContents(contents4);
  
          //********test for size()****************
          int expected  = contents1.length() + contents2.length() + contents3.length() + contents4.length();
          int result = documents.size();
  
          if(result==expected){ // test fro equality
              System.out.println("size() works");
  
          }else{
              System.out.println("size() doesn't work");
          }
  
          //*****************************************
  
          //*****************test for getNumFiles()******************
  
          expected =5;// what value should expected be set to?
          result = documents.getNumFiles();
  
          if(result==expected){ // test fro equality
              System.out.println("NumFiles() works");
  
          }else{
              System.out.println("NumFiles() doesn't work");
  
          }
  
          //output the results of the test for equality
  
          // **************************************
  
          //*****************test for getNumFiles()******************
  
          expected = 5; // what value should expected be set to?
          result = documents.getNumFolders();  
  
  
  
          if(result==expected){ // test fro equality
              System.out.println("NumFolder() works");
  
          }else{
              System.out.println("NumFolder() doesn't work");
              System.out.printf("%d",result);
          }
  
          // **************************************
  
  
       }
    }
  </code></pre>
  
  <p>File Class</p>
  
  <pre><code>  public class File extends AbstractFile
     {
      private String contents;
  /**
   * Constructor for objects of class File
   */
  public File(String name)
  {
      super();
      this.name = name;
  }
  
  public String getContents(){
      return contents;
  }
  
  public void setContents(String contents){
     this.contents = contents;
  }
  
  @Override
  public int size()
  {
      if(contents==null){ //handle situation where contents may not have been set
         return 0; 
      }
      return contents.length();
  }
  @Override
  public int getNumFiles(){
      return 1; // a File object just returns 1 (it contains one File - itself)
  }
  @Override
  public int getNumFolders(){
      //TODO
      return 0;
  }
  @Override
  public AbstractFile find(String name){
      //TODO Later - not in mini assignment
      return null;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Hi, So Basically I'm having trouble with the <code>getNumFolders(method)</code> I have a test class created with 5 folders and these are stored in an <code>ArrayList</code> of type <code>AbstractFile</code> called files. I am trying to loop through this <code>ArrayList</code> to count the folders, bare in mind this <code>ArrayList</code> also contains files, so they must not be counted. 
  I would appreciate any help.
  Thanks so much!</p>