Results 1 to 8 of 8
  1. #1
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Little Help with method in a Class

    Hi everyone,

    I am relatively new to Java,

    My problem is I have two classes 1) Track 2)PlayList

    I have a method that should print all objects in the ArrayList but for the life of me I'm unable to get it to work. (this method is in the PlayList Class

    Error I receive is: non-static method playLength() cannot be referenced from a static context

    Below is my code:
    #Track Class
    Java Code:
    public class Track
    {
        /** Instance variables
        */
        // Reference number
        private String referenceNumber;
        // Track play length
        private int playLength;
        // Track name
        private String trackName;
        // Track artist's name
        private String artistsName;
        // Track genre
        private String genre;
    
        /**
         * Constructor for objects of class Track
         */
        public Track(String newReferenceNumber, String newTrackName, String newArtistsName, String newGenre, int newPlayLength)
        {   
          referenceNumber = newReferenceNumber;
          trackName = newTrackName;
          artistsName = newArtistsName;
          genre = newGenre;
          playLength = newPlayLength;
        }
      
    
    
        /**
         * Displays the track play length and appends seconds
         */
        public void playLength(int playLength)
           {
            String playLengthStr = "";
            playLengthStr = Integer.toString(playLength);
            System.out.println(playLengthStr + " Seconds");
        }
        
        /**
         * Displays the track details defined by the variables inputted by user
         */
        public void trackDetailsComplete()
        {     
           // Prints line for each variable with a value inputed by user
           System.out.println ("Reference Number:  " + referenceNumber + " " + "Track Name:  " +
                                trackName + " " + "Artist's Name:  " + artistsName + " " + "Genre:  " + genre
                                + " " + "Play Length:  " + playLength);
           
           // System.out.println ("~~ Track -Play Length ~~ " + "\n" + "  " + playLength);
           // System.out.println ("~~ Track -Name ~~ " + "\n" + "  " + trackName);
           // System.out.println ("~~ Track -Artist's Name ~~ " + "\n" + "  " + artistsName);
           // System.out.println ("~~ Track -Genre ~~ " + "\n" + "  " + genre);
        }
        
        /**
         * Returns the track reference number
         */
        public String referenceNumber()
        {
            return referenceNumber;
        }
    
        
        /**
         * Returns track name
         */
        public String trackName()
        {
            return trackName;
        }
    
        /**
         * Returns track artist's name
         */
        public String artistsName()
        {
            return artistsName;
        }
    
        /**
         * Returns track play length
         */
        public int playLength()
        {
            // put your code here
            return playLength;
        }
    
        /**
         * Returns track genre
         */
        public String genre()
        {
            return genre;
        }
    
        /**
         * Input of Track Reference Name
         */
        public void inputReferenceNumber(String newReferenceNumber)
        {
            referenceNumber = newReferenceNumber;
            int size = newReferenceNumber.length();        
            // Checks if reference name given to the track instance meets minimum requirements of three(3) charcters or more and if not, 
            //notifies user
            if (size <= 2)
        {
            System.out.println("Please re-input Track reference name");
            System.out.println("As it does not meet the minimum requirement of three(3) characters or more");
        }
        }
        
        /**
         * Creates a new instance with given values defined by the variables
         */
        public void newInstance(String newReferenceNumber, String newTrackName, String newArtistsName, String newGenre, int newPlayLength)
        {
            referenceNumber = newReferenceNumber;
            playLength = newPlayLength;
            trackName = newTrackName;
            artistsName = newArtistsName;
            genre = newGenre;
            int size = newReferenceNumber.length();
                    
            // Checks if reference name given to the track instance meets minimum requirements of three(3) charcters or more and if not, 
            //notifies user
            if (size <= 2)
        {
            System.out.println("Please re-input Track reference name");
            System.out.println("As it does not meet the minimum requirement of three(3) characters or more");
        }
    
        }
    }
    #PlayList Class:
    Java Code:
    public class PlayList
    {
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.ListIterator;
    import java.util.Collections;
        // Store a new piece of information about a music track
        private ArrayList<Track> tracks;
        int total = 0;
            /**
         * Performs any initialization that is required for the notebook.(Constructor)
         */
        public void PlayList()
        {
            // 
            tracks = new ArrayList<Track>();
        }
    
        public void addTrack()    //(String referenceNumber,String trackName,String artistsName,String genre,int playLength)
        {
            
            //Defines elements in the array
            Scanner input = new Scanner (System.in);
            String referenceNumber;
            String trackName;
            String artistsName;
            String genre;
            int playLength;
            
            // Asks user to input track reference number.
            System.out.print ("Enter a track reference number: "); 
            referenceNumber = input.nextLine();
    
            // Asks user to input track name.
            System.out.print ("Enter track name: "); 
            trackName = input.nextLine();
    
            // Asks user to input track artist's name.
            System.out.print ("Enter track artist's name: "); 
            artistsName = input.nextLine();
    
            // Asks user to input track genre.
            System.out.print ("Enter track genre: "); 
            genre = input.nextLine();
    
            // Asks user to input track play length
            System.out.print ("Enter track play length (*in seconds*): "); 
            playLength = input.nextInt();
    
            // Compiles user input into a track and adds to array.  *uses Track COnstructor*
            Track track = new Track(referenceNumber, trackName, artistsName, genre, playLength);
            tracks.add(track);
            
        }
    
        /**
         * Number of Tracks in play list.
         */
        public int numberOfTracks()
        {
            return tracks.size();
        }
    
        /**
         * Remove Track from the play list.
         */
        public void trackRemoval()
        {
            int trackNumber;
            Scanner input = new Scanner (System.in);
            System.out.print ("Enter track number to [Remove]: "); 
            trackNumber = input.nextInt();
            if (trackNumber < 0)
            {
                // This is not a valid track Number, so do nothing.
            }
            else if(trackNumber < numberOfTracks())
            // This is a valid track number
                tracks.remove(trackNumber);
        }
    
    [B]    /**
         * Print all Track information stored in array
         */
        public void printTracks()
        {
                total += (Track.playLength());
                ListIterator iterator = tracks.listIterator();
                while (iterator.hasNext())
                {
                    System.out.println(iterator.next());
                }
        }[/B]
    
    }
    Assistance would be greatly appreciated :)

    thanks :)

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    Track.playLength());
    is referring to the method statically. A static method is one you call with the following form
    Java Code:
    ClassName.methodName();
    Think of all the Math methods, they are all static methods, do you every create a math object first?

    What you need is an object of type track to call that method on.

    You need to somehow create an object of type track, then add that objects method calls return value to total.

    Java Code:
    class X{
      private int x;
      public int getX(){
        return x;
      }
    }
    public class XL{
      ArrayList<X> theXL = new ArrayList<X>();
      int total;
      public void printList(){
        for(X x : theXL){
          total += x.getX();
        }
      }
    }

  3. #3
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Hi sunde887,

    I'm not really understanding, could you please elaborate.

    thankyou

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

  5. #5
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Hi thanks for your advice, but i went another way

    Java Code:
        public void PlayList()
        {
            tracks = new ArrayList<Track>();
            
        }
        
        
        public void addTrack(Track newTrack)
        {   
            tracks = new ArrayList<Track>();
            tracks.add(newTrack);
        }
    This is now working, I will revisit implementing user input via Scanner when I have more knowledge in constructing a class better.

    Thanks

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    your add track method is incorrect, it defeats the purpose of creating a class with an array list.

    You intend to add an item to the array list, but what you really do, is create a new array list every time the method is called, and add the track to the array list. So the arraylist will only hold the last added track. The add track method should look like this.

    Java Code:
    public void addTrack(Track track){
      tracks.add(track);
    }
    All this does is add the track to the array list, so each time you call this method another track is added and the array list is expanded by 1.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your originally posted code was nearly perfect. The add method seemed like it would work fine. However, you imported inside the class, which is incorrect.

    The first thing to come in your source code is the package name(if there is one), then a list of all the import statements, then finally the class.
    Java Code:
    package name;
    
    import x;
    import y;
    import z;
    
    public class ABC{}
    The real error was all in your print method. Like I said you were referring to the playLength method statically.

    What you want to do is this
    Java Code:
    foreach track in tracks
      add playlength to total
      print track
    end foreach

  8. #8
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Hi sunde887,

    Sorry but the code actually works for me(adding tracks not printing), but you are right about the print method I will show this below in my code and also i could use track instead of newTrack, but either works was just a little habit to use new*.

    Firstly have to initialize the ArrayList invoking method:PlayList() this is after obviously creating the object first of the class PlayList

    secondly a then create 2 separate objects of the Class Track with the fields defined.

    once they are done then invoking the method addTrack() and choosing 1 of the track methods, then performing that again a second time, so adding to track objects to the arraylist.

    My addTrack Method:
    Java Code:
        public void addTrack(Track newTrack)
        {   
            tracks.add(newTrack);
        }
    My printTracks method:
    Java Code:
    System.out.println("List of Tracks currently in playlist");
            for(Track track : tracks)
            {
                track.trackDetailsComplete();
            }
            System.out.println("Number of Tracks in Play List: " + numberOfTracks());
    }
    *what do you think of the print method?, It is working for me now"

    baring in mind I have to Classes one Track and one PlayList.

    I am creating this in BlueJ by the way so not your standard IDE, I forgot to mention that, also these are not being packaged up so running from within a terminal

Similar Threads

  1. Replies: 5
    Last Post: 01-26-2011, 07:38 PM
  2. Replies: 2
    Last Post: 05-01-2010, 09:59 AM
  3. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 03:48 AM
  4. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  5. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 01:25 AM

Posting Permissions

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