Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////Testo Esercizio: http://imageshack.us/a/img829/3808/19022013.jpg
- import java.util.Random;
- abstract class A
- {
- private float[] x;
- public A(float a)
- {
- x=new float[10];
- x[0]=a;
- for(int i=1; i<x.length; i++)
- {
- x[i]=(float)(Math.sin(100*x[i-1]));
- }
- }
- public abstract float f();
- public float[] getX()
- {
- return x;
- }
- public String toString()
- {
- String str="";
- for(int i=0; i<x.length; i++)
- {
- str+=""+x[i]+" ";
- }
- return str;
- }
- }
- class B extends A
- {
- public B(float a)
- {
- super(a);
- }
- public float f()
- {
- float max=getX()[0];
- for(int i=1; i<getX().length; i++)
- {
- if(getX()[i]>max)
- {
- max=getX()[i];
- }
- }
- return max;
- }
- public String toString()
- {
- return "class B"+"[ "+super.toString()+"] f()="+f();
- }
- }
- class C extends A
- {
- public C(float a )
- {
- super(a);
- }
- public float f()
- {
- float somma=0;
- for(int i=0;i<getX().length;i++)
- {
- somma+=getX()[i];
- }
- return (float)(somma/getX().length);
- }
- public int gte(float thr)
- {
- int contatore=0;
- for(int i=0;i<getX().length;i++)
- {
- if(getX()[i]>thr)
- {
- contatore++;
- }
- }
- return contatore;
- }
- public String toString()
- {
- return "class C"+"[ "+super.toString()+"] f()="+f()+" gte(.7)="+gte((float)0.7);
- }
- }
- class D
- {
- private A a;
- private B b;
- private C c;
- public D(float seed)
- {
- if(seed>0.5)
- {
- a=new B(seed);
- }else
- {
- a=new C(seed);
- }
- b=new B(seed/2);
- c=new C(seed/3);
- }
- public float m()
- {
- float sigma=0;
- A p;
- if(a instanceof C)
- {
- p=c;
- }else
- {
- p=b;
- }
- for(int i=0; i<(p.getX()).length; i=i+2)
- {
- sigma+=Math.max(a.getX()[i],p.getX()[i]);
- }
- return sigma;
- }
- public float h(float thr)
- {
- if(a instanceof C)
- {
- return (float)((c.f()/b.f())*((C)a).gte(thr));
- }else
- {
- return (float)(c.f()/b.f());
- }
- }
- public String toString()
- {
- return a.toString()+"\n"+" "+b.toString()+"\n"+" "+c.toString()+" "+"h(.7)="+h((float)0.7)+" m()="+m();
- }
- }
- class Esercizio_12
- {
- public static void main(String[] args)
- {
- Random r=new Random(347537586);
- D[] vett=new D[50];
- for(int i=0;i<vett.length; i++)
- {
- vett[i]=new D(r.nextFloat());
- }
- //Parte 0
- for(int i=0;i<vett.length; i++)
- {
- System.out.println(i+" "+vett[i].toString());
- }
- //Parte 1
- float sommaH=0;
- for(int i=0;i<vett.length;i++)
- {
- sommaH+=vett[i].h((float)0.7);
- }
- System.out.print("Media= "+sommaH/vett.length);
- //Parte 2
- System.out.println(" vett[4].m()+vett[5].m()= "+(vett[4].m()+vett[5].m()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement