Looking for advice

I have to create a Positional container. I am supposed to implement this container. I am given the interface (Position.java) for a myPositions class I'm supposed to create, the sequence interface (sequence.java) which is implemented by the mySequence.java class that I am supplied with. I am simply given a bare-bones layout for methods I have to use according to the interfaces. In addition, I am given Problem.java which is the test class which will work with the methods of mySequence.java to create, insert, compare, swap, etc... elements added to the Positional Container.

My question:  I understand that I am supposed to create a myPositions.java class...I do not understand what it's purpose is though. Also, in mySequence.java I'm told to make my container based off of a linkedlist or an arrayList.. I can add any methods, variables or classes as I wish. I don't know how to go about creating a linkedlist/arrayList and use the given methods to access the data using Positions. I hope this makes sense. Any help, advice, information would be greatly appreciated. Here's the files:

note: the sequence package contains mySequence.java, Position.java, and Sequence.java
note: I'm not allowed to use java.util, or any other java libraries for linked lists, etc...

mySequence.java
================================================================
package sequence;

public class MySequence implements Sequence{

   public MySequence(){
               //TODO
   }

   /**
    * Inserts the comparable object into the container and returns the Position
    * of that object in the container.
    */
   public Position insert(Comparable o) {
               //TODO
               return null;
   }
   /**
    * Removes the item at the given Position and returns the object
    * stored there.
    */
   public Comparable remove(Position p) {
               //TODO
            return null;
   }
   
   /**
    * Returns the Position that comes after the given Position, or
    * null, if no such Position exists.
    */
   public Position getNext(Position p){
            //TODO
            return null;
   }
   /**
    * Returns the Position that comes before the given Position, or
    * null, if no such Position exists.
    */
   public Position getPrev(Position p){
            //TODO
               return null;
   }
   /**
    * Swaps the items stored in positions a and b.
    */
   public void swap(Position a, Position b){
            //TODO
   }

   public Position getFirst(){
            //TODO
            return null;
   }
   public Position getLast(){
            //TODO
            return null;
   }

   public boolean isEmpty() {
            //TODO
            return false;
   }


}
================================================================

Position.java (interface)
================================================================
package sequence;


/**
 * Position interface for the sequence problem.
 */
public interface Position {
      /**
       * The method retrieves and returns the Comparable stored at  this position
       * @return The comparable stored here.
       */
  public Comparable element ();
}
================================================================

Sequence.java (interface)
================================================================
package sequence;

public interface Sequence{

   public Position insert(Comparable o);
   public Position getFirst();
   public Position getLast();
   public Comparable remove(Position p);
   public Position getNext(Position p);
   public Position getPrev(Position p);
   public void swap(Position a, Position b);
   public boolean isEmpty();

}
================================================================

Problem.java
================================================================
import sequence.*;
import java.util.Random;

public class Problem {  
      Sequence seq;
      
      public Problem(){
            seq = new MySequence(); //MySequence();  //Your sequence goes here.
      }
      
      public void doIt(){
            insertValues(20);
            print();
            sortIt();
            print();
      }
      
      /**      
       * inserts some random values into the sequence
       */
      private void insertValues(int n){
            Random rand = new Random();
            for(int i=0; i < n;i++){
                  seq.insert(new Integer(rand.nextInt(50)));  
            }
            
      }
      
      private void sortIt(){
            for(Position p = seq.getLast(); p != seq.getFirst(); p=seq.getPrev(p)){
                  for(Position q = seq.getFirst(); q != p; q=seq.getNext(q)){
                        //compare q to its neighbor
                        if( (q.element()).compareTo(seq.getNext(q).element()) > 0){
                              //swap them
                              seq.swap(q, seq.getNext(q));
                        }
                  }
            }
      }
      
      private void print(){
            Position p = seq.getFirst();
            System.out.print("Sequence:");
            do{
                  System.out.print(p.element() + " ");
                  p = seq.getNext(p);
            }while(p != null);
            System.out.println();
      }
      
      public static void main(String args[]){
            Question1 sort = new Question1();
            sort.doIt();
      }
}
================================================================
Thanks so very much in advance.
LVL 9
D4LyAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
> ie: how do i represent element?

looks like you could just store the Comparable instance as a member variable

> also, when you write list.add() 3 comments ago, what is add?

add() is a method in the ArrayList class.
http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html
0
 
objectsCommented:
have an instanmce of an ArrayList as a member variable and have your merthods operate on that list:

public class MySequence implements Sequence{

   private ArrayList list = new ArrayList();

   public MySequence(){
   }

   /**
    * Inserts the comparable object into the container and returns the Position
    * of that object in the container.
    */
   public Position insert(Comparable o) {
        list.add(o);
        return null;  // you'll need to return a Position instance here
   }

   similiar for other methods
0
 
objectsCommented:
> I understand that I am supposed to create a myPositions.java class...I do not understand what it's purpose is though.

you need an implementation of the Positions interface

eg.

public myPositions implements Position
{
   public Comparable element ()
   {
       // implementation here
   }
}


you'll need to return an insyance of this class in the example insert() method I posted above.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
D4LyAuthor Commented:
objects, when/where am i supposed to reference the myPositions class? what is its purpose? i don't get the concept of it all.
0
 
objectsCommented:
Interfaces only define the methods that implementing classes must provide, you cannot create an instance of them.
So for example in the insert() method above you need to return an object that implements Position, that what you need to write.
0
 
D4LyAuthor Commented:
alright, i think i'm starting to get it...what does my implementation look like? ie: how do i represent element? also, when you write list.add() 3 comments ago, what is add? i understand what it is doing, but i don't have an add method anywhere.
0
 
D4LyAuthor Commented:
ah, got the add() method now. still very confused about how Positions ties into everything conceptually.
0
 
objectsCommented:
the myPosition implementation could be as simple as a wrapper around a Comparable, depends on the exact detaiuls of the requirements:

public myPosition implements Position
{
   private Comparable c;

   public myPosition(Comparable o)
   {
      c = o;
   }

   public Comparable element ()
   {
       return c;
   }
}


0
 
D4LyAuthor Commented:
thx...i'll probably be making another question or two later!
0
 
objectsCommented:
will keep an eye out for it :)
0
 
D4LyAuthor Commented:
ok, hopefully you get this comment!

for each method similar to insert in the mySequence class, you said i need to return a Position instance, which makes sense...but what exactly is the instance's value each time? what am I returning? and where do i grab this from?? I am quite a noob to java...but i'm sure you already figured that. sry for the questions!!! thanks so much.
0
 
objectsCommented:
you'll return a new instance of your myPosition class that wraps the relevent element.
0
All Courses

From novice to tech pro — start learning today.