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
import java.util.Arrays;
import java.util.Scanner;
public class Student {
String [] N(int name){
System.out.println("Input the student names\n");
Scanner input = new Scanner(System.in);
String [] stuN = new String[name];
for(int x=0; x<name; x++){
System.out.println("Name "+(x+1));
stuN[x] = input.nextLine();
}
return stuN;
}
int [][] marks(int name){
int [][] stuMark = new int[name][3];
for(int x=0; x<name; x++){
for(int y=0; y<3; y++){
System.out.println("Input test mark: "+(y+1));
Scanner input = new Scanner(System.in);
stuMark[x][y] = input.nextInt();
}
}
return stuMark;
}
int [] display(String stuN[], int stuMark[][]){
System.out.println("Name Test 01 Test 02 Test 03 Final");
System.out.println("---- ------- ------- -------- ---------");
int [] totalMarks = new int[stuN.length];
for(int x=0; x<stuN.length; x++){
int sum = 0;
System.out.print("%s ",stuN[x]);
for(int y=0; y<3; y++){
System.out.print("%d ",stuMark[x][y]);
sum += stuMark[x][y];
}
System.out.print("%d ",sum /3);
totalMarks[x] = sum /3;
}
return totalMarks;
}
public static void main(String[] args) {
Student S = new Student();
int [] results = S.display(S.N(1), S.marks(1));
System.out.println(Arrays.toString(results));
}
}
Comments
Leave a comment