Results 1 to 3 of 3
  1. #1
    SwordMaster is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Get Value of another Method

    Hey guys

    I'm new to Java and I'd like to ask you the following:

    The exercise is:
    Rewrite both the listFile and removeFile methods in MusicOrganizer so that they use your validIndex method to check their parameter, instead of the current boolean expression. They should only call get or remove on the ArrayList if validIndex returns true.

    Code:

    public void checkIndex (int Number)
    {
    if (Number >= 0 && Number < files.size())
    {}
    else {
    System.out.println("Error");
    }
    }
    public boolean validIndex (int Numba)
    {
    if (Numba >= 0 && Numba < files.size())
    { return true; }
    else {return false;}
    }

    /**
    * Return the number of files in the collection.
    * @return The number of files in the collection.
    */
    public int getNumberOfFiles()
    {
    return files.size();
    }

    /**
    * List a file from the collection.
    * @param index The index of the file to be listed.
    */
    public void listFile(int index)
    {
    if(index >= 0 && index < files.size()) {
    String filename = files.get(index);
    System.out.println(filename);
    }
    }

    /**
    * Remove a file from the collection.
    * @param index The index of the file to be removed.
    */
    public void removeFile(int index)
    {
    if(index >= 0 && index < files.size()) {
    files.remove(index);
    }
    }
    }


    How do I get the true/false value from the validIndex Method into the other two?

    Thanks for you help!

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Get Value of another Method

    You call a method by using its name and any arguments needed. For instance:

    Java Code:
    public int isEven(int i)
    {
      if (i%2==0)
        return true;
      return false;
    }
    public void someMethod()
    {
      boolean even=isEven(2);
    }
    In the above example, the even boolean would be set to true.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Get Value of another Method

    Please go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 03-23-2012, 04:53 AM
  2. Replies: 1
    Last Post: 10-17-2011, 01:00 AM
  3. Declaring a method inside a method: Runnable?
    By africanhacker in forum New To Java
    Replies: 8
    Last Post: 03-23-2011, 04:30 AM
  4. Turning Recursion Method into Iterative method
    By mattakuevan in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 06:46 AM
  5. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •