Advertisement
comercio1

poolifhshsf

Apr 27th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. 1. Modi6car el ejercicio de los cursos y alumnos para que se implemente composición entre las clases Curso y
  2. Alumno. Para ello incluir en clase Curso un vector de objetos Alumno. Además modi6car la clase Alumno para
  3. que almacene las notas en un vector de enteros y no guarde el promedio com un atributo.
  4. package curso ;
  5. public class Alumno {
  6. private String nombre ;
  7. private int legajo ;
  8. private int [] notas ;
  9. public String getNombre () {
  10. return nombre ;
  11. }
  12. public void setNombre ( String nombre ) {
  13. this . nombre = nombre ;
  14. }
  15. public int getLegajo () {
  16. return legajo ;
  17. }
  18. public void setLegajo ( int legajo ) {
  19. this . legajo = legajo ;
  20. }
  21. public Alumno ( String nombre , int legajo , int cantidadNotas ) {
  22. this . nombre = nombre ;
  23. this . legajo = legajo ;
  24. this . notas = new int [ cantidadNotas ];
  25. for ( int i = 0; i < cantidadNotas ; i ++) {
  26. this . notas [i] = -1;
  27. }
  28. }
  29. 1
  30. Laboratorio de Computación III Clase 5
  31. public void agregarNota ( int nota )
  32. {
  33. for ( int i = 0; i < notas . length ; i ++) {
  34. if( notas [i] == -1)
  35. notas [i] = nota ;
  36. }
  37. }
  38. public float getPromedio ()
  39. {
  40. int total = 0, cantidad = 0;
  41. for ( int i = 0; i < notas . length ; i ++)
  42. {
  43. if( notas [i] > -1)
  44. {
  45. total += notas [i];
  46. cantidad ++;
  47. }
  48. }
  49. return ( float ) total / cantidad ;
  50. }
  51. @Override
  52. public String toString () {
  53. return " Alumno " + nombre + " - Legajo : " + legajo + " - Promedio : " + getPromedio ();
  54. }
  55. }
  56. package curso ;
  57. public class Curso {
  58. private String nombre ;
  59. private Alumno [] alumnos ;
  60. public String getNombre () {
  61. return nombre ;
  62. }
  63. public void setNombre ( String nombre ) {
  64. this . nombre = nombre ;
  65. }
  66. public Curso ( String nombre , int cantidad ) {
  67. this . nombre = nombre ;
  68. this . alumnos = new Alumno [ cantidad ];
  69. }
  70. public void agregarAlumno ( Alumno nuevo ) {
  71. for ( int i = 0; i < alumnos . length ; i ++) {
  72. if ( alumnos [i] == null ) {
  73. alumnos [i] = nuevo ;
  74. return ;
  75. }
  76. }
  77. }
  78. public float promedioGeneral () {
  79. float suma = 0f;
  80. 2
  81. Laboratorio de Computación III Clase 5
  82. int c = 0;
  83. for ( int i = 0; i < alumnos . length ; i ++) {
  84. if ( alumnos [i] != null ) {
  85. c ++;
  86. suma += alumnos [i]. getPromedio ();
  87. }
  88. }
  89. return suma / c;
  90. }
  91. public int cantidadMayor8 () {
  92. int c = 0;
  93. for ( int i = 0; i < alumnos . length ; i ++) {
  94. if ( alumnos [i] != null && alumnos [i]. getPromedio () > 8) {
  95. c ++;
  96. }
  97. }
  98. return c;
  99. }
  100. public String listadoOpcionStringBuilder () {
  101. StringBuilder sb = new StringBuilder ();
  102. for ( int i = 0; i < alumnos . length ; i ++){
  103. if ( alumnos [i] != null ) {
  104. sb. append ( alumnos [i]. toString ());
  105. sb. append ("\n");
  106. }
  107. }
  108. return sb. toString ();
  109. }
  110. public String listadoOpcionStringBuilderForEach () {
  111. StringBuilder sb = new StringBuilder ();
  112. for ( Alumno alumno : alumnos ) {
  113. if ( alumno != null ) {
  114. sb. append ( alumno . toString ());
  115. sb. append ("\n");
  116. }
  117. }
  118. return sb. toString ();
  119. }
  120. public String listadoOpcionString ()
  121. {
  122. String listado = "";
  123. for ( int i = 0; i < alumnos . length ; i ++) {
  124. if( alumnos [i] != null )
  125. {
  126. listado += alumnos [i]. toString () + "\n";
  127. }
  128. }
  129. return listado ;
  130. }
  131. public String listadoOpcionStringForEach ()
  132. {
  133. String listado = "";
  134. for ( Alumno alumno : alumnos ) {
  135. if ( alumno != null ) {
  136. listado += alumno . toString () + "\n";
  137. }
  138. }
  139. return listado ;
  140. 3
  141. Laboratorio de Computación III Clase 5
  142. }
  143. }
  144. package curso ;
  145. import java . util . Scanner ;
  146. public class Main {
  147. public static void main ( String [] args ) {
  148. Scanner sc = new Scanner ( System .in );
  149. String nombreCurso ;
  150. int cantidadAlumnos ;
  151. System . out . println (" Ingrese el nombre del curso ");
  152. nombreCurso = sc. next ();
  153. System . out . println (" Ingrese la cantidad de alumnos ");
  154. cantidadAlumnos = sc. nextInt ();
  155. Curso c = new Curso ( nombreCurso , cantidadAlumnos );
  156. for ( int i = 0; i < cantidadAlumnos ; i ++) {
  157. System . out . println (" Ingrese el nombre del alumno ");
  158. String nombre = sc. next ();
  159. System . out . println (" Ingrese el legajo ");
  160. int legajo = sc. nextInt ();
  161. System . out . println (" Ingrese la cantidad de notas ");
  162. int notas = sc. nextInt ();
  163. Alumno nuevo = new Alumno ( nombre , legajo , notas );
  164. System . out . println (" Ingrese " + notas + " notas ");
  165. for ( int j = 0; j < notas ; j ++) {
  166. System . out . println (j + ")");
  167. int nota = sc. nextInt ();
  168. nuevo . agregarNota ( nota );
  169. }
  170. c. agregarAlumno ( nuevo );
  171. }
  172. System . out . println (" Listado de alumnos ingresados ");
  173. System . out . println (c. listadoOpcionStringBuilderForEach ());
  174. System . out . println ("El promedio general del curso es: " + c. promedioGeneral ());
  175. System . out . println (" Hay " + c. cantidadMayor8 () + " alumnos con promedio > 8");
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement