Hi everyone, Need some help with a couple of problems. 1. I have to incorporate a linked list and its methods I have made into another class for an assignment. Keep getting non-static method .... cannot be referenced from a static context. 2. Am trying to write a recursive method that will return a) the total track time for a CD & b) the total track time for the entire catalogue. Both codes for question are below, thanks in advance!! 1. Put this method class CDList { private CDNode head; private TrackNode trackHead;
//constructor public CDList() { head = null; trackHead = null; }
//addCD() method: Will add CD to list public void addCD(String artist, String title) { CDNode newNode = new CDNode(artist, title); CDNode current;
//if no other CD's exist add to head if (head == null) { newNode.next = head; head = newNode; }
//when other nodes already present else { current = head; while (current.next != null) current = current.next; current.next = newNode; } } into this other class & method //first part of class switch(item) { // 'a' pressed -- add a cd function case 'a': addCD(); break; //the first part of the method public static void addCD() { String artistName; String cdTitle;
// prompt for the name of the artist and the cd's title System.out.print("\nEnter the artist's name: "); artistName = safeReadLine();
System.out.print("\nEnter the CD title: "); cdTitle = safeReadLine();
// *********************************************** // here you know both the title and artist name of // the CD you have to create // artistName -- String holding the artist name // cdTitle -- String holding the cd's title // *********************************************** etc etc. 2. This is my attempt at the recursive method to calc the track time, isn't working (have included nodes for clarity) class CDNode { protected String cdTitle; protected String cdArtist; Track trackHead; CDNode next;
public CDNode (String title, String artist) { cdArtist = artist; cdTitle = title; trackHead = null; next = null; } } class TrackNode { protected String trackTitle; protected int trackLength; TrackNode next;
public TrackNode (String title, int length) { trackTitle = title; trackLength = length; next = null; } } public int CDTime(TrackNode current) { current = trackHead; int result = 0;
if (current == null) return result;
else return result + current.trackLength + CDTime(current.next); }
Ryan, Your error message should be giving you the clue, somewhere in a static method you are calling an instance method without an object reference. i.e. you are doing 'method()' and compiler expects 'obj.method()'. It may be that you should change 'method()' to be static OR change the static method to be an instance method. In class TrackNode what is the purpose of 'int result' if it's value never changes ? Where is 'trackHead' defined ? [This message has been edited by Colin Kenworthy (edited December 06, 2001).]