This discussion is archived
10 Replies Latest reply: Sep 25, 2012 2:30 AM by rukbat RSS

Small cusom class with generic dataypes.

843785 Newbie
Currently Being Moderated
I'm working on a little project for a class in algorithm analysis. I have to use Java to create a simple list data type from scratch (BasicList); it has 8 functions + constructor. The implementation must be self-contained (no import declarations) and the functions are designated by an interface:
interface LeanList <ItemType> // A basic list ADT
{
public static final int MAX_LENGTH=10;

public boolean insertItem(ItemType newItem);
public boolean deleteItem();
public boolean advance();
public boolean retreat();
public boolean jumpTo(int pos);
public boolean validPos();
public ItemType getItem();
public int length();
The idea is to create a class that another developer could utilize. You cannot declare any public classes other than the ones listed in the interface, plus the contructor. The assignment has also specified that my BasicList class must start as follows:
public class BasicList <ItemType> implements LeanList <ItemType>
{
  . . .
  . . .
  . . .
} 
I have a few problems with this....for starters, I must change the first line to include "abstract" to avoid compiler errors. Also, I'm very new to the use of generic data types, and some of my code gives "type-mismatch" errors.
I've got the code to compile without errors using suggested fixes in eclipse, but the code seems drastically wrong:
public abstract class BasicList<ItemType> implements LeanList<ItemType>
{ 
     private int m; //list lenght counter
     private int p; // current position in list
     private BasicList[] thisList;//array of generic data

     public BasicList()
     {
          m = 0;
          p = 0;
          thisList = new BasicList[MAX_LENGTH]; 
     }
     
     public boolean insertItem(ItemType newItem)
     {
          if (p == 0 || m == MAX_LENGTH)
          {
               return false;
          }
          else
          {
               for (int count= p; count<=m;count++)
               {
                    thisList[count+1] = thisList[count];
               }
               m++;
               thisList[p] =  (BasicList) newItem;
               return true;
          }
     }
     
     public boolean deleteItem()
     {
          if (m==0 || p==0 || p==m+1)
          {
                    return false;
          }
          else
          {
               for (int count = p; count <= m; count++)
               {
                    thisList[count] = thisList[count+1];
               }
               thisList[m] = null;
               m--;
               return true;
          }
          
     }
     public boolean advance()
     {
          if (p==m+1)
          {
               return false;
          }
          else
          {
               p++;
               return true;
          }
     }
     public boolean retreat()
     {
          if (p==0)
          {
               return false;
          }
          else
          {
               p--;
               return true;
          }
     }
     public boolean jumpTo(int pos)
     {
          if (pos < 0 || pos > m+1)
          {
               return false;
          }
          else
          {
               p = pos;
               return true;
          }
     } 
     public boolean validPos()
     {
          if (1 <= p && p <= m)
          {
               return true;
          }
          else
          {
               return false;
          }
     } 
     public ItemType getItem()
     {
          if (1 <= p && p <= m)
          {
               return (ItemType)thisList[p];
          }
          else
          {
               return null;
          }
      
     }
     public int length()
     {
          return m;
     } 
     
}
I've already decided that programming is not necessarily the best avenue for me, but I really don't wanna bomb the class..... can anyone make this easier to understand? shed any light?
thx in advance.
  • 1. Re: Small cusom class with generic dataypes.
    843785 Newbie
    Currently Being Moderated
    Well, you don't want to declare your class abstract if you want to do anything useful with it. What error is it giving you?
    private BasicList[] thisList;//array of generic data{}
    
    That should be ItemType[] instead of BasicList[], since you probably don't want to have a list that contains other lists.
    
    Also your indices are wrong, from a quick look you can advance() to the last position and try to insert, getting an ArrayIndexOutOfBoundException, because your list is tries to start from 1,
    whereas lists and arrays usually start from 0.
    
    
    Post the error messages you're getting.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 2. Re: Small cusom class with generic dataypes.
    843785 Newbie
    Currently Being Moderated
    Thanks for your reply...after following your first few tips, I've come to code that looks like this:
    public class BasicList<ItemType> implements LeanList<ItemType>
    { 
         private int m;
         private int p;
         private ItemType[] thisList;
    
         public BasicList()
         {
              m = 0;
              p = 0;
              thisList = (ItemType[]) new BasicList[MAX_LENGTH]; 
         }
         
         public boolean insertItem(ItemType newItem)
         {
              if (p == 0 || m == MAX_LENGTH)
              {
                   return false;
              }
              else
              {
                   for (int count= p; count<=m;count++)
                   {
                        thisList[count+1] = thisList[count];
                   }
                   m++;
                   thisList[p] =  (ItemType) newItem;
                   return true;
              }
         }
         
         public boolean deleteItem()
         {
              if (m==0 || p==0 || p==m+1)
              {
                        return false;
              }
              else
              {
                   for (int count = p; count <= m; count++)
                   {
                        thisList[count] = thisList[count+1];
                   }
                   thisList[m] = null;
                   m--;
                   return true;
              }
              
         }
         public boolean advance()
         {
              if (p==m+1)
              {
                   return false;
              }
              else
              {
                   p++;
                   return true;
              }
         }
         public boolean retreat()
         {
              if (p==0)
              {
                   return false;
              }
              else
              {
                   p--;
                   return true;
              }
         }
         public boolean jumpTo(int pos)
         {
              if (pos < 0 || pos > m+1)
              {
                   return false;
              }
              else
              {
                   p = pos;
                   return true;
              }
         } 
         public boolean validPos()
         {
              if (1 <= p && p <= m)
              {
                   return true;
              }
              else
              {
                   return false;
              }
         } 
         public ItemType getItem()
         {
              if (1 <= p && p <= m)
              {
                   return (ItemType)thisList[p];
              }
              else
              {
                   return null;
              }
          
         }
         public int length()
         {
              return m;
         } 
         
    }
    and the only compiler error is :

    BasicList.java uses unchecked or unsafe operations.

    Does any of this make sense or am I way off track here?
    thx
  • 3. Re: Small cusom class with generic dataypes.
    843785 Newbie
    Currently Being Moderated
    That's not a compiler error, that's a compiler warning.
    thisList = (ItemType[]) new BasicList[MAX_LENGTH]; 
    That's shite, you want
    thisList = new ItemType[MAX_LENGTH]; 
    Again, it's a list of ItemType objects (where the actual type depends on the generic parameter passed to it when used), not a list of lists.

    Stop cramming BasicList in there, the only place where you need BasicList is in the class' name and the constructor.
  • 4. Re: Small cusom class with generic dataypes.
    843785 Newbie
    Currently Being Moderated
    ok, making progress....
    first, I thought this section
    public BasicList()
         {
              m = 0;
              p = 0;
              thisList = new ItemType[MAX_LENGTH]; 
         }
    is the constructor....which is why i'm trying to declare a new BasicList.....

    Second, using your change to line 11, there is a compiler error for "generic array creation"; eclipse elaborates by telling me that I can't do that....

    where to go from here?
  • 5. Re: Small cusom class with generic dataypes.
    843785 Newbie
    Currently Being Moderated
    RiTarDid wrote:
    ok, making progress....
    first, I thought this section
    public BasicList()
         {
              m = 0;
              p = 0;
              thisList = new ItemType[MAX_LENGTH]; 
         }
    is the constructor....which is why i'm trying to declare a new BasicList.....
    It is the constructor, and it's called when you do new BasicList();
    What is wrong is that you were trying to create an array of BasicLists in BasicList's constructor. Which would result in you having
    a list of lists of lists of lists of lists...if you understand the point.

    Since you're storing items in your list, you want them to be of type ItemType, not of type BasicList.
    Second, using your change to line 11, there is a compiler error for "generic array creation"; eclipse elaborates by telling me that I can't do that....
    Ah, I guess generics expects you to create untyped arrays then.
    thisList = new Object[MAX_LENGTH]; 
    should work.
  • 6. Re: Small cusom class with generic dataypes.
    843785 Newbie
    Currently Being Moderated
    Excellent, i think that will work.....i don't know where i got the idea that a default constructor had to contain a new BasicList....
    Thanks again :D
  • 7. Re: Small cusom class with generic dataypes.
    795426 Newbie
    Currently Being Moderated
    -Kayaman- wrote:
    Ah, I guess generics expects you to create untyped arrays then.
    thisList = new Object[MAX_LENGTH]; 
    should work.
    It's not that per se. The type hierarchy for arrays is not typesafe (i.e. Integer[ ] is a subclass of Number[ ], which will gladly let you try to put a Double into your Integer[ ]). Since generics is designed to guarantee type safety, they don't work together at all and the compiler will prevent you from trying to use generics in a non-typesafe way.

    If you can convince yourself that the cast to a generic array is typesafe, you can tell the compiler to ignore the warning using the @SuppressWarnings({"unsafe"}) annotation, which can be attached to a method, block, or definition-with-assignment.
  • 8. Re: Small cusom class with generic dataypes.
    964100 Newbie
    Currently Being Moderated
    Hi,

    I am doing this assignment and was wondering how you made out with it?

    Thanks
  • 9. Re: Small cusom class with generic dataypes.
    rp0428 Guru
    Currently Being Moderated
    Welcome to the forum!

    So you used your very first forum post to resurrect a thread that is four years old?
    >
    I am doing this assignment and was wondering how you made out with it?
    >
    Chances are the OP either flunked out and took up another career or solved the problem and has been gainfully employed for the past four years.

    If you have a question or issue you need to open your own thread, post your own code and explain what you are having a problem with.

    Please read the FAQ for how to post a question on the forum.
  • 10. Re: Small cusom class with generic dataypes.
    rukbat Guru Moderator
    Currently Being Moderated
    Moderator Action:
    Locking the thread so that it won't be resurrected again.