?
Solved

Looking for advice

Posted on 2004-10-02
13
Medium Priority
?
315 Views
Last Modified: 2012-08-14
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.
0
Comment
Question by:D4Ly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12209787
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
 
LVL 92

Expert Comment

by:objects
ID: 12209834
> 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
 
LVL 9

Author Comment

by:D4Ly
ID: 12209891
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 12209909
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
 
LVL 92

Expert Comment

by:objects
ID: 12209911
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12209921
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
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 12209933
> 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
 
LVL 9

Author Comment

by:D4Ly
ID: 12209935
ah, got the add() method now. still very confused about how Positions ties into everything conceptually.
0
 
LVL 92

Expert Comment

by:objects
ID: 12209946
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
 
LVL 9

Author Comment

by:D4Ly
ID: 12209962
thx...i'll probably be making another question or two later!
0
 
LVL 92

Expert Comment

by:objects
ID: 12209969
will keep an eye out for it :)
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12209998
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
 
LVL 92

Expert Comment

by:objects
ID: 12210028
you'll return a new instance of your myPosition class that wraps the relevent element.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question