Advertisement
Andrei_M

Additional_Ass_1.java

Oct 23rd, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. class Book
  2. {
  3.     public String autor, titlu;
  4.    
  5.     public Book(String autor, String titlu)
  6.     {
  7.         this.autor = autor;
  8.         this.titlu = titlu;
  9.     }
  10.     public boolean equals(Object o)
  11.     {
  12.         return (o instanceof Book) && ((Book)o).autor.equals(this.autor) && ((Book)o).titlu.equals(this.titlu);
  13.     }
  14. }
  15.  
  16. class Set
  17. {
  18.     private int max_elements, capacity = 0;
  19.     private Book vector[];
  20.    
  21.    
  22.     public Set(int x)
  23.     {
  24.         max_elements = x;
  25.         vector = new Book[max_elements];
  26.     }
  27.    
  28.     public boolean addBook(Book new_book)
  29.     {
  30.         if(capacity == max_elements)
  31.             return false;
  32.         else
  33.         {
  34.             if(capacity != 0)
  35.                 for(int i = 0; i < capacity; i++)
  36.                 {
  37.                     if(vector[i].equals(new_book))
  38.                         return false;
  39.                 }
  40.             vector[capacity] = new_book;
  41.             capacity++;
  42.         }
  43.         return true;
  44.     }
  45.    
  46.     public String toString()
  47.     {
  48.         String buffer = "";
  49.         for(int i = 0; i < capacity; i++)
  50.             buffer +=  (i+1) + ". " + vector[i].autor + " : " + vector[i].titlu + "\n";
  51.        
  52.         return buffer;
  53.     }
  54.    
  55.     public Set union(Set set_2)
  56.     {
  57.         int new_capacity = this.capacity + set_2.capacity;
  58.        
  59.         Set new_set = new Set(new_capacity);
  60.        
  61.         for(int i = 0 ; i < this.capacity; i++)
  62.         {
  63.             new_set.addBook(this.vector[i]);
  64.         }
  65.        
  66.         for(int j = 0; j < set_2.capacity; j++)
  67.         {
  68.             new_set.addBook(set_2.vector[j]);
  69.         }
  70.        
  71.         return new_set;
  72.     }
  73. }
  74.  
  75.  
  76.  
  77. public class Main
  78. {
  79.  
  80.    
  81.     public static void main(String[] args)
  82.     {
  83.         Book b1, b2, b3, b4;
  84.         b1 = new Book("autor1","carte1");
  85.         b2 = new Book("autor2","carte2");
  86.         b3 = new Book("autor3","carte3");
  87.         b4 = new Book("autor4","carte4");
  88.        
  89.        
  90.         Set s1, s2;
  91.         s1 = new Set(3);
  92.         s2 = new Set(2);
  93.        
  94.         s1.addBook(b1);
  95.         s1.addBook(b2);
  96.         s1.addBook(b3);
  97.        
  98.         s2.addBook(b1);
  99.         s2.addBook(b2);
  100.        
  101.        
  102.         System.out.print(s1);
  103.         System.out.print(s2);
  104.         System.out.println("");
  105.         System.out.println(s1.union(s2));
  106.        
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement