putting a program together

can you help me put this program all together
import java.io.*;
import pArrayList;
public class Deque{
    protected Object[] array;
    protected int start,end,number;
    public Deque(int maxsize){
        array = new Object[maxsize];
        start = end = number = 0;
    }
    public boolean isEmpty(){
        return number == 0;
    }
    public boolean isFull(){
        return number >= array.length;
    }
    public int size(){
        return number;
    }
    public void insertRight(Object o){
        if(number < array.length){
            array[start = (++start % array.length)] = o;
            number++;
        }
    }
    public void insertLeft(Object o){
        if(number < array.length){
            array[end] = o;
            end = (--end + array.length) % array.length;
            number++;
        }
    }
    public Object removeRight(){
        if(isEmpty())
            return null;
        number--;
        int i = start;
        start = (--start + array.length) % array.length;
        return array[i];
    }
    public Object removeLeft(){
        if(isEmpty())
            return null;
        number--;
        return array[end = (++end % array.length)];
    }
    public Object peek(int n){
        if(n >= number)
            return null;
        return array[(end + 1 + n) % array.length];
    }
}
****************************
 
class pArrayListTest{
    public static void main(String[] args){
        pArrayList l = new pArrayList(10);
        Integer j = null;
        int i;
        System.out.println("starting...");
        for(i=0;i<5;i++){
            j = new Integer((int)(Math.random() * 100));
            l.insertRight(j);
            System.out.println("insertright: " + j);
        }
        while(!l.isFull()){
            j = new Integer((int)(Math.random() * 100));
            l.insertLeft(j);
            System.out.println("insertleft: " + j);
        }
        for(i=0;i<l.size();i++)
            System.out.println("peek "+i+": "+l.peek(i));
        for(i=0;i<5;i++)
            System.out.println("removeright: " + ((Integer)l.removeRight()));
        while(!l.isEmpty())
            System.out.println("removeleft: " + ((Integer)l.removeLeft()));
        System.out.println("Done ;-)");
    }
}
=======================================================================
=======================================================================
public class PriorityQ { 
    private Comparable[] array; 
    private int index; 
    int maxIndex = 0; 
//The instance variables are declared private so that 
//other classes cannot have direct access to them.
    public PriorityQ() { 
        array = new Comparable [16]; 
        index = 0;
        
    } 
    public boolean isEmpty () { 
        return index == 0; 
    } 
 public boolean isFull()   // true if queue is full
      { return (index == maxIndex); 
      }
 
 
 
public void insert (Comparable item) { 
//insert is similar to push:
     if (index == array.length) { 
//can do resize (); 
        } 
        array[index] = item; 
        index++; 
    } 
//the remove method has to traverse the array to find and 
//remove the largest item:
    public Comparable remove () { 
        if (index == 0) return null; 
        
        int maxIndex = 0; 
// find the index of the item with the highest priority 
        
        for (int i=1; i<index; i++) { 
            if (array[i].compareTo (array[maxIndex]) > 0) { 
                maxIndex = i; 
            } 
        } 
        Comparable result = array[maxIndex]; 
// move the last item into the empty slot 
        index--; 
        array[maxIndex] = array[index]; 
        return result; 
    } 
}
****************************
class PriorityQApp
   {
   public static void main(String[] args)
      {
       
       PriorityQ pq = new PriorityQ (); 
        Integer item = new Integer (17); 
        pq.insert (item); 
  
           
  while (!pq.isEmpty ()) { 
        item = (Integer) pq.remove (); 
        System.out.println (item); 
    } 
 } 
}  
  
 
 
 
=====================================================================
=====================================================================
 
public class CList
  {
    class Node
      { int val; int next; }
    Node M[]; 
    int free, max = 10000;
    
    CList() 
      { 
        M = new Node[max+1]; 
        for (int i = 0; i < max; i++)
          { M[i] = new Node(); M[i].next = i+1; }
        M[max] = new Node(); M[max].next = 0;
        free = 0;
      }
            
    Node next(Node x)
      { return M[x.next]; }
    int val(Node x)
      { return x.val; }
            
    Node insert(Node x, int v) 
      { 
        int i = free; free = M[free].next;
        M[i].val = v; 
        if (x == null) M[i].next = i;
        else { M[i].next = x.next;  x.next = i; }
        return M[i];
      }
            
    void delete(Node x) 
      { int i = x.next; x.next = M[i].next; 
        M[i].next = free; free = i;
      }
  }
************************* 
class DoIt
  { 
      public static void main(String[] args)
      { 
                
      int N = Integer.parseInt(args[0]);
      int M = Integer.parseInt(args[1]);
                  
      CList L = new CList();
             
        CList.Node x = null;
        for (int i = 1; i <= N; i++)
          x = L.insert(x, i); 
        while (x != L.next(x))
          {
            for (int i = 1; i < M; i++) 
              x = L.next(x);
            L.delete(x);
          }
        System.out.println("What's Left is " + L.val(x));
      }
}
======================================================================
======================================================================
public class CList
  {
    static class Node
      { int val; Node next;
        Node(int v) { val = v; }
      }
    
    Node next(Node x)
      { return x.next; }
    int val(Node x)
      { return x.val; }
    
    Node insert(Node x, int v) 
      { Node t = new Node(v);
        if (x == null) t.next = t;
        else { t.next = x.next;  x.next = t; }
        return t;
       }
    
    void delete(Node x) 
      { x.next = x.next.next; }
  }

