Foros del Web » Programación para mayores de 30 ;) » Java »

colecciones

Estas en el tema de colecciones en el foro de Java en Foros del Web. Nada estoy realizando un ejercicio de colecciones y no me sale aver si me podrian ayudar en concreto varios ejercicio asi que si soy tan ...
  #1 (permalink)  
Antiguo 26/04/2012, 07:27
 
Fecha de Ingreso: marzo-2012
Mensajes: 30
Antigüedad: 12 años, 1 mes
Puntos: 0
colecciones

Nada estoy realizando un ejercicio de colecciones y no me sale aver si me podrian ayudar en concreto varios ejercicio asi que si soy tan amables es que si no no puedo continuar .Por un lado tengo la clase Socio:
/**
* Store details of a club membership.
*
* @author David J. Barnes and Michael Kolling
* @version 2006.03.30
*/
public class Socio
{
// The name of the member.
private String nombre;
// The month in which the membership was taken out.
private int mes;
// The year in which the membership was taken out.
private int año;

/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public Socio()
{
// put your code here

}


/**
* Constructor for objects of class Membership.
* @param name The name of the member.
* @param month The month in which they joined. (1 ... 12)
* @param year The year in which they joined.
*/
public Socio(String nombre, int mes, int año)
throws IllegalArgumentException
{
if(mes < 1 || mes > 12) {
throw new IllegalArgumentException(
"Mes " + mes + " el parametro esta fuera del rango 1 ... 12");
}
this.nombre = nombre;
this.mes= mes;
this.año = año;
}

/**
* @return The member's name.
*/
public String getNombre()
{
return nombre;
}

/**
* @return The month in which the member joined.
* A value in the range 1 ... 12
*/
public int getMes()
{
return mes;
}

/**
* @return The year in which the member joined.
*/
public int getAño()
{
return año;
}

/**
* @return A string representation of this membership.
*/
public String toString()
{
return "Nombre: " +nombre +
" joined in month " +
mes + " of " + año;
}
}

Luego tengo la clase Club que utiliza la clase socio:

import java.util.ArrayList;

/**
* Store details of club memberships.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Club
{
private ArrayList<Socio> socios;

/**
* Constructor for objects of class Club
*/
public Club()
{
socios=new ArrayList<Socio>();

}

/**
* metodo para inscribir un nuevo socio al club.
* lo podemos asociar mediante parametros.
*/
public void agregarSocios(String nombre, int mes, int año)
{
socios.add(new Socio( nombre,mes, año));

}



/**
* @return The number of members (Membership objects) in
* the club.
*/
public int numberDeSocios()
{
return socios.size();
}

/**
*
*/
public int asociadoEnMes(int mes)
throws IllegalArgumentException
{
if(mes < 1 || mes > 12) {
throw new IllegalArgumentException(
"Mes " + mes + " el parametro esta fuera del rango 1 ... 12");
}

int asociadoEnMes=0;
int indice=0;
while(indice<socios.size()){
if((socios.get(indice)). getMes()=mes){
asociadoEnMes= asociadoEnMes+socios.get(Socio);}
return asociadoEnMes;}

}




}
Entonces tengo que realizar un ejercicio el cual me dice que tengo que crear un metodo que me diga al pasarle el parametro mes el numero de socios que se afiliaron ese mes .lo que tengo que hacer es recorrer la coleccion y los elemetos que se afiliaron ese mes sumarlos y devolverlos en una variable pero no se hacerlo espero que me ayuden por favor
  #2 (permalink)  
Antiguo 26/04/2012, 07:51
 
Fecha de Ingreso: marzo-2012
Ubicación: Madrid
Mensajes: 74
Antigüedad: 12 años, 1 mes
Puntos: 12
Respuesta: colecciones

La comparacion de enteros en java se hace con el operador "==". El operador "=" es para las asignaciones.


socios.get(Socio) No tiene sentido. De hecho creo que ni compilará.

Repasa el codigo, tienes errores de logica en el algoritmo del metodo asociadoEnMes.

Etiquetas: clase, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:39.