In this experiment, people take turns playing and you can delete or add players, Seem to have a couple of issues left, not sure how to work out. One of the problems is the display, the other is a class interface. Need any help or suggestions I can get. Thank you in advance!!

import java.lang.Comparable.*;
public class Person
{

    private String name;
    public int turnCount = 0;

    public Person(String input)
    {
        name = input;
    }
        public Person()
        {
            name = null;
        }

        public void incTurn()
        {
            turnCount++;
        }

        public String getName()
        {
            return name;
        }

        public void setName(String NewName)
        {
            name = NewName;
        }

    }


public class Node
{
    //private Person Name; 
    //private int item;
    private Node next;
    //private Object pop;
    private  Person newperson;

    //create first new node 
    public Node(Person p)
    {
        newperson = p;
        next = null;
    }

    //create new node to list
    public Node(Person p, Node nextNode)
    {
        newperson = p;
        next = nextNode;

    }

    //set new item
    public void setItem(Person p)
    {
        newperson=p;
    }

    //returning our new item as item
    public Person getItem()
    {
        return newperson;
    }

    //setting the pointer
    public void setNext(Node nextNode)
    {
        next = nextNode;
    }

    //returning next item
    public Node getNext()
    {
        return next;
    }   
}

public class ListIndexOutOfBoundsException
            extends IndexOutOfBoundsException {
  public ListIndexOutOfBoundsException(String s) {
    super(s);
  }  // end constructor
}  // end Exception

import java.lang.Object.*;
public class ListReferenceBased implements List
{
private Node head;
private int NumItems; // Number of items in the list
    public ListReferenceBased()
    {
    NumItems = 0;
    head = null;
    }
  public boolean isEmpty() {
    return NumItems == 0;
  }  // end isEmpty
  public int size() {
    return NumItems;
  }  // end size
  public Node find(int index) {
  // --------------------------------------------------
  // Locates a specified node in a linked list.
  // Precondition: index is the number of the desired
  // node. Assumes that 1 <= index <= numItems+1
  // Postcondition: Returns a reference to the desired 
  // node.
  // --------------------------------------------------
    Node curr = head;
    for (int skip = 1; skip < index; skip++) {
      curr = curr.getNext();
    } // end for
    return curr;
  } // end find
  public Object get(int index, Object dataItem) 
                throws ListIndexOutOfBoundsException {
    if ( index >= 1 && index <= NumItems) {
      // get reference to node, then data in node

       Node curr = find(index);
       dataItem = curr.getItem();
      return dataItem;

    } 
    else {
      throw new ListIndexOutOfBoundsException(
                "List index out of bounds on get");
    } // end if
  } // end get
  public void add(int index, Person item)
                  throws ListIndexOutOfBoundsException {
    if (index >= 1 && index <= NumItems+1) {
      if (index == 1) {
        // insert the new node containing item at
        // beginning of list
        Node newNode = new Node(item, head);
        head = newNode;
      } 
      else {
        Node prev = find(index-1);
        // insert the new node containing item after 
        // the node that prev references
        Node newNode = new Node(item, prev.getNext());
        prev.setNext(newNode);
      } // end if
      NumItems++;
    } 
    else {
      throw new ListIndexOutOfBoundsException(
                "List index out of bounds on add");
    } // end if
  }  // end add
  public void remove(int index) 
                     throws ListIndexOutOfBoundsException {
    if (index >= 1 && index <= NumItems) {
      if (index == 1) {
        // delete the first node from the list
        head = head.getNext();
      } 
      else {
        Node prev = find(index-1);
        // delete the node after the node that prev
        // references, save reference to node
        Node curr = prev.getNext(); 
        prev.setNext(curr.getNext());
      } // end if
      NumItems--;
    } // end if
    else {
      throw new ListIndexOutOfBoundsException(
                "List index out of bounds on remove");
    } // end if
  }   // end remove
  public void removeAll() {
    // setting head to null causes list to be
    // unreachable and thus marked for garbage 
    // collection
    head = null;
    NumItems = 0;
  } // end removeAll
} // end ListReferenceBased


