2 Replies - 257 Views - Last Post: 02 October 2012 - 05:09 PM Rate Topic: -----

#1 ali11  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 50
  • Joined: 25-October 11

not sure why getting error

Posted 02 October 2012 - 04:45 PM

This one has no errors
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

class DynamicArrayOfInts {

    private int[] storage;
    private int size;
    private final  int INITIAL_CAPACITY = 8;
    private final int GROW_FACTOR = 2;

    public DynamicArrayOfInts() {
      storage = new int[INITIAL_CAPACITY];
      size = 0;
    }

    private void rangeCheck(int index){
if (index < 0 || index >= size)

                  if(index < 0 || index >= size)
      throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);

    }

    private void ensureCapacity(int size_wanted) {
      int max_capacity = storage.length;
      if (size_wanted > max_capacity) {
        max_capacity = max_capacity * GROW_FACTOR +1;
        storage = Arrays.copyOf(storage, max_capacity); // increases array size + copy contents
      }
    }

    public int size() {
      return size;

    }

     public boolean equals(Object aThat) { // aThat is a DynamicArrayOfInts object
if (aThat==this) {
    return true;
} else {
       return false;  // added so code would compile
    }
}
    public boolean equals(List<Integer> list)  { // list is a LinkedList, or ArrayList, etc

       return false;  // added so code would compile
    }

    public int get(int position){
rangeCheck(position);
return storage [position];

    }

    public void set(int index, int value){
        rangeCheck(index);

                    storage[index] = value;


    }

    public void insertAt(int index, int value) {
        if (index < 0 || index > size)

                          throw new IndexOutOfBoundsException("Index: " + index + ", Size: "

                                     + size);

                    ensureCapacity(size + 1);

                    int moveCount = size - index;

                    if (moveCount > 0)

                          System.arraycopy(storage, index, storage, index + 1, moveCount);

                    storage[index] = value;

                    size++;


    }

    public void add(int value) {
if (size== storage.length)
{
    ensureCapacity ((size+1)*2);
    }
    storage[size]= value;
    size++;
}
    public void removeAt(int index) {
       rangeCheck(index);

                   int moveCount = size - index - 1;

                   if (moveCount > 0)

                         System.arraycopy(storage, index + 1, storage, index, moveCount);

                   size--;


    }

    public void printAll() {
for (int i= 0; i < size; i++) {
             System.out.printf("  [%d]=%d", i, get(i));
        }

      System.out.println();
   }





    public static void main(String args[]) {
       DynamicArrayOfInts list1 = new DynamicArrayOfInts();
       list1.insertAt(0,1);
       list1.insertAt(1,2);
       list1.add(3);
       // list1 is 1, 2, 3
       System.out.print("list1: "); list1.printAll();
       list1.set(2,100);
       // list1 1 is 1, 2, 100
       //System.out.print("list1: "); list1.printAll();
       System.out.println("list1[2]=" + list1.get(2));
       list1.removeAt(2);
       // list1 is 1, 2
       System.out.print("list1: "); list1.printAll();
       DynamicArrayOfInts list2 = new DynamicArrayOfInts();
       list2.insertAt(0,2);
       list2.insertAt(0,3);
       list2.insertAt(0,1);
       list2.removeAt(1);
       // list2 is 1, 2
       System.out.print("list2: ");list2.printAll();
       System.out.println("list1.size()=" + list1.size() + ", list2.size()=" + list2.size());
       // list1 and list2 are equal
       System.out.println("list1 equals list2 is " + list1.equals(list2));
       list2.insertAt(2,3);
       // list2 is 1, 2, 3
      System.out.println("list1.size()=" + list1.size() + ", list2.size()=" + list2.size());
       // list1 and list2 are not equal
       System.out.println("list1 equals list2 is " + list1.equals(list2));
         ArrayList list3 = new ArrayList();
         list3.add(1);list3.add(2);list3.add(3);
         System.out.println("list2 equals list3 is " + list2.equals(list3));
    }
}


2nd file this one is giving me error


import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
import java.util.Vector;

public class PA1 {

        static class ExecutionResults {
                int size;
                long linkedListTime;
                long arrayListTime;
                long dynamicArrayTime;

                public ExecutionResults(int size) {
                        this.size = size;
                }
        }

        public static void main(String[] args) throws Exception {

                int choice = 4;
                if (args.length >0)
                         choice = Integer.parseInt(args[0]);

                     // warm up the JVM
                for (int i = 0; i < 100; i++)
                   CheckPerformance(1000,choice);

                // print header
                if (choice == 4)
                    System.out.printf("%-15s %-20s %-20s %-20s %n",
                      "size",  "LinkedList", "ArrayList",      "Dynamic Array");

                // perform the benchmarking
                displayResult(CheckPerformance(100,choice),choice);
                displayResult(CheckPerformance(200,choice),choice);
                displayResult(CheckPerformance(500,choice),choice);
                displayResult(CheckPerformance(1000,choice),choice);
                displayResult(CheckPerformance(2000,choice),choice);
                displayResult(CheckPerformance(3000,choice),choice);
                displayResult(CheckPerformance(4000,choice),choice);
                displayResult(CheckPerformance(8000,choice),choice);
                displayResult(CheckPerformance(16000,choice),choice);
        }

        private static void displayResult(ExecutionResults r, int choice) {

                long min = 1;
                if (choice == 4) {
                    min = Math.min(r.linkedListTime, Math.min(r.arrayListTime, r.dynamicArrayTime));

                    System.out.printf("%-15d %-10d (%-3.0f%%)    %-10d (%-3.0f%%)     %-10d (%-3.0f%%)%n",
                                r.size,
                                r.linkedListTime,   (100 * (double) r.linkedListTime) / min,
                                r.arrayListTime,    (100 * (double) r.arrayListTime) / min,
                                r.dynamicArrayTime, (100 * (double) r.dynamicArrayTime) / min);
                }
        }

        private static ExecutionResults CheckPerformance(int size, int choice) {

                Integer[] array = new Integer[size];
                Random random = new Random(123456789L);

                LinkedList<Integer> linkedList = new LinkedList<Integer>(Arrays.asList(-1));
                ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(-1));
                DynamicIntArray dynamicArray = new DynamicIntArray();
                dynamicArray.add(-1);

                for (int i = 0; i < array.length; i++)
                        array[i] = random.nextInt(Integer.MAX_VALUE);

                ExecutionResults result = new ExecutionResults(size);

                     switch (choice) {
                case 1:
                    long before = System.nanoTime();
                 insertIntoLinkedList(array, linkedList);
                  result.linkedListTime = (System.nanoTime() - before) / 1000;
                  break;
                     case 2:
                    before = System.nanoTime();
                 insertIntoArrayList(array, arrayList);
                  result.arrayListTime = (System.nanoTime() - before) / 1000;
                       break;
                     case 3:
                    before = System.nanoTime();
                 insertIntoDynamicArray(array, dynamicArray);
                  result.dynamicArrayTime = (System.nanoTime() - before) / 1000;
                  break;
                 case 4:
                    before = System.nanoTime();
                 insertIntoLinkedList(array, linkedList);
                  result.linkedListTime = (System.nanoTime() - before) / 1000;

                    before = System.nanoTime();
                 insertIntoArrayList(array, arrayList);
                  result.arrayListTime = (System.nanoTime() - before) / 1000;

                    before = System.nanoTime();
                 insertIntoDynamicArray(array, dynamicArray);
                  result.dynamicArrayTime = (System.nanoTime() - before) / 1000;
                 }

                // check that they are equal
                if (choice == 4 && !(linkedList.equals(arrayList) && dynamicArray.equals(arrayList)))
                        throw new RuntimeException("Lists not equal...");

                return result;
        }


        private static void insertIntoLinkedList(Integer[] intArray, LinkedList<Integer> list) {
              for (Integer integer : intArray) {
                                    for (ListIterator<Integer> it = list.listIterator(); it.hasNext();)/> {
                                            if (integer.compareTo(it.next()) >= 0) {
                                                    it.previous(); // should be added before element
                                                    it.add(integer);
                                                    break;
                                            }
                                    }
                            }

        }

        private static void insertIntoArrayList(Integer[] intArray, ArrayList<Integer> list) {
            for (Integer integer : intArray) {
                                    int list_size = list.size();  // on purpose: it is smarter to avoid calling size() every loop
                                    for (int i = 0; i < list_size; i++) {
                                            if (integer.compareTo(list.get(i)) >= 0) {
                                                    list.add(i, integer);
                                        break;

        }
    }
}
}

         private static void insertIntoDynamicArray(Integer[] numbers, DynamicIntArray array) {
  for(Integer integer : numbers){
        int value = integer.intValue();
         //        int array_size = array.size();           // on purpose: even faster would be to avoid calling size() every loop
        for(int idx = 0; idx < array.size(); idx++){
          if(value >= array.get(idx)){
            array.insertAt(idx, value);
            break;
          }
        }
      }
    }
          }

i am getting following error
C:\Users\papa\Desktop\PA1.java:144: cannot find symbol
symbol : class DynamicIntArray
location: class PA1
private static void insertIntoDynamicArray(Integer[] numbers, DynamicIntArray array) {
^
C:\Users\papa\Desktop\PA1.java:144: cannot find symbol
symbol : class DynamicIntArray
location: class PA1
private static void insertIntoDynamicArray(Integer[] numbers, DynamicIntArray array) {
^
C:\Users\papa\Desktop\PA1.java:72: cannot find symbol
symbol : class DynamicIntArray
location: class PA1
DynamicIntArray dynamicArray = new DynamicIntArray();
^
C:\Users\papa\Desktop\PA1.java:72: cannot find symbol
symbol : class DynamicIntArray
location: class PA1
DynamicIntArray dynamicArray = new DynamicIntArray();
^
3 errors

Tool completed with exit code 1

This post has been edited by ali11: 02 October 2012 - 04:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: not sure why getting error

#2 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: not sure why getting error

Posted 02 October 2012 - 04:55 PM

The compiler can't find your "DynamicIntArray" class, which is not weird since you called your class "DynamicArrayOfInts".
Was This Post Helpful? 0
  • +
  • -

#3 xriBit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-October 12

Re: not sure why getting error

Posted 02 October 2012 - 05:09 PM

View PostKakerergodt, on 02 October 2012 - 04:55 PM, said:

The compiler can't find your "DynamicIntArray" class, which is not weird since you called your class "DynamicArrayOfInts".


Yeah, that's what I found too. Be careful when you name your modules (class, objects, etc. )This is more than just a compiler error. Logical perhaps. And is hard to debug (when not realized).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1