Java programs: 1. Create a Student class that will store their name and the grad
ID: 3817231 • Letter: J
Question
Java programs:
1. Create a Student class that will store their name and the grade from 8 tests. The following methods should be written:a. The constructorb. Get method for the namec. Set method for the named. toString()e. Add grade (for one test)f. Get the average test score (Each test is equally weighted.)g. Remove all grades (wipe out all their scores)
2. Create a lab7.java file that will have a main() method that has the followinglogic:a. Declare an array that will hold Student objects. You can assume there will be no more than 25 students to deal with.b. Read a file called “grades.txt” and populate the data for each Student inthe array. See the note below on what the data will look like in the file.c. Display each student name and the average test score they received.
Notes:
1. The grades.txt file can be found on Canvas. Each line will contain one student’s
data with the student name followed by 8 test scores. All the fields are separatedby spaces. The student name will not contain a space. Test scores are out of 100.
2. You will have to create two separate java files. Compile both of them, run only the application. Turn in both files.
3. Add a comment on top of each method, describing what that method does.
Explanation / Answer
JAVA PROGRAM :
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
class Student{
public String name;
public int grade[];
int ind=0;
public Student(String name){
this.name = name;
grade = new int[8];
}
//This method returns the name of the student
public String getName() {
return name;
}
//This method sets the name of the student
public void setName(String name) {
this.name = name;
}
//This method adds a grade
public void addGrade(int marks){
grade[ind++] = marks;
}
//This method resets all the grades
public void wipeGrades(){
grade = new int[8];
ind = 0;
}
//This method determines the average test score
public double avgTestScore(){
double sum = 0;
for(int i=0;i<8;i++){
sum+=grade[i];
}
return sum/8d;
}
}
public class lab7 {
public static void main(String[] args) throws IOException {
Scanner obj = new Scanner(new File("D:/file.txt"));
Student students[] = new Student[25];
int ind = 0;
while(obj.hasNext()){
String str[] = obj.nextLine().trim().split(" ");
students[ind] = new Student(str[0]);
for(int i=1;i<=8;i++){
students[ind].addGrade(Integer.parseInt(str[i]));
}
System.out.println("Name "+students[ind].getName()+" Average "+students[ind].avgTestScore());
ind++;
}
}
}
File.txt
prudhvi 90 91 87 100 92 88 35 89
Naveen 78 67 45 90 67 89 67 12
OUTPUT :
Name prudhvi Average 84.0
Name Naveen Average 64.375
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.