Results 1 to 8 of 8
- 04-05-2011, 10:59 AM #1
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
#PlayList 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"); } } }
Assistance would be greatly appreciated :)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] }
thanks :)
- 04-05-2011, 11:18 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
is referring to the method statically. A static method is one you call with the following formJava Code:Track.playLength());
Think of all the Math methods, they are all static methods, do you every create a math object first?Java Code:ClassName.methodName();
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(); } } }
- 04-05-2011, 10:44 PM #3
Hi sunde887,
I'm not really understanding, could you please elaborate.
thankyou
- 04-05-2011, 11:03 PM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 04-05-2011, 11:26 PM #5
Hi thanks for your advice, but i went another way
This is now working, I will revisit implementing user input via Scanner when I have more knowledge in constructing a class better.Java Code:public void PlayList() { tracks = new ArrayList<Track>(); } public void addTrack(Track newTrack) { tracks = new ArrayList<Track>(); tracks.add(newTrack); }
Thanks
- 04-05-2011, 11:34 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
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.Java Code:public void addTrack(Track track){ tracks.add(track); }
- 04-05-2011, 11:39 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
The real error was all in your print method. Like I said you were referring to the playLength method statically.Java Code:package name; import x; import y; import z; public class ABC{}
What you want to do is this
Java Code:foreach track in tracks add playlength to total print track end foreach
- 04-06-2011, 09:26 AM #8
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:
My printTracks method:Java Code:public void addTrack(Track newTrack) { tracks.add(newTrack); }
*what do you think of the print method?, It is working for me now"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()); }
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
-
super class reference variable accesses overriding sub class method
By subith86 in forum New To JavaReplies: 5Last Post: 01-26-2011, 06:38 PM -
how call from inner class(anonymous or not), a method of parent class?
By lse123 in forum AWT / SwingReplies: 2Last Post: 05-01-2010, 08:59 AM -
Child-Class Calling a Method in a Parent-Class
By Blah_ in forum New To JavaReplies: 5Last Post: 09-29-2009, 02:48 AM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks