Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 02Create a class, Student, with the following private attributes: name

ID: 3703851 • Letter: Q

Question

Question 02Create a class, Student, with the following private attributes: name (string), and an array or vector to hold 5 grades (int). The class has public set and get methods as well as a public method to calculate and return the student’s average grade (as float).The main program will create an array or vector to handle up to 30 students. The data for all students will be read from file studentData.txt (which you will create) and stored into the array or vector of student objects. You must have between 6 and 30 students in your file (read until eof).Your program will process the array or vector of students and write to the console the student’s name, all grades, and average grade, using a column format. Allow 35 spaces for name, and eight spaces for each grade.After all students are listed, write summary data, one per line, of how many students were in the class, the highest and lowest average grade, and the average grade for the entire class.

Explanation / Answer

Hello there,

I am writing here complete java code for above requirement, in which i tested the code and have output for data of 5 students and i am also posting here the format of text file i.e.

ramesh 21 20 23 34 43
hari 12 20 23 34 43
tushar 23 20 23 34 43
juhi 25 20 23 34 43
raman 25 20 23 34 43

you can add more entries upto 30 for running the code :

the final code is:

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Scanner;

public class Student {

private String name;

private Integer[] marks = new Integer[5];

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer[] getMarks() {

return marks;

}

public void setMarks(Integer[] marks) {

this.marks = marks;

}

public static void main(String[] args) throws IOException {

Scanner s = new Scanner(new File("/home/ist/Desktop/my Folder/Chegg/test/txt.txt"));

Student[] finalArr = new Student[30];

int i= 0 ;

while (s.hasNext())

{

if(i==30){

throw new IOException("you can add upto 30 entries only");

}

finalArr[i] = new Student();

Integer[] temp = new Integer[5];

finalArr[i].setName(s.next());

temp[0] = s.nextInt();

temp[1] = s.nextInt();

temp[2] = s.nextInt();

temp[3] = s.nextInt();

temp[4] = s.nextInt();

// System.out.println(temp[0]);

finalArr[i].setMarks(temp);

i++;

}

System.out.println("Name"+" "+"Marks"+" "+"Average");

Map<String,Float> countMap = new HashMap<String,Float>();

List<Float> countList = new ArrayList<Float>();

for (Student student : finalArr) {

if(student != null){

System.out.println(student.getName()+" "+getMark(student.getMarks())+" "+getAvg(student.getMarks()).toString());

countList.add(getAvg(student.getMarks()));

countMap.put(student.getName(), getAvg(student.getMarks()));

}

}

int minIndex = countList.indexOf(Collections.min(countList));

int maxIndex = countList.indexOf(Collections.max(countList));

System.out.println("the total students who are having lowest average grade are:"+Collections.frequency(countList, countList.get(minIndex)));

System.out.println("the total students who are having highest average grade are:"+Collections.frequency(countList, countList.get(maxIndex)));

Double average = countList.stream().mapToDouble(val -> val).average().getAsDouble();

System.out.println("the Average grade of class is:"+average);

}

private static List<String> getMark(Integer[] marks2) {

// TODO Auto-generated method stub

List<String> temp= new ArrayList<String>();

for (Integer integer : marks2) {

temp.add(integer.toString());

}

return temp;

}

private static Float getAvg(Integer[] marks2) {

// TODO Auto-generated method stub

Integer sum = 0;

for (Integer integer : marks2) {

sum +=integer;

}

Float avg = (float) (sum/5);

return avg;

}

}

in this code, i created the Array of student object which has string of name and integer array of marks, which has get and set method for it. It also has seperate public methods for getMarks and getAverge.

The output for 5 entries is :

Name Marks Average

ramesh [21, 20, 23, 34, 43] 28.0

hari [12, 20, 23, 34, 43] 26.0

tushar [23, 20, 23, 34, 43] 28.0

juhi [25, 20, 23, 34, 43] 29.0

raman [25, 20, 23, 34, 43] 29.0

the total students who are having lowest average grade are:1

the total students who are having highest average grade are:2

the Average grade of class is:28.0

Hope, you got the complete solution, feel free to ask any doubt,

Thank you