Store values in vector

0

I have an exercise on vetores/array with simple value, where I must correct a test, comparing it with the feedback and still calculate the percentage of the students that reached the average.

  

6) Make a program to correct multiple choice tests. Each   Proof has eight questions and each question is worth one point. The first   set of data to be read is the feedback template. The others are the   numbers and the answers they gave the questions. There are ten   students enrolled. Calculate and show:

     

a. The number and grade of each student;
  B. The percentage of approval   knowing that the minimum grade is 6.

My feedback will be declared at the beginning, that is, I have an array already in the "answers"

The teacher provided an image as she intended the exercise to run

Mycodeiscurrently:

importjava.util.Scanner;publicclassExercicioVetor6{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubScannerscan=newScanner(System.in);intarrayAluno[]=newint[10];String[]gabarito=newString[]{"b", "d", "e", "a", "b", "c", "b", "a"};
        String resposta;
        int nota = 0;


        for (int i = 0; i < 10; i ++) {

            System.out.println("--- aluno " + (i+1) + " ---");

            for (int j = 0; j < 8; j++) {
                System.out.println("digite a resposta da " + (j+1) + " questão");
                resposta = scan.next();

                if (resposta.equals(gabarito[j]) ) {
                    nota++; 
                }
            }
            System.out.println("A nota do aluno " + (i+1) + " é : " + nota);
            nota = 0; 
        }

    }


    }

But because of the teacher's exercise, I have to keep the student's answers, then the program will ask for the number of the student desired, compare the answers and finally I must give the percentage of students with an average of 6 up! When I squeeze the code it repeats 10 times for me to enter the students' responses, compares them as feedback and provides the grade!

But I believe that something is missing, I do not know what to do to stay the same as the image!

    
asked by anonymous 10.08.2018 / 14:14

1 answer

0

A possible implementation with java8

    public class App {

        public static void main(String[] args) throws IOException {
            List<Aluno> alunos = new ArrayList<>();
            long numeroAluno = 11110L;

            for (int i = 0; i < 10; i++) {
                Aluno aluno = new Aluno();
                aluno.setNumero(numeroAluno++);
                aluno.responderQuestoesDaProva();

                alunos.add(aluno);
            }

            Gabarito gabarito = new Gabarito();
            gabarito.adicionarRespostasCorretas(Collections.nCopies(8, "A"));

            for (Aluno aluno : alunos) {
                aluno.corrigirProva(gabarito);

                System.out.println(aluno.getNumero() + " : " + aluno.getNota());
            }

            long totalDeAprovados = alunos.stream()
                    .filter((a) -> a.getNota() >= 3 ? true : false)
                    .count();

            System.out.println("Passou: " + totalDeAprovados + " " + (totalDeAprovados * 100 / alunos.size()));
        }

    }

Test.java:

    public class Prova {

        private List<String> questoes = new ArrayList<>();

        public void add(String questao) {
            questoes.add(questao);
        }

        public List<String> getQuestoes() {
            return Collections.unmodifiableList(questoes);
        }
    }

Student.java:

    public class Aluno {

        private Long numero;
        private Prova prova;
        private int nota;

        private List<String> respostasPossiveis = new ArrayList<String>() {        
            private static final long serialVersionUID = 1L;
            {
                add("A");
                add("B");
                add("C");
                add("D");
            }
        };

        public Long getNumero() {
            return numero;
        }

        public void setNumero(Long numero) {
            this.numero = numero;
        }

        public Prova getProva() {
            return prova;
        }

        public void setProva(Prova prova) {
            this.prova = prova;
        }

        public void setNota(int nota) {
            this.nota = nota;
        }

        public int getNota() {
            return nota;
        }

        public void verQuestoesRespondidas() {
            prova.getQuestoes().forEach((q) -> System.out.print(q));
        }

        public void responderQuestoesDaProva() {
            prova = new Prova();
            for(int i = 0; i < 8; i++) {
                Collections.shuffle(respostasPossiveis);

                prova.add(respostasPossiveis.stream()
                        .findFirst()
                        .get());
            }
        }

        public void corrigirProva(Gabarito gabarito) {
            List<String> respostasCorretas = gabarito.getRespostas();
            List<String> questoesRespondidas = prova.getQuestoes();

            for (int i = 0;i < respostasCorretas.size(); i++) {
                if(respostasCorretas.get(i).equals(questoesRespondidas.get(i))) nota++;
            }
        }

    }

Template.java:

    public class Gabarito {

        private List<String> respostas;

        public List<String> getRespostas() {
            return Collections.unmodifiableList(respostas);
        }

        public void adicionarRespostasCorretas(List<String> respostas) {
             this.respostas = respostas;

        }
    }
    
18.08.2018 / 21:59