Open in new window

elrayissAsked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
You could solve the problem by doing something like this:

int N = 0;
int M = 0;
if (args.size()>0)
   N = Integer.parseInt(args[0]);
if (args.size()>1)
   M = Integer.parseInt(args[1]);
0
 
elrayissAuthor Commented:
the following is what i got but i am facing some problems when running it. thank youu.
first i got the Deque in a file
public class Deque {
	protected Object[] array;
    protected int start,end,number;
    public Deque(int maxsize){
        array = new Object[maxsize];
        start = end = number = 0;
    }
    public boolean isEmpty(){
        return number == 0;
    }
    public boolean isFull(){
        return number >= array.length;
    }
    public int size(){
        return number;
    }
    public void insertRight(Object o){
        if(number < array.length){
            array[start = (++start % array.length)] = o;
            number++;
        }
    }
    public void insertLeft(Object o){
        if(number < array.length){
            array[end] = o;
            end = (--end + array.length) % array.length;
            number++;
        }
    }
    public Object removeRight(){
        if(isEmpty())
            return null;
        number--;
        int i = start;
        start = (--start + array.length) % array.length;
        return array[i];
    }
    public Object removeLeft(){
        if(isEmpty())
            return null;
        number--;
        return array[end = (++end % array.length)];
    }
    public Object peek(int n){
        if(n >= number)
            return null;
        return array[(end + 1 + n) % array.length];
 
}
}
 then i have the PArrayList in another file but i got some errors its telling me that insertRight left isEmpty methods are not defined.
 
public class Deque {
	protected Object[] array;
    protected int start,end,number;
    public Deque(int maxsize){
        array = new Object[maxsize];
        start = end = number = 0;
    }
    public boolean isEmpty(){
        return number == 0;
    }
    public boolean isFull(){
        return number >= array.length;
    }
    public int size(){
        return number;
    }
    public void insertRight(Object o){
        if(number < array.length){
            array[start = (++start % array.length)] = o;
            number++;
        }
    }
    public void insertLeft(Object o){
        if(number < array.length){
            array[end] = o;
            end = (--end + array.length) % array.length;
            number++;
        }
    }
    public Object removeRight(){
        if(isEmpty())
            return null;
        number--;
        int i = start;
        start = (--start + array.length) % array.length;
        return array[i];
    }
    public Object removeLeft(){
        if(isEmpty())
            return null;
        number--;
        return array[end = (++end % array.length)];
    }
    public Object peek(int n){
        if(n >= number)
            return null;
        return array[(end + 1 + n) % array.length];
 
}
}
then 
 
public class CList {
	  class Node
      { int val; int next; }
    Node M[]; 
    int free, max = 10000;
    
    CList() 
      { 
        M = new Node[max+1]; 
        for (int i = 0; i < max; i++)
          { M[i] = new Node(); M[i].next = i+1; }
        M[max] = new Node(); M[max].next = 0;
        free = 0;
      }
            
    Node next(Node x)
      { return M[x.next]; }
    int val(Node x)
      { return x.val; }
            
    Node insert(Node x, int v) 
      { 
        int i = free; free = M[free].next;
        M[i].val = v; 
        if (x == null) M[i].next = i;
        else { M[i].next = x.next;  x.next = i; }
        return M[i];
      }
            
    void delete(Node x) 
      { int i = x.next; x.next = M[i].next; 
        M[i].next = free; free = i;
}
}
then 
 
public class Dolt {
	public static void main(String[] args)
    { 
              
    int N = Integer.parseInt(args[0]);
    int M = Integer.parseInt(args[1]);
                
    CList L = new CList();
           
      CList.Node x = null;
      for (int i = 1; i <= N; i++)
        x = L.insert(x, i); 
      while (x != L.next(x))
        {
          for (int i = 1; i < M; i++) 
            x = L.next(x);
          L.delete(x);
        }
      System.out.println("What's Left is " + L.val(x));
    }
}
 
 
 
 

Open in new window

0
 
zzynxSoftware engineerCommented:
>> the following is what i got but i am facing some problems when running it.
Maybe you can tell us what problems?
The more information you provide the better/quicker we can help you.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
elrayissAuthor Commented:
ok this is the error message Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
      at Dolt.main(Dolt.java:6)
 but there are no errors in the dolt file the only problems is in the PArrayListTest where its telling me that the insertRight isFull and size....are undefined.
0
 
zzynxSoftware engineerCommented:
>> but there are no errors in the dolt file
Sure there are!

That error is referencing to this line in Dolt:

>> int N = Integer.parseInt(args[0]);
In this line, you (try to) take the first parameter (and parse it as an int) while no parameters are available.
0
 
elrayissAuthor Commented:
ok where should i add that piece of code and will it fix all the errors.
0
 
elrayissAuthor Commented:
good
0
 
zzynxSoftware engineerCommented:
>> where should i add that piece of code
It replaces these two lines:

    int N = Integer.parseInt(args[0]);
    int M = Integer.parseInt(args[1]);

>> will it fix all the errors
It will fix the error you told me.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.