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.
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);
}
}
Comments
Leave a comment