Solved

Looking for advice

Posted on 2004-10-02
13
305 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 92

Accepted Solution

by:
objects earned 250 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now