Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Modi6car el ejercicio de los cursos y alumnos para que se implemente composición entre las clases Curso y
- Alumno. Para ello incluir en clase Curso un vector de objetos Alumno. Además modi6car la clase Alumno para
- que almacene las notas en un vector de enteros y no guarde el promedio com un atributo.
- package curso ;
- public class Alumno {
- private String nombre ;
- private int legajo ;
- private int [] notas ;
- public String getNombre () {
- return nombre ;
- }
- public void setNombre ( String nombre ) {
- this . nombre = nombre ;
- }
- public int getLegajo () {
- return legajo ;
- }
- public void setLegajo ( int legajo ) {
- this . legajo = legajo ;
- }
- public Alumno ( String nombre , int legajo , int cantidadNotas ) {
- this . nombre = nombre ;
- this . legajo = legajo ;
- this . notas = new int [ cantidadNotas ];
- for ( int i = 0; i < cantidadNotas ; i ++) {
- this . notas [i] = -1;
- }
- }
- 1
- Laboratorio de Computación III Clase 5
- public void agregarNota ( int nota )
- {
- for ( int i = 0; i < notas . length ; i ++) {
- if( notas [i] == -1)
- notas [i] = nota ;
- }
- }
- public float getPromedio ()
- {
- int total = 0, cantidad = 0;
- for ( int i = 0; i < notas . length ; i ++)
- {
- if( notas [i] > -1)
- {
- total += notas [i];
- cantidad ++;
- }
- }
- return ( float ) total / cantidad ;
- }
- @Override
- public String toString () {
- return " Alumno " + nombre + " - Legajo : " + legajo + " - Promedio : " + getPromedio ();
- }
- }
- package curso ;
- public class Curso {
- private String nombre ;
- private Alumno [] alumnos ;
- public String getNombre () {
- return nombre ;
- }
- public void setNombre ( String nombre ) {
- this . nombre = nombre ;
- }
- public Curso ( String nombre , int cantidad ) {
- this . nombre = nombre ;
- this . alumnos = new Alumno [ cantidad ];
- }
- public void agregarAlumno ( Alumno nuevo ) {
- for ( int i = 0; i < alumnos . length ; i ++) {
- if ( alumnos [i] == null ) {
- alumnos [i] = nuevo ;
- return ;
- }
- }
- }
- public float promedioGeneral () {
- float suma = 0f;
- 2
- Laboratorio de Computación III Clase 5
- int c = 0;
- for ( int i = 0; i < alumnos . length ; i ++) {
- if ( alumnos [i] != null ) {
- c ++;
- suma += alumnos [i]. getPromedio ();
- }
- }
- return suma / c;
- }
- public int cantidadMayor8 () {
- int c = 0;
- for ( int i = 0; i < alumnos . length ; i ++) {
- if ( alumnos [i] != null && alumnos [i]. getPromedio () > 8) {
- c ++;
- }
- }
- return c;
- }
- public String listadoOpcionStringBuilder () {
- StringBuilder sb = new StringBuilder ();
- for ( int i = 0; i < alumnos . length ; i ++){
- if ( alumnos [i] != null ) {
- sb. append ( alumnos [i]. toString ());
- sb. append ("\n");
- }
- }
- return sb. toString ();
- }
- public String listadoOpcionStringBuilderForEach () {
- StringBuilder sb = new StringBuilder ();
- for ( Alumno alumno : alumnos ) {
- if ( alumno != null ) {
- sb. append ( alumno . toString ());
- sb. append ("\n");
- }
- }
- return sb. toString ();
- }
- public String listadoOpcionString ()
- {
- String listado = "";
- for ( int i = 0; i < alumnos . length ; i ++) {
- if( alumnos [i] != null )
- {
- listado += alumnos [i]. toString () + "\n";
- }
- }
- return listado ;
- }
- public String listadoOpcionStringForEach ()
- {
- String listado = "";
- for ( Alumno alumno : alumnos ) {
- if ( alumno != null ) {
- listado += alumno . toString () + "\n";
- }
- }
- return listado ;
- 3
- Laboratorio de Computación III Clase 5
- }
- }
- package curso ;
- import java . util . Scanner ;
- public class Main {
- public static void main ( String [] args ) {
- Scanner sc = new Scanner ( System .in );
- String nombreCurso ;
- int cantidadAlumnos ;
- System . out . println (" Ingrese el nombre del curso ");
- nombreCurso = sc. next ();
- System . out . println (" Ingrese la cantidad de alumnos ");
- cantidadAlumnos = sc. nextInt ();
- Curso c = new Curso ( nombreCurso , cantidadAlumnos );
- for ( int i = 0; i < cantidadAlumnos ; i ++) {
- System . out . println (" Ingrese el nombre del alumno ");
- String nombre = sc. next ();
- System . out . println (" Ingrese el legajo ");
- int legajo = sc. nextInt ();
- System . out . println (" Ingrese la cantidad de notas ");
- int notas = sc. nextInt ();
- Alumno nuevo = new Alumno ( nombre , legajo , notas );
- System . out . println (" Ingrese " + notas + " notas ");
- for ( int j = 0; j < notas ; j ++) {
- System . out . println (j + ")");
- int nota = sc. nextInt ();
- nuevo . agregarNota ( nota );
- }
- c. agregarAlumno ( nuevo );
- }
- System . out . println (" Listado de alumnos ingresados ");
- System . out . println (c. listadoOpcionStringBuilderForEach ());
- System . out . println ("El promedio general del curso es: " + c. promedioGeneral ());
- System . out . println (" Hay " + c. cantidadMayor8 () + " alumnos con promedio > 8");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement