CSE-8B / PA5 / Starter / SortedList.java
SortedList.java
Raw
//File: SortedList.java
//Name: Trai Pham
//Date: 02/09/2020
//Course: CSE 8B

/*Extends OrderedDS implements all methods in OrderedDS and DataStructure
classes, and BasicMethods interface

*/
//this class sorts a set of elements from lowest to greatest value
public class SortedList extends OrderedDS{
  private int maxSize;
  private int indexAt;
  private static final String EMPTY_ERROR = "SortedList is empty";
  private static final String MAX_SIZE_ERROR =
  "SortedList maximum limit reached";

//default constructor
  public SortedList(){
    this.maxSize = 0;
    this.indexAt = 0;
    this.array = null;
  }

//initializing maxSize of array
  public SortedList(int maxSize){
    if(maxSize < 0){
      this.maxSize = 0;
      this.array = null;
    }
    else{
      this.maxSize = maxSize;
      this.array = new int [this.maxSize];
    }
  }

//creates a deep copy of the SortedList object
  public SortedList(SortedList s){
    if( s == null){
      this.maxSize = 0;
      this.array = null;
    }
    else {
      this.maxSize = s.maxSize;
      for(int i = 0; i < this.maxSize; i++){
        this.array[i] = s.array[i];
      }
    }
  }


  public void add(int element){
//checks if SortedList is full or SortedList is null
    if(this.array == null || this.indexAt == this.array.length || this.maxSize ==0){
      System.out.println(MAX_SIZE_ERROR);
      return;
    }

    else{
//this would start off the array
      if(this.indexAt == 0){
        this.array[this.indexAt] = element;
      }
//int var would save the position at index i when element is greater than
//a number at position i
      int var = 0;
      for(int i = this.indexAt; i > 0; i--){
        if(element > this.array[i-1]){
          var = i;
          break;
        }
      }
//Shifting the element after the added element to the right
      if(this.indexAt < this.maxSize){
        for(int i = this.indexAt; i > var; i--){
          this.array[i] = this.array[i-1];
        }
      }
//every element before the element would be in it's original spot without being
//shifted

//this code would place the greatest element in the last index
        if(this.indexAt == this.maxSize){
          for(int i = indexAt-1; i > var; i--){
            this.array[i] = this.array[i-1];
          }
        }
      this.array[var] = element;
      this.indexAt++;
    }
  }

  @Override
    public int delete(){
  //empty error check
      if(this.indexAt == 0|| this.array == null){
        System.out.println(EMPTY_ERROR);
        return -1;
      }
      else{
        int reserved = this.array[0];
        for( int i = 1; i < this.indexAt; i++){
            this.array[i-1] = this.array[i];
        }
        this.indexAt--;
        return reserved;
        }
    }

  @Override
    public int peek(){
//when SortedList is empty
      if(this.indexAt == 0 || this.array == null){
        System.out.println(EMPTY_ERROR);
        return -1;
      }
      else{
//returns first element inserted in list
        return this.array[0];
      }
    }

  public int size(){
      return this.indexAt;
    }
  public String toString(){
    String output = "";
    if(this.indexAt == this.array.length + 1 || this.array == null){
      return "";
    }

    StringBuilder a = new StringBuilder();
    output = this.array[0] + "";
    a.append(output);
    for(int i = 1; i < this.indexAt; i++){
      a.append("-" + this.array[i]);
    }
    return a.toString();
  }

  public static void main(String[] args){
    SortedList a = new SortedList(5);
    // System.out.println(a.array.length); //gives you length of the array
    a.add(16);
    System.out.println(a);
    a.add(5);
    System.out.println(a);
    a.add(10);
    System.out.println(a);
    a.add(25);
    System.out.println(a);
    a.add(9);
    System.out.println(a);
    System.out.println("Testing add method");
    a.delete();
    System.out.println(a);
    a.delete();
    System.out.println(a);
    a.delete();
    System.out.println(a);
    System.out.println("Testing deleting method");
    System.out.println(a.peek());
    System.out.println("Returning the top number");
    // System.out.println("Testing delete method");
    System.out.println(a.size());
    System.out.println("Counting number of elements");
  }
}