class Allievo
public class Allievo { private String cognome, nome; private char sesso; private int nascita; public Allievo(String cognome, String nome, char sesso, int nascita) { setCognome(cognome); setNome(nome); setSesso(sesso); setNascita(nascita); } public String getCognome() { return cognome; } public void setCognome(String cognome) { this.cognome = cognome; } public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; } public char getSesso() { return sesso; } public void setSesso(char sesso) { if (sesso == 'm' || sesso == 'M' || sesso == 'f' || sesso == 'F') this.sesso = sesso; } public int getNascita() { return nascita; } public void setNascita(int nascita) { this.nascita = nascita; } public String toString() { return "Allievo: cognome=" + getCognome() + ", nome=" + getNome() + ", sesso=" + getSesso() + ", nascita=" + getNascita(); } }
class Main
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { Allievo[] classe3E = new Allievo[30]; InputStreamReader input = new InputStreamReader(System.in); BufferedReader tastiera = new BufferedReader(input); System.out.println("Inserisci il numero di allievi:"); int nMax = 0; try { nMax = Integer.parseInt(tastiera.readLine()); } catch (NumberFormatException | IOException e) { } for (int i = 0; i < nMax; i++) { System.out.println("Allevo " + i + ": Cognome, Nome, Sesso, Nascita"); try { classe3E[i] = new Allievo(tastiera.readLine(), tastiera.readLine(), tastiera.readLine().charAt(0), Integer.parseInt(tastiera.readLine())); } catch (NumberFormatException | IOException e) { } } System.out.println("Elenco degli allievi"); for (int i = 0; i < nMax; i++) { System.out.println(classe3E[i].toString()); } } }