Advertisement
TS10315016

final_exam_04

May 29th, 2015
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. package final_exam_04;
  2. import java.util.*;
  3.  
  4. public class MyFruitList<E> extends ArrayList<E>
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         MyFruitList<Fruit> fruts = new MyFruitList<Fruit>();
  9.         fruts.add(new Fruit());
  10.         fruts.add(new Fruit());
  11.        
  12.         MyFruitList<Apple> apps  = new MyFruitList<Apple>();
  13.         apps.add(new Apple());
  14.         apps.add(new Apple());
  15.        
  16.         MyFruitList<GreenApple> gapps  = new MyFruitList<GreenApple>();
  17.         gapps.add(new GreenApple());
  18.         gapps.add(new GreenApple());
  19.        
  20.         System.out.println(fruts.count());
  21.         System.out.println(fruts);
  22.         copyAll(fruts,apps);
  23.         System.out.println(fruts.count());
  24.         System.out.println(fruts);
  25.         copyAll(fruts,gapps);
  26.         System.out.println(fruts.count());
  27.         System.out.println(fruts);
  28.     }
  29.    
  30.     public int count()
  31.     {
  32.         return this.size();
  33.     }
  34.    
  35.     public static <E,T> void copyAll(ArrayList<E> a , ArrayList<T> b)
  36.     {
  37.         ArrayList<Object> c = (ArrayList<Object>)a;
  38.         c.addAll(b);
  39.     }
  40. }
  41.  
  42. class Fruit extends Object
  43. {
  44.     private static int id;
  45.     private int myid;
  46.     Fruit()
  47.     {
  48.         myid = id;
  49.         id++;
  50.     }
  51.     public String toString()
  52.     {
  53.         return "Fruit"+myid;
  54.     }
  55. }
  56.  
  57.  
  58. class Apple extends Fruit
  59. {
  60.     private static int id;
  61.     private int myid;
  62.     Apple()
  63.     {
  64.         myid=id;
  65.         id++;
  66.     }
  67.     public String toString()
  68.     {
  69.         return "Apple"+myid;
  70.     }  
  71. }
  72.  
  73. class GreenApple extends Apple
  74. {
  75.     private static int id;
  76.     private int myid;
  77.    
  78.     GreenApple()
  79.     {
  80.         myid=id;
  81.         id++;
  82.     }
  83.     public String toString()
  84.     {
  85.         return "GreenApple"+myid;
  86.     }
  87.    
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement