Advertisement
NetBender

Esercizio nr.12 Seconda Prova

Feb 20th, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. /////Testo Esercizio: http://imageshack.us/a/img829/3808/19022013.jpg
  2. import java.util.Random;
  3. abstract class A
  4. {
  5.     private float[] x;
  6.     public A(float a)
  7.     {
  8.         x=new float[10];
  9.         x[0]=a;
  10.         for(int i=1; i<x.length; i++)
  11.         {
  12.             x[i]=(float)(Math.sin(100*x[i-1]));
  13.         }
  14.     }
  15.     public abstract float f();
  16.     public float[] getX()
  17.     {
  18.         return x;
  19.     }
  20.     public String toString()
  21.     {
  22.         String str="";
  23.         for(int i=0; i<x.length; i++)
  24.         {
  25.             str+=""+x[i]+" ";
  26.         }
  27.         return str;
  28.     }
  29. }
  30. class B extends A
  31. {
  32.     public B(float a)
  33.     {
  34.         super(a);
  35.     }
  36.     public float f()
  37.     {
  38.         float max=getX()[0];
  39.         for(int i=1; i<getX().length; i++)
  40.         {
  41.             if(getX()[i]>max)
  42.             {
  43.                 max=getX()[i];
  44.             }
  45.         }
  46.         return max;
  47.     }
  48.     public String toString()
  49.     {
  50.         return "class B"+"[ "+super.toString()+"] f()="+f();
  51.     }
  52. }
  53. class C extends A
  54. {
  55.     public C(float a )
  56.     {
  57.         super(a);
  58.     }
  59.     public float f()
  60.     {
  61.         float somma=0;
  62.         for(int i=0;i<getX().length;i++)
  63.         {
  64.             somma+=getX()[i];
  65.         }
  66.         return (float)(somma/getX().length);
  67.     }
  68.     public int gte(float thr)
  69.     {
  70.         int contatore=0;
  71.         for(int i=0;i<getX().length;i++)
  72.         {
  73.             if(getX()[i]>thr)
  74.             {
  75.                 contatore++;
  76.             }
  77.         }
  78.         return contatore;
  79.     }
  80.     public String toString()
  81.     {
  82.         return "class C"+"[ "+super.toString()+"]  f()="+f()+" gte(.7)="+gte((float)0.7);
  83.     }
  84. }
  85. class D
  86. {
  87.     private A a;
  88.     private B b;
  89.     private C c;
  90.     public D(float seed)
  91.     {
  92.         if(seed>0.5)
  93.         {
  94.             a=new B(seed);
  95.         }else
  96.         {
  97.             a=new C(seed);
  98.         }
  99.         b=new B(seed/2);
  100.         c=new C(seed/3);
  101.     }
  102.     public float m()
  103.     {
  104.         float sigma=0;
  105.         A p;
  106.         if(a instanceof C)
  107.         {
  108.             p=c;
  109.         }else
  110.         {
  111.             p=b;
  112.         }
  113.         for(int i=0; i<(p.getX()).length; i=i+2)
  114.         {
  115.             sigma+=Math.max(a.getX()[i],p.getX()[i]);
  116.         }
  117.         return sigma;
  118.     }
  119.     public float h(float thr)
  120.     {
  121.         if(a instanceof C)
  122.         {
  123.             return (float)((c.f()/b.f())*((C)a).gte(thr));
  124.         }else
  125.         {
  126.             return (float)(c.f()/b.f());
  127.         }
  128.     }
  129.     public String toString()
  130.     {
  131.         return a.toString()+"\n"+"    "+b.toString()+"\n"+"    "+c.toString()+" "+"h(.7)="+h((float)0.7)+" m()="+m();
  132.     }
  133. }
  134. class Esercizio_12
  135. {
  136.     public static void main(String[] args)
  137.     {
  138.         Random r=new Random(347537586);
  139.         D[] vett=new D[50];
  140.         for(int i=0;i<vett.length; i++)
  141.         {
  142.             vett[i]=new D(r.nextFloat());
  143.         }
  144.         //Parte 0
  145.         for(int i=0;i<vett.length; i++)
  146.         {
  147.             System.out.println(i+"    "+vett[i].toString());
  148.         }
  149.         //Parte 1
  150.         float sommaH=0;
  151.         for(int i=0;i<vett.length;i++)
  152.         {
  153.             sommaH+=vett[i].h((float)0.7);
  154.         }
  155.         System.out.print("Media= "+sommaH/vett.length);
  156.         //Parte 2
  157.         System.out.println("    vett[4].m()+vett[5].m()= "+(vett[4].m()+vett[5].m())); 
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement