Nom:_________________________________Prénom:___________________________ Université Pierre et Marie Curie Controle III: UE: Eléments de programmation par objets avec Java (2I002) 1 (6.0 points) : Héritage et classe abstraite On veut écrire les classes à la hiérarchie suivante (le niveau d’indentation correspond au niveau de la hiérarchie) : Figure3D |____| Parallelepipede |____ Cube | Ellipsoide |___ Sphere • La classe Figure3D a les attributs pour garder les positions x, y et z de la figure. Un constructeur à trois paramètres devra initialiser les positions. publicabstractclassFigure3D{ protectedintx,y,z; publicFigure3D(intx,inty,intz){ this.x=x; this.y=y; this.z=z; } publicabstractdoublevolume(); } • La classe Parallelepipede a les attributs L=largeur, k=longueur et h=hauteur. Un constructeur à trois paramètres pour initialiser les attributs. Une méthode pour calculer le volume V = L x k x h publicclassParallelepipedeextendsFigure3D{ protectedintl; protectedintk; protectedinth; publicParallelepipede(intx,inty,intz,intl,intk,inth){ super(x,y,x); this.l=l; this.k=k; this.h=h; } publicdoublevolume(){ return(l*k*h); } } • La classe cube a l’attribue c=côte. Un constructeur à un paramètre pour initialiser côte. Une méthode pour calculer le volume V=c3. publicclassCubeextendsParallelepipede{ publicCube(intx,inty,intz,intcote){ super(x,y,z,cote,cote,cote); } } • La classe Ellipsoide a les attributs a, b et c (voir figure). Un constructeur à trois paramètres pour initialiser les attributs. Une méthode pour calculer le volume V =(4πabc)/3 publicclassEllipsoideextendsFigure3D{ protectedinta; protectedintb; protectedintc; publicEllipsoide(intx,inty,intz,inta,intb,intc){ super(x,y,x); this.a=a; this.b=b; this.c=c; } publicdoublevolume(){ return((4*Math.PI*a*b*c)/3); } } • La classe Sphere a l'attributs r=rayon. Un constructeur à un paramètre pour initialiser rayon. Une méthode pour calculer le volume V =(4πr3)/3 publicclassSphereextendsEllipsoide{ } publicSphere(intx,inty,intz,intrayon){ super(x,y,z,rayon,rayon,rayon); } Voici comme tester les classes ci-dessus. publicclassTestFig3D{ publicstaticvoidmain(String[]args){ Parallelepipedepar=newParallelepipede(0,0,0,2,4,5); System.out.println("VolumeParallelepipede="+par.volume()); Cubecube=newCube(0,0,0,4); System.out.println("VolumeCube="+cube.volume()); Ellipsoideelip=newEllipsoide(0,0,0,3,1,2); System.out.println("VolumeEllipsoide="+elip.volume()); Spheresphere=newSphere(0,0,0,3); System.out.println("VolumeSphere="+sphere.volume()); } } 2 (6.0 points) : Héritage et liaison dynamique 2.1 La méthode main suivante est-elle correcte ? Expliquez les erreurs. publicclassA{} 1publicclassBextendsA{ 2 publicvoidmetB(){System.out.println("hello");} 3 publicstaticvoidmain(String[]a){ 4 Bc1=newB(); 5 Am1=c1; 6 c1=(B)m1; 7 c1=m1; 8 Am2=newA(); 9 Bc2=(B)m2; 10} 11} Compilation B.java:7: error: incompatible types: A cannot be converted to B c1 = m1; 1 error Execution Exception in thread "main" java.lang.ClassCastException: A cannot be cast to B at B.main(B.java:9) 2.2 Soient les 2 classes suivantes : publicclassC{ publicvoidf(){} } publicclassDextendsC{ publicvoidg(){} } publicclassEextendsC{ publicvoidh(){} } Parmi les instructions suivantes, lesquelles provoquent une erreur à la compilation et les lesquelles provoquent une erreur à l’exécution? Expliquez chaque ligne. 1Ca1=newC(); 2Dp1=newD(); 3Ec1=newE(); 4Ca2=p1; 5Ca3=(C)p1; 6Dp2=a1; 7Dp3=(D)a1; 8a2.g(); 9p1.f(); 10p2.h(); Compilation TestCDE.java:6: error: incompatible types: C cannot be converted to D D p2=a1; ^ TestCDE.java:8: error: cannot find symbol a2.g(); ^ symbol: method g() location: variable a2 of type C TestCDE.java:10: error: cannot find symbol p2.h(); ^ symbol: method h() location: variable p2 of type D 3 errors Execution Exception in thread "main" java.lang.ClassCastException: C cannot be cast to D at TestCDE.main(TestCDE.java:7) 3 (8.0 point) : Exception Ecrire la classe Liste qui contiendra les méthodes -getListInt(String [] tab) ; Cette méthode va transformer un tableau de String dans un tableau d’entiers. Pour cela utilisez la méthode Integer.parseInt pour transforme une String en entier et lève une exception NumberFormatException si la chaîne n’est pas un entier. Cette exception sera capturée dans le main. -division (int [] tab) ; Cette méthode va diviser le élément n+1 par n et retourner la somme. Par exemple : tab = [2 , 4, 8], 4/2 + 8/4 = 6. La méthode lancera une instance de la classe ArithmeticException lorsque une division par zéro se présente. Cette exception sera capturée dans le main. -maxSomme(int [] tab, int n) ; Cette méthode va faire la somme des éléments de tab et lancera une Exception du type MaxSommeExecption si la somme est superior à N. L’exception sera capturée dans le main. Vous devez créer la classe MaxSommeExecption. -public static void main (String [] args) ; La méthode main va appeler chaque méthode précédant et traiter les exceptions. public class Liste { public static int [] getListInt( String [ ] tab ) throws NumberFormatException{ int [] tabInt = new int [tab.length]; for (int i = 0; i < tab.length; i++){ tabInt[i] = Integer.parseInt(tab[i]); } return tabInt; } public static double division( int [ ] tab ) throws ArithmeticException{ double div = 0; for (int i = 0; i < tab.length - 1; i++){ div = div + (tab[i+1]/tab[i]); } return div; } public static double maxSomme( int [ ] tab, int n ) throws MaxSommeExecption { double somme = 0; for (int i = 0; i < tab.length; i++){ somme = somme + tab[i]; if (somme > n) throw new MaxSommeExecption("La somme est superior a " + n); } return somme; } public static void main( String [ ] args ) { try{ int [] tabInt = getListInt(args); double div = division(tabInt); double somme = maxSomme(tabInt, 3); }catch (NumberFormatException e){ System.out.println("NumberFormatException " +e.getMessage()); }catch (ArithmeticException e){ System.out.println("ArithmeticException " + e.getMessage()); }catch (MaxSommeExecption e){ System.out.println("MaxSommeExecption " + e.getMessage()); } } } public class MaxSommeExecption extends Exception{ public MaxSommeExecption(String msg){ super (msg); } }