Friday, 15 June 2012

java - Trying to get my playCount method to work in music player -



java - Trying to get my playCount method to work in music player -

i'm trying create playcount method musicplayer.

in track method have defined field playcount , created method increments it. can't seem have called in main class when song played.

public class track { // artist. private string artist; // track's title. private string title; // track stored. private string filename; public int playcount; /** * constructor objects of class track. * @param artist track's artist. * @param title track's title. * @param filename track file. */ public track(string artist, string title, string filename) { setdetails(artist, title, filename); playcount = 0; } public void resetcount(){ playcount = 0; } public void incrementcount(){ playcount++; }

and here's main class code omitted.

public class musicorganizer { // arraylist storing music tracks. private arraylist<track> tracks; // player music tracks. private musicplayer player; // reader can read music files , load them tracks. private trackreader reader; /** * create musicorganizer */ public musicorganizer() { tracks = new arraylist<track>(); player = new musicplayer(); reader = new trackreader(); readlibrary("audio"); system.out.println("music library loaded. " + getnumberoftracks() + " tracks."); system.out.println(); } /** * add together track file collection. * @param filename file name of track added. */ public void addfile(string filename) { tracks.add(new track(filename)); } /** * add together track collection. * @param track track added. */ public void addtrack(track track) { tracks.add(track); } /** * play track in collection. * @param index index of track played. */ public void playtrack(int index) { if(indexvalid(index)) { track.incrementcount(); //this problem is. want phone call increment method adds one. stopplaying(); track track = tracks.get(index); player.startplaying(track.getfilename()); system.out.println("now playing: " + track.getartist() + " - " + track.gettitle()); } }

what error "non static method incrementcount() cannot referenced static context. not understand , how should prepare it. , help appreciated!

you should phone call on track instance, not track class.

public void playtrack(int index) { if(indexvalid(index)) { track track = tracks.get(index); stopplaying(); player.startplaying(track.getfilename()); track.incrementcount(); system.out.println("now playing: " + track.getartist() + " - " + track.gettitle()); } }

java

No comments:

Post a Comment