import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
public class Clasevec {
String nombre;
String cedula;
int edad;
float notas[];
public Clasevec(String nombre, String cedula, int edad, int cantidadNotas) {
this.nombre = nombre;
this.cedula = cedula;
this.edad = edad;
notas = new float[cantidadNotas];
}
/**
* Agrega una nueva nota al vector de notas
*
* @param posicion
* @param nota
*/
public void addNota(int posicion, float nota) {
notas[posicion] = nota;
}
/**
* Muestra en pantalla las notas registradas
*/
public void verNotas() {
System.out.print("Las notas son: ");
for (int i = 0; i < notas.length; i++) {
System.out.print(notas[i] + (i + 1 < notas.length ? ", " : "\n"));
}
}
/**
* Devuelve el promedio de las notas
*
* @return
*/
public float getPromedio() {
float suma = 0;
for (int i = 0; i < notas.length; i++) {
suma += notas[i];
}
return suma / notas.length;
}
public void ingresarNotas() {
int r;
for (int k = 0; k < notas.length; k++) {
notas[k] = Float.parseFloat(JOptionPane.showInputDialog("Ingrese la nota"));
}
}
public String mostrarNotas() {
String salida = "";
for (int k = 0; k < notas.length; k++) {
salida += notas[k] + " ";
}
return (salida);
}
public String mostrar() {
return (nombre + " " + cedula + " " + edad + " " + mostrarNotas());
}
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
/**
* Metodo utilizado para leer Strings
*
* @param message mensaje que se le muestra al usuario con las instrucciones
* de lo que debe introducir
* @return
*/
public static String leer(String message) {
String s = null;
try {
System.out.print(message);
s = br.readLine();
} catch (IOException ex) {
System.out.println("Hubo un error de lectura vuelva a intentarlo");
} finally {
if (s == null) {
s = leer(message);
}
}
return s;
}
/**
* Metodo utilizado para leer numeros enteros
*
* @param message
* @return
*/
public static Integer leerInt(String message) {
String s = leer(message);
Integer i = null;
try {
i = Integer.parseInt(s);
} catch (NumberFormatException ex) {
System.out.println("Hubo un error de formato. intentar de nuevo");
} finally {
if (i == null) {
i = leerInt(message);
}
}
return i;
}
/**
* Metodo utilizado para leer numeros enteros
*
* @param message
* @return
*/
public static Float leerFloat(String message) {
String s = leer(message);
Float f = null;
try {
f = Float.parseFloat(s);
} catch (NumberFormatException ex) {
System.out.println("Hubo un error de formato. intentar de nuevo");
} finally {
if (f == null) {
f = leerFloat(message);
}
}
return f;
}
public static void main(String[] args) {
System.out.println("Programa Clasevec");
String nombre = leer("Introduce el nombre: ");
String cedula = leer("Introduce la cedula: ");
int edad = leerInt("Introduce la edad: ");
int cantidadNotas = leerInt("Introduce la cantidad de notas: ");
Clasevec c = new Clasevec(nombre, cedula, edad, cantidadNotas);
for (int i = 0; i < cantidadNotas; i++) {
c.addNota(i, leerFloat("Introduce la nota " + (i + 1) + ": "));
}
c.verNotas();
System.out.println("El promedio de las notas es: " + c.getPromedio());
}
}