Advertisement
Guest User

ArrayList without CollectionsV2

a guest
Feb 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.04 KB | None | 0 0
  1.  
  2. public interface ArrayListInterface <E> {
  3.     // There are 2 versions of add for arrayList
  4.     public void add (E a);      // This method adds at the end of list
  5.     public void add (int index, E a);   // This is overloaded method
  6.                                 // adds at location index
  7.    
  8.     // Remove method
  9.     public E remove (int index);    // remove from index
  10.    
  11.     // get and set
  12.     public E get (int index);       // Returns the object at index
  13.     public void set(int index, E a); // Update the value at index
  14.    
  15.     public int getSize();       // Returns the number of elements in list
  16.     public int indexOf(E a);    // Returns the index of the object
  17.    
  18. }
  19.  
  20. public class ArrayList <E> implements ArrayListInterface<E> {
  21.  
  22.     private int size;   // How many elements are there in the AL
  23.     private int capacity;   // How big is the AL
  24.     private E[] myArray;    // This is the reference to actual data
  25.    
  26.     // Constructors
  27.    
  28.     // Default constructor, creates an array list of size 2
  29.     public ArrayList() {
  30.         this.capacity = 20;
  31.         this.size = 0;      // Initially there is no data
  32.         myArray = (E[]) new Object[this.capacity]; // Create the
  33.         // array with default size
  34.     }
  35.    
  36.     // Overloaded constructor; creates an AL of user defined size
  37.     public ArrayList(int capacity) {
  38.         this.capacity = capacity;
  39.         this.size = 0;      // Initially there is no data
  40.         myArray = (E[]) new Object[this.capacity]; // Create the
  41.         // array with default size
  42.     }
  43.    
  44.     @Override
  45.     public void add(E a) {
  46.         // This method adds an element to the end of the AL
  47.         if(size < capacity) {
  48.             // This means there is space at the end of the AL
  49.             myArray[size] = a;  // size gives the index of first
  50.                                 // empty location
  51.             size++;     // Update the total number of elements in AL
  52.         }
  53.         else {
  54.             // We do not have space to add the element
  55.             System.out.println(" Not enough space! Call reallocate");
  56.             this.reallocate();  // Call reallocate
  57.             this.add(a);    // Once we have bigger AL, then add data
  58.         }
  59.        
  60.     }
  61.  
  62.     private void reallocate() {
  63.         // This method doubles the capacity of the AL
  64.         this.capacity *= 2;
  65.         E[] temp = (E[]) new Object[this.capacity];
  66.         // Now, we have an empty array double the size
  67.         // Copy over the elements from original array to temp
  68.         for(int i = 0; i < myArray.length;i++) {
  69.             temp[i] = myArray[i];
  70.         }
  71.         // Now update the reference to reflect the change
  72.         this.myArray = temp;
  73.        
  74.     }
  75.  
  76.     @Override
  77.     public void add(int index, E a) {
  78.         // This method inserts the data at given index
  79.         // First, check if the index is valid or not
  80.         if(index < 0 || index > size) {
  81.             System.out.println("Invalid index!");
  82.             return;
  83.         }
  84.         else if(index == size) {
  85.             // This indicates that the element needs to be added
  86.             // at the end of the AL
  87.             // Now, we have a method for that!
  88.             this.add(a); // Call the add() method to handle
  89.         }
  90.         else {
  91.             // We will insert the data by shifting elements
  92.             // Is there enough space to shift?
  93.             if(this.capacity == this.size) {
  94.                 // AL is already full
  95.                 System.out.println("Not enough space! Call reallocate");
  96.                 this.reallocate();
  97.             }
  98.             // We have space to shift elements
  99.             for(int i = size; i > index; i--) {
  100.                 this.myArray[i] = this.myArray[i -1];
  101.             }
  102.             // Don't forget to insert
  103.             this.myArray[index] = a;
  104.             // Update the total number of elements
  105.             this.size++;
  106.         }
  107.        
  108.     }
  109.  
  110.     @Override
  111.     public E remove(int index) {
  112.         // This method deletes an element from the given index
  113.         // Make sure that the index is valid
  114.         if(index < 0 || index >= size) {
  115.             System.out.println("Invalid index!");
  116.             return null;
  117.         }
  118.         // We will save the element to be deleted in a temp
  119.         E temp = myArray[index];
  120.         // Then we need to shift elements to the left
  121.         for(int i = index; i < size -1 ; i++) {
  122.             this.myArray[i] = this.myArray[i+1];
  123.         }
  124.         // Don't forget to update size
  125.         size--;    
  126.         return temp;
  127.     }
  128.  
  129.     @Override
  130.     public E get(int index) {
  131.         if(index < 0 || index >= size) {
  132.             System.out.println("Invalid index!");
  133.             return null;
  134.         }
  135.         return myArray[index];
  136.     }
  137.  
  138.     @Override
  139.     public void set(int index, E a) {
  140.         if(index < 0 || index >= size) {
  141.             System.out.println("Invalid index!");
  142.             return;
  143.         }
  144.         myArray[index] = a;
  145.        
  146.     }
  147.  
  148.     @Override
  149.     public int getSize() {
  150.         return this.size;
  151.     }
  152.  
  153.     @Override
  154.     public int indexOf(E a) {
  155.         // TODO Auto-generated method stub
  156.         return 0;
  157.     }
  158.    
  159.     // Method to print the contents of the AL
  160.     // We will override the toString method
  161.     @Override
  162.     public String toString() {
  163.         String s = "";
  164.         for(int i = 0; i < size; i++) {
  165.             s = s + myArray[i];
  166.         }
  167.        
  168.         return s;
  169.     }
  170.  
  171. }
  172.  
  173. import java.util.Scanner;
  174. public class driver {
  175.  
  176.     public static void main(String[] args) {
  177.         // Here, we create object of AL, and test the methods
  178.         ArrayList<LibraryBooks> myList = new ArrayList<>();
  179.         Scanner myObj = new Scanner(System.in);
  180.         int option = 0;
  181.        
  182.         System.out.println("Select a number option: ");
  183.         System.out.println("1. List all books");
  184.         System.out.println("2. Display all books sorted by publishing year");
  185.         System.out.println("3. Sort the books according to length in pages");
  186.         System.out.println("4. Sort the books according to review ratings");
  187.         System.out.println("5. Ask user for subject and display all books belonging to that specific subject");
  188.         System.out.println("6. Search for a specific book name and display all info about said book");
  189.         System.out.println("7. Add a book to the list of books");
  190.         System.out.println("8. Exit");
  191.         option = myObj.nextInt();
  192.         while(option != 8) {
  193.             if(option == 1) {
  194.                 System.out.println(myList.toString());
  195.             }
  196.             if(option == 2) {
  197.                
  198.             }
  199.             if(option == 4) {
  200.                
  201.             }
  202.             if(option == 5) {
  203.                
  204.             }
  205.             if(option == 7) {
  206.                 System.out.println("Please provide details of the new book...");
  207.                 LibraryBooks newBook = getDetailsNewBook();
  208.                 myList.add(newBook);
  209.             }
  210.             System.out.println("Select an option: ");
  211.             option = myObj.nextInt();
  212.         }
  213.        
  214.         myObj.close();
  215.     }
  216.  
  217.     private static LibraryBooks getDetailsNewBook() {
  218.         // TODO Auto-generated method stub
  219.         Scanner input = new Scanner(System.in);
  220.         System.out.println("Enter Title of new Book: ");
  221.         String title = input.nextLine();
  222.         System.out.println("Enter Subject of new book: ");
  223.         String uSub = input.nextLine();
  224.         System.out.println("Enter the year for book: ");
  225.         int uYear = input.nextInt();
  226.         System.out.println("Enter the amount of pages: ");
  227.         int uPage = input.nextInt();
  228.         System.out.println("Enter the review rating: ");
  229.         double uRate = input.nextDouble();
  230.         LibraryBooks lb1 = new LibraryBooks(title, uSub, uYear, uPage, uRate);
  231.         return lb1;
  232.     }
  233.    
  234. }
  235.  
  236. import java.util.Scanner;
  237. import java.util.Comparator;
  238.  
  239. public class LibraryBooks  {
  240.  
  241.     private String title;
  242.     private String subject;
  243.     private int year;
  244.     private int page;
  245.     private double rating;
  246.     private int titleVal = 21;
  247.  
  248.     //Default Constructor
  249.     public LibraryBooks () {
  250.         this.title = null;
  251.         this.subject = null;
  252.         this.year = 0;
  253.         this.page = 0;
  254.         this.rating = 0.0;
  255.     }
  256.     //User Inputed Constructor
  257.     public LibraryBooks(String uTitle, String uSub, int uYear, int uPage, double uRate) {
  258.         this.title = uTitle;
  259.         this.subject = uSub;
  260.         this.year = uYear;
  261.         this.page = uPage;
  262.         this.rating = uRate;
  263.     }
  264.    
  265.     public void setTitle(String newTitle) {
  266.         this.title = newTitle;
  267.     }
  268.  
  269.     public void setSubject(String newSubject) {
  270.         this.subject = newSubject;
  271.     }
  272.  
  273.     public void setYear(int newYear) {
  274.         this.year = newYear;
  275.     }
  276.  
  277.     public void setPage(int newPage) {
  278.         this.page = newPage;
  279.     }
  280.    
  281.     public void setRating(double newRate) {
  282.         this.rating = newRate;
  283.     }
  284.  
  285.     public String getTitle() {
  286.         return title;
  287.     }
  288.  
  289.     public String getSubject() {
  290.         return subject;
  291.     }
  292.  
  293.     public int getYear() {
  294.         return year;
  295.     }
  296.  
  297.     public int getPage() {
  298.         return page;
  299.     }
  300.    
  301.     public double getRating() {
  302.         return rating;
  303.     }
  304.    
  305.     @Override
  306.     public String toString() {
  307.         return "Title: "+title+ "\nSubject: " + subject + "\nYear Published: " + year + "\nNumber of Pages: " + page + "\nReview Rating: " + rating +"\n\n";
  308.     }
  309.    
  310.     public static Comparator<LibraryBooks> LibraryPublish = new Comparator<LibraryBooks>() {
  311.         public int compare(LibraryBooks b1, LibraryBooks b2)   {
  312.             int publish1 = b1.getYear();
  313.             int publish2 = b2.getYear();
  314.            
  315.             return publish1 - publish2;
  316.         }
  317.     };
  318.    
  319.     public static Comparator<LibraryBooks> LibraryPages = new Comparator<LibraryBooks>() {
  320.         public int compare(LibraryBooks b1, LibraryBooks b2) {
  321.             int Page1 = b1.getPage();
  322.             int Page2 = b2.getPage();
  323.            
  324.             return Page1 - Page2;
  325.         }
  326.     };
  327.    
  328.     public static Comparator<LibraryBooks> LibraryRating = new Comparator<LibraryBooks>() {
  329.         public int compare(LibraryBooks b1, LibraryBooks b2) {
  330.             if (b1.getRating() < b2.getRating()) return -1;
  331.             if (b1.getRating() > b2.getRating()) return 1;
  332.             return 0;
  333.         }
  334.     };
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement