11-19-2013

posted Nov 19, 2013, 8:50 AM by Samuel Konstantinovich   [ updated Dec 13, 2013, 10:04 AM ]
Goal: Make a myStringList class, by converting a MyList.

You should have written the MyList class on your own. If you haven't you should go back and try to do it. However, I would like everyone to continue to the next step, so I am providing code for you. 


0. Test my tester (attached at the end of the post) with your MyList.java. 

1. There is 1 method missing: find. You must implement find in MyList.java first before proceeding.

2. Convert it MyList to work with Strings instead of int, this new class should be called MyStringList.

public class MyList{
    private int[] data;
    private int size;

    public int size(){
return size;
    }

    public MyList(){
size = 0;
data = new int[8];
    }

    private void grow(){
int[] newData = new int[size*2];
for(int i = 0; i < size ;i++){
   newData[i] = data[i];
}
data = newData;
    }
    
    private void shrink(){
int[] newData = new int[data.length/2];
for(int i = 0; i < size ;i++){
   newData[i] = data[i];
}
data = newData;
    }

    public void add(int value){
if(size == data.length){
   grow();
}
data[size] = value;
size++;
    }

    public void add(int index, int value){
if(index<0){
   //when the index is negative print an error and do nothing else
   System.out.println("Error index out of range:"+index);
   return;
}
if(size == data.length){
   grow();
}
if(index >= size){
   data[size] = value;
}else{
   for(int i = size; i > index; i--){
data[i] = data[i-1];
   }
   data[index] = value;
}
size++;
    }

    //return the index of the first occurrence of the value in the list. (lowest index)
    //when the value is not found, return -1;
    public int find(int value){
      /*NEEDS TO BE IMPLEMENTED*/
      return -1;
    }

    public int get(int index){
if(index >= 0 && index < size){
   return data[index];
}else{
   //when the index is out of range, print an error, and return maximum negative integer
   System.out.println("Error index out of range:"+index);
   return Integer.MIN_VALUE;
}
    }


    public int set(int index, int value){
if(index >= 0 && index < size){
   int old = data[index];
   data[index] = value;
   return old;
}else{
   //when the index is out of range, print an error, and return maximum negative integer
   System.out.println("Error index out of range:"+index);
   return Integer.MIN_VALUE;
}
    }



    public int remove(int index){
if(size <= data.length / 4){
   shrink();
}
if(index < 0 || index >= size){
   //when the index is out of range, print an error, and return maximum negative integer
   System.out.println("Error index out of range:" + index);
   return Integer.MIN_VALUE;
}else{
   int result = data[index];
   for(int i = index; i < size - 1; i++){
data[i] = data[i + 1];
   }
   size--;
   return result;
}
    }

    public String toString(){
String a = "[";
if(size == 1){
   return a + data[0] + "]";
}else{
   for(int i = 0; i < size - 1; i++){
a += data[i] + ", ";
   }
   return a + data[size - 1] + "]";
}       
    }
}

ċ
ListTesterKonstantTorment.java
(2k)
Samuel Konstantinovich,
Nov 19, 2013, 11:54 AM
Comments