import java.lang.Object.*;
public class LinkedListTester implements ListReferenceBased
{
private static Node curr;
private Node head;

    public static void main(String [] args)
    {
        boolean done = false;
        ListReferenceBased call = new ListReferenceBased();
        LinkedList<String> list = new LinkedList<String>();
        //head=call.find(1);        

        Scanner in = new Scanner(System.in);

        while(done != true)
        {
        System.out.println("Enter A to add, D to Delete, p to Display, or Done to quit");
        String command = in.next();


        if(command.equalsIgnoreCase("A"))
        {
            System.out.println("Enter name of player");
            String item = in.next();

            Person newperson = new Person(item);

            //Node newnode = new Node(newperson);     // Crates new node


            call.add(call.size()+1, newperson);

           /*
            get.setNext(head);
            head = newNode;
             newnode.setnext(curr.getNext);


            prev.setNext(newnode);

            NewNode = newNode(item);
            prev.setNext(curr.getNext(item));
            */ 


       }
       if(command.equalsIgnoreCase("D"))
       {
            System.out.println("Enter name of player");
            String item = in.next();
            int index=1;
            for(curr=call.find(1); curr.getNext()!=null;curr=curr.getNext())
            {
                Person currPerson = (Person)curr.getItem();
                if(currPerson.getName().equalsIgnoreCase(item))
                {
                     call.remove(index);
                }
                else
                    index++;
            }

        }
       if(command.equalsIgnoreCase("P"))
       {


             for(Node print = call.find(1); print.getNext()!= null; print = print.getNext())
           {
               System.out.println(curr.getItem().getName());

            }
            done = true;
        }


    }//End While
  }
}

Thanks again!!

Recommended Answers

All 3 Replies

Member Avatar for iamthwee

I might have a look at this later. For those who have time to look at it now I have tagged it up courtesy of astyle.

import java.lang.Comparable.*;
public class Person
{

  private String name;
  public int turnCount = 0;

  public Person ( String input )
  {
    name = input;
  }
  public Person()
  {
    name = null;
  }

  public void incTurn()
  {
    turnCount++;
  }

  public String getName()
  {
    return name;
  }

  public void setName ( String NewName )
  {
    name = NewName;
  }

} 
public class Node
{
//private Person Name;
//private int item;
  private Node next;
//private Object pop;
  private Person newperson;

//create first new node
  public Node ( Person p )
  {
    newperson = p;
    next = null;
  }

//create new node to list
  public Node ( Person p, Node nextNode )
  {
    newperson = p;
    next = nextNode;
  }

//set new item
  public void setItem ( Person p )
  {
    newperson = p;
  }

//returning our new item as item
  public Person getItem()
  {
    return newperson;
  }

//setting the pointer
  public void setNext ( Node nextNode )
  {
    next = nextNode;
  }

//returning next item
  public Node getNext()
  {
    return next;
  }
}

public class ListIndexOutOfBoundsException
      extends IndexOutOfBoundsException
{
  public ListIndexOutOfBoundsException ( String s )
  {
    super ( s );
  } // end constructor
} // end Exception

