Create a program with three methods/functions, The first one takes in n which is the size of an array then creates and populate the array with student names, the second method takes in n which is the size of an array then creates and populate n by 3 array with student marks(The three marks are for test 1 - 3) and finally a method that takes in the two arrays, calculates the final mark of all test and print out the results as indicated below: The first method should return and array of names and the second should return an array all test marks(i.e. 2d array with 3 columns). The third method in addition should also return an array for all the final marks
Name Test 01 Test 02 Test 03 Final
-------- --------- --------- --------- ---------
Gerry 55 55 55 55
Sample Test 1:
Enter the total number of students:
1
Enter name:
koko
Enter mark for Test 1:
78
Enter mark for Test 2:
65
Enter mark for Test 3:
70
Name Test01 Test02 Test03 Final
------ ------ ------ ------ -----
koko 78 65 70 71
package student;
import java.util.Arrays;
import java.util.Scanner;
public class Student {
String [] names(int n){
System.out.println("Enter the student names\n");
Scanner scan = new Scanner(System.in);
String [] student_names = new String[n];
for(int i=0; i<n; i++){
System.out.println("Name "+(i+1));
student_names[i] = scan.nextLine();
}
return student_names;
}
int [] [] marks(int n){
int [] [] student_marks = new int[n][3];
for(int i=0; i<n; i++){
for(int j=0; j<3; j++){
System.out.println("Enter mark for Test "+(j+1));
Scanner scan = new Scanner(System.in);
student_marks[i][j] = scan.nextInt();
}
}
return student_marks;
}
int [] display(String student_names[], int student_marks[][]){
System.out.println("Name Test 01 Test 02 Test 03 Final");
System.out.println("---- ------- ------- -------- ---------");
int [] final_mark = new int[student_names.length];
for(int i=0; i<student_names.length; i++){
int sum = 0;
System.out.printf("%s ",student_names[i]);
for(int j=0; j<3; j++){
System.out.printf("%d ",student_marks[i][j]);
sum += student_marks[i][j];
}
System.out.printf("%d ",sum /3);
final_mark[i] = sum /3;
}
return final_mark;
}
public static void main(String[] args) {
Student stu = new Student();
int [] results = stu.display(stu.names(1), stu.marks(1));
System.out.println(Arrays.toString(results));
}
}
Comments
Leave a comment