# Linked List and recursion help

Matt Ryann
Greenhorn
Posts: 1
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,
1. Put this method
class CDList
{

//constructor
public CDList()
{
}

public void addCD(String artist, String title)
{
CDNode newNode = new CDNode(artist, title);
CDNode current;

{
}

else
{
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':
break;
//the first part of the method
{
String artistName;
String cdTitle;

// prompt for the name of the artist and the cd's title
System.out.print("\nEnter the artist's name: ");

System.out.print("\nEnter the CD title: ");

// ***********************************************
// 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;
CDNode next;

public CDNode (String title, String artist)
{
cdArtist = artist;
cdTitle = title;
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)
{
int result = 0;

if (current == null)
return result;

else
return result + current.trackLength + CDTime(current.next);
}

Colin Kenworthy
Ranch Hand
Posts: 88
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).]