import java.lang.Object.*;
public class ListReferenceBased implements List
{
  private Node head;
  private int NumItems; // Number of items in the list
  public ListReferenceBased()
  {
    NumItems = 0;
    head = null;
  }
  public boolean isEmpty()
  {
    return NumItems == 0;
  } // end isEmpty
  public int size()
  {
    return NumItems;
  } // end size
  public Node find ( int index )
  {
// --------------------------------------------------
// Locates a specified node in a linked list.
// Precondition: index is the number of the desired
// node. Assumes that 1 <= index <= numItems+1
// Postcondition: Returns a reference to the desired
// node.
// --------------------------------------------------
    Node curr = head;
    for ( int skip = 1; skip < index; skip++ )
    {
      curr = curr.getNext();
    } // end for
    return curr;
  } // end find
  public Object get ( int index, Object dataItem )
    throws ListIndexOutOfBoundsException
    {
      if ( index >= 1 && index <= NumItems )
      {
// get reference to node, then data in node

        Node curr = find ( index );
        dataItem = curr.getItem();
        return dataItem;

      }
      else
      {
        throw new ListIndexOutOfBoundsException (
          "List index out of bounds on get" );
      } // end if
    } // end get
  public void add ( int index, Person item )
    throws ListIndexOutOfBoundsException
    {
      if ( index >= 1 && index <= NumItems + 1 )
      {
        if ( index == 1 )
        {
// insert the new node containing item at
// beginning of list
          Node newNode = new Node ( item, head );
          head = newNode;
        }
        else
        {
          Node prev = find ( index - 1 );
// insert the new node containing item after
// the node that prev references
          Node newNode = new Node ( item, prev.getNext() );
          prev.setNext ( newNode );
        } // end if
        NumItems++;
      }
      else
      {
        throw new ListIndexOutOfBoundsException (
          "List index out of bounds on add" );
      } // end if
    } // end add
  public void remove ( int index )
    throws ListIndexOutOfBoundsException
    {
      if ( index >= 1 && index <= NumItems )
      {
        if ( index == 1 )
        {
// delete the first node from the list
          head = head.getNext();
        }
        else
        {
          Node prev = find ( index - 1 );
// delete the node after the node that prev
// references, save reference to node
          Node curr = prev.getNext();
          prev.setNext ( curr.getNext() );
        } // end if
        NumItems--;
      } // end if
      else
      {
        throw new ListIndexOutOfBoundsException (
          "List index out of bounds on remove" );
      } // end if
    } // end remove
  public void removeAll()
  {
// setting head to null causes list to be
// unreachable and thus marked for garbage
// collection
    head = null;
    NumItems = 0;
  } // end removeAll
} // end ListReferenceBased 
import java.lang.Object.*;
public class LinkedListTester implements ListReferenceBased
{
  private static Node curr;
  private Node head;

  public static void main ( String [] args )
  {
    boolean done = false;
    ListReferenceBased call = new ListReferenceBased();
    LinkedList<String> list = new LinkedList<String>();
//head=call.find(1);

    Scanner in = new Scanner ( System.in );

    while ( done != true )
    {
      System.out.println ( "Enter A to add, D to Delete, p to Display, or Done to quit" );
      String command = in.next(); 
      if ( command.equalsIgnoreCase ( "A" ) )
      {
        System.out.println ( "Enter name of player" );
        String item = in.next();

        Person newperson = new Person ( item );

//Node newnode = new Node(newperson); // Crates new node 
        call.add ( call.size() + 1, newperson );

        /*
        get.setNext(head);
        head = newNode;
        newnode.setnext(curr.getNext); 
        prev.setNext(newnode);

        NewNode = newNode(item);
        prev.setNext(curr.getNext(item));
        */ 
      }
      if ( command.equalsIgnoreCase ( "D" ) )
      {
        System.out.println ( "Enter name of player" );
        String item = in.next();
        int index = 1;
        for ( curr = call.find ( 1 ); curr.getNext() != null; curr = curr.getNext() )
        {
          Person currPerson = ( Person ) curr.getItem();
          if ( currPerson.getName().equalsIgnoreCase ( item ) )
          {
            call.remove ( index );
          }
          else
            index++;
        }

      }
      if ( command.equalsIgnoreCase ( "P" ) )
      { 
        for ( Node print = call.find ( 1 ); print.getNext() != null; print = print.getNext() )
        {
          System.out.println ( curr.getItem().getName() );
        }
        done = true;
      } 
    }//End While
  }
}

Please use [code]

[/code] tags for future reference.

Anyone with suggestions??

In this experiment, people take turns playing and you can delete or add players, Seem to have a couple of issues left, not sure how to work out. One of the problems is the display, the other is a class interface. Need any help or suggestions I can get. Thank you in advance!!

<snipped>

What are the issues specifically? Display and "a class interface" aren't much to go on. State what is wrong and your expectations of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.