Link List of Integers


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Link List of Integers

  1. #1
    Join Date
    Jul 2005
    Posts
    3

    Exclamation Link List of Integers

    Hello,

    First off, I'm new to programming so if you're reading my post and shaking heads, please bear with me . I'm currently doing some practice programs for my Java class dealing with Linked Structures.

    I have a IntList.java class which contain the definitions for a linked list of integers. The class also contains an inner class IntNode, which holds info for a single node within the list and the following IntList methods of:

    public IntList()
    public void addToFront(int val)
    public void addToEnd(int val)
    public void removeFirst()
    public void print()

    I then have an IntListTest.java file that let's me use these methods...Here's where things get complicated (for me anyways); I'm suppose to add the following methods into the IntList clas, and add the option to the driver IntListTest...

    public int length() -- which returns the number of elements in the list
    public String toString() -- returns a String containing the print val of the list
    public void removeLast()
    public void search(int input)
    public void insertAt(int index, int newValue)
    public void removeAt(int index)

    The only thing that I need is advice on how to start adding each method. This class would be a lot easier if it were face to face, but this is an online course that is proving to be certainly challenging. I hope anyone can provide input for this program. F.Y.I: I using a java compiler version 1.5...

    Onip28

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    So you should have an IntList.java file that looks something like:
    Code:
    public class IntList {
      public IntList() { ... }
      public void addToFront(int val) { ... }
      public void addToEnd(int val) { ... }
      public void removeFirst() { ... }
      public void print() { ... }
    }
    In order to add a method to the class, you just need to add another method declaration. e.g.
    Code:
    public class IntList {
      public IntList() { ... }
      public void addToFront(int val) { ... }
      public void addToEnd(int val) { ... }
      public void removeFirst() { ... }
      public void print() { ... }
    
      public int length() { ... }
      public String toString() { ... }
      public void removeLast() { ... }
      public void search(int input) { ... }
      public void insertAt(int index, int newValue) { ... }
      public void removeAt(int index) { ... }
    }
    I put "..." where the actual code for each method gets filled in. I'm not sure exactly what your IntListTest.java looks like. I would guess that it is a JUnit test class in which case it would look something like:
    Code:
    public class IntListTest extends TestCase {
      public void testAddToFront() { ... }
      public void testAddToEnd() { ... }
      public void testRemoveFirst() { ... }
      public void testPrint() { ... }
    
      public int testLength() { ... }
      public String testToString() { ... }
      public void testRemoveLast() { ... }
      public void testSearch() { ... }
      public void testInsertAt() { ... }
      public void testRemoveAt() { ... }
    }
    In this case you would fill in the "..." with code to test your functions and assert statements. For example a very simple test for the length() function might be:
    Code:
    public void testLength() {
      IntList list = new IntList();
      assertEquals("list should start as empty", list.length(), 0);
      
      list.addToFront(2);
      assertEquals("list with 1 element should have length of 1", list.length(), 1);
    
      list.addToFront(1);
      assertEquals("list with 2 elements should have length of 2", list.length(), 2);
    }
    This probably wouldn't be enough to throughly test the length() method but it's a start.

    Hope this helps.
    ~evlich

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center