Store values in vector

Asked

Viewed 940 times

0

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

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

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

My feedback will be declared right at the beginning, ie I have an arrayinstanciada already with the "answers"

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

inserir a descrição da imagem aqui

My code at the moment is :

import java.util.Scanner;

public class ExercicioVetor6 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

       Scanner scan = new Scanner(System.in);

        int arrayAluno[] = new int[10];
        String[] gabarito = new String[] {"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 by the teacher’s exercise she wants me to keep the answers of the students, then the program will ask the number of the desired student,compare the answers and finally I must provide the percentage of students with average of 6 up! When I run the code it repeats 10 times so I can type in the students' answers,!

But I believe something is missing, I don’t know what to do to look like the image!

  • 1

    Array array, and each position of the student vector, Voce will need to store another vector, which is the 8 answers given by it. Although it is not very simple nor "elegant" to do this. Are you sure it is necessary to store notes from each student? By the content of the exercise, it is completely discardable to do this.

  • now I’m more confused.. how will I do Array of Array?

  • 1

    "But I believe something is missing" Did it get confusing, believe or miss? : ) If you need to save the students' answer, a 10 position array will only allow you to have a student’s data. Have you used a two-dimensional array? Would that be: int[][] arrayAluno= new int[10][8] I mean, it supports 10 students with 8 grades each, so I was going to be able to save everything and calculate it as I want later. To save the data can simply be like this: arrayAluno[i][j] = scan.next(), see if you can understand the logic, otherwise I give more details

  • Ricardo is missing something.. because I haven’t finished the exercise since I couldn’t even ask the user to enter the student’s number

  • Ricardo and me use a simple array? Faria an arrayAluno = new int[10] and a Abarito[8] ?

  • I insist that it is completely unnecessary to store the grade of each student to know the average of approved and the grade of each one. The exercise does not say at any time that one should store this.

  • Articuno, even if the exercise asks me to type in the student I want to see the notes I don’t need an array to store the notes?

  • No, no need, the exercise says that are data "to be read", unless your teacher has asked orally that you store, the exercise does not ask for it. You can display the student and his grade immediately after checking his test feedback, following next time, and repeating the same procedure. At the end is that you will take the amount of students with grades above 6 hits and calculate the percentage. But still, it would not be necessary to know more the grade, and yes, how many students were approved.

  • and how do I get the program to ask me to type in the student number and display the grades? just with a System.out.println ?

  • If you do it this way, with an array it will look very dirty, because you will either need 2 different arrays to store the student’s number and another one of his notes, or an array like Ricardo mentioned, however, with an extra input in the innermost array, to store the student’s number, something like arrayAluno[10][9].There is no way out of this, with simple array it would sound like dirty code, or gambit.

  • I plead for what the staff said now in Ala the teacher that the notes are stored

  • Note only? Don’t need to store every question answered by every student, correct?

  • That just the note

  • Create a two-dimensional student array, as we have explained, only instead of [10][8], do [10][2], where you will keep in each of the 10 positions, the student number and your grade.

  • thanks for the help

Show 10 more comments

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()));
        }

    }

Java tasting.:

    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);
        }
    }

Java student.:

    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++;
            }
        }

    }

Jig.:

    public class Gabarito {

        private List<String> respostas;

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

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

        }
    }
  • Java 8 or higher

  • It would be interesting to put a form with lower verses too, the author may not use java 8.

  • @Beauty article, I will edit later to add a code without the features of java 8

Browser other questions tagged

You are not signed in. Login or sign up in order to post.