Answer to Question #323376 in Java | JSP | JSF for Dunga

Question #323376

Write a Java driven application with the following requirements:



1. Must enter names of students.


2. Enter test 1, 2 and 3 scores for each of my students.


3.  Program must calculate the average of each student.


4. Average of each student must appear below their names. ARRAY and MENU implementation should be necessary.





1
Expert's answer
2022-04-04T09:25:40-0400
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Student{
    String name;
    int[] test =new int[3];

    public double avgTests(){
        return (test[0]+test[1]+test[2])/3;
    }
    Student(int[] test, String name){
        this.test=test;
        this.name=name;
    }

    @Override
    public String toString() {
        return name + " : " + avgTests();
    }
}
public class Main {

    public void shoAll(List<Student> list){
        for (Student student : list) {
            System.out.println(student.toString());
        }
    }

    public void addStudent(List<Student> list, Scanner in){

        int[] mark = new int[3];

        System.out.print("Name: ");
        String name = in.next();

        System.out.print("Test 1: ");
        mark[0]= in.nextInt();
        System.out.print("Test 2: ");
        mark[1]= in.nextInt();
        System.out.print("Test 3: ");
        mark[2]= in.nextInt();

        list.add(new Student(mark,name));
    }

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        List<Student> list = new ArrayList<Student>();
        Main main = new Main();

        char c;
        do {

            System.out.print("Menu\n1. Add student\n2. Show all students\n3. Exit\nEnter: ");
            int ch = in.nextInt();

            switch (ch) {
                case 1:
                    main.addStudent(list, in);
                    break;
                case 2:
                    main.shoAll(list);
                    break;
                case 3:
                    System.exit(0);
                    break;
            }

        } while (true);

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS