?
Solved

putting a program together

Posted on 2009-02-23
8
Medium Priority
?
197 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:elrayiss
  • 4
  • 4
8 Comments
 

Author Comment

by:elrayiss
ID: 23716187
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
 
LVL 37

Expert Comment

by:zzynx
ID: 23719903
>> 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
 

Author Comment

by:elrayiss
ID: 23722939
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
Industry Leaders: 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 37

Expert Comment

by:zzynx
ID: 23722990
>> 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
 
LVL 37

Accepted Solution

by:
zzynx earned 2000 total points
ID: 23723036
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
 

Author Comment

by:elrayiss
ID: 23725466
ok where should i add that piece of code and will it fix all the errors.
0
 

Author Closing Comment

by:elrayiss
ID: 31550328
good
0
 
LVL 37

Expert Comment

by:zzynx
ID: 23731087
>> 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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 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