Use the partial class definition shown below and create the following methods to
ID: 3594159 • Letter: U
Question
Use the partial class definition shown below and create the following methods to include in the Student class: a constructor that takes one input argument as the number of homework grades, a method that will calculate the average homework grade for the student. Also create a main method (not included in the Student class) that calculates the average homework grade for each student in a course of 25 total students. Each student in the course has 4 homework assignments. public class Student { private String firstName; // first name of student private String lastName; // last name of student private int[ ] homeworkGrades; // the homework grades for the student private double averageHomeworkGrade; // the average homework grade for the student }
Explanation / Answer
// All the requrement has been fulfilled as the description given
package org.java.demo.programs;
import java.util.Scanner;
public class Student{
private String firstName;
private String lastName;
private int[ ] homeworkGrades;
private double averageHomeworkGrade;
public Student(int[] homeworkGrades){ // for taking integer array as input during
// each student initialization
this.homeworkGrades = homeworkGrades;
}
public double calculateAverageGrades(){ // for calculating average grades
double sum= 0.0;
int count = 0;
double average = 0.0;
for(int i = 0; i < homeworkGrades.length; i++){
sum = sum + homeworkGrades[i];
count++;
}
this.averageHomeworkGrade= (double)sum/count;
return averageHomeworkGrade;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getFirstName(){
return this.firstName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getLastName(){
return this.lastName;
}
public static void main(String args[]){
Student[] arr = new Student[25]; // array of 25 students
for(int s = 0; s < arr.length; s++){
int[] assignments = new int[4];
System.out.println("Enter the grades in number for 4 assignments");
Scanner sc3 = new Scanner(System.in);
for(int j = 0; j < 4; j++){
assignments[j] = sc3.nextInt();
}
/*while(sc3.hasNextInt()){
assignments[j] = sc3.nextInt();
j++;
}*/
Student obj = new Student(assignments);
System.out.println("Enter first Name of this Student");
Scanner sc = new Scanner(System.in);
String fname = sc.nextLine();
obj.setFirstName(fname);
System.out.println("Enter Last Name of this Student");
Scanner sc1 = new Scanner(System.in);
String lname = sc1.nextLine();
obj.setLastName(lname);
double amount = obj.calculateAverageGrades();
System.out.println("Average Grades for this student " + amount);
arr[s] = obj;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.