Advertisement
Guest User

Untitled

a guest
Feb 6th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. public class MyGenericStack<E> {
  2.     //char[] array=new char[10];
  3.     E[] array = (E[])new Object[10];
  4.     int size=0;
  5.     boolean empty=true;
  6.    
  7.     public void push(E element) {
  8.         if(size==array.length-1) {
  9.             //System.out.println("The array is full");
  10.             E[] newArray=(E[])new Object[array.length+5];
  11.            
  12.             for (int i=0; i<array.length;i++) {
  13.                 newArray[i]=array[i];
  14.             }
  15.             array=newArray;    
  16.         }
  17.             array[size]=element;
  18.             empty=false;
  19.             size++;
  20.        
  21.     }
  22.  
  23.    
  24.     public void pop(){
  25.         if(empty==false) {
  26.             array[size]=null;
  27.             size--;
  28.         }else {
  29.             System.out.println("Can't pop an empty stack");
  30.         }
  31.        
  32.         if(size==0) {
  33.             empty=true;
  34.         }
  35.     }
  36.    
  37.     public E peek() {
  38.         E element=array[size-1];
  39.         return element;
  40.     }
  41.    
  42.     public int size() {
  43.        
  44.         return size;
  45.     }
  46.  
  47.     public boolean isEmpty() {
  48.         return empty;
  49.     }
  50.    
  51. }
  52. public class Book {
  53.  
  54.         String title;
  55.         String author;
  56.         String category;
  57.    
  58.        
  59.         public Book(String title, String author, String category) {
  60.            
  61.         }
  62.  
  63.         public String getCategory() {
  64.             return category;
  65.         }
  66.  
  67.         public String toString() {
  68.             return title + " by " + author +", " + "category:" + category;
  69.         }
  70.    
  71.    
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement