In: Computer Science
Lab 3 Java Classes and Memory Management
Multi-file Project
public class Student
{
private String fName ;
private String lName ;
private double[] grades;
}
This class should be in the package com.csc241. Write a program that prompts a user for a total number of students and dynamically allocates memory for just that number of Student objects in a Student[]. Then continuously re-prompt the user to enter, on a studentbystudent basis,
Next,
As always, keep in mind all of the general rules of good program construction.
How many students are in your class - 3
Enter the first name of student #1 - Joe
Enter Mike’s last name - Shmo
How many grades will you be entering for Joe Shmo? – 5
Plz enter the grades for Joe Shmo
100
97
87
87
92
Grade Statistics for Joe Shmo (average=92.6 ; max/min=100/87)
Enter the first name of student #2 - Jack
Enter Jack’s last name - Schwartz
How many grades will you be entering for Jack Schwartz? – 3
Plz enter the grades for Jack Schwartz
100
90
0
Grade Statistics for Jack Schwartz (average=63.3 ; max/min=100/0)
Enter the first name of student #3 - Joan
Enter Joan’s last name - Jameson
How many grades will you be entering for Joan Jameson? – 4
Plz enter the grades for Joan Jameson
98
95
93
94
Grade Statistics for Joan Jameson (average=95.0 ; max/min=98/93)
Ensemble Statistics - Average=86.1 ; Max/Min=100/0)
Do you wish to continue (Y/N) - n
//Java code
package com.csc241; public class Student { private String fName ; private String lName ; private double[] grades; //Constructor public Student() { fName = "None"; lName ="None"; grades = null; } //Overload Constructor public Student(String fName, String lName, double[] grades) { this.fName = fName; this.lName = lName; this.grades = grades; } //getters and setters public String getfName() { return fName; } public void setfName(String fName) { this.fName = fName; } public String getlName() { return lName; } public void setlName(String lName) { this.lName = lName; } public double[] getGrades() { return grades; } public void setGrades(double[] grades) { this.grades = grades; } public double getAverage() { double sum =0; for (int i = 0; i <grades.length ; i++) { sum += grades[i]; } double avg = sum/grades.length; return avg; } public double getMax() { double max = grades[0]; for (int i = 0; i <grades.length ; i++) { if(max<grades[i]) max = grades[i]; } return max; } public double getMin() { double min = grades[0]; for (int i = 0; i <grades.length ; i++) { if(min>grades[i]) min = grades[i]; } return min; } @Override public String toString() { return "Grade Statistics for "+fName+" "+lName+"(average = "+String.format("%.1f",getAverage())+"; max/min = "+String.format("%.0f",getMax())+"/"+String.format("%.0f",getMin())+")"; } }
//==================================
import com.csc241.Student; import java.util.Scanner; public class Main { public static void main(String[] args) { //Create student object Student student = new Student(); //For user input Scanner input = new Scanner(System.in); Student[] students; System.out.print("How many students are in your class: "); int numberOfStudents = input.nextInt(); students = new Student[numberOfStudents]; for (int i = 0; i <students.length ; i++) { System.out.print("Enter the first name of student #"+(i+1)+": "); student.setfName(input.next()); System.out.print("Enter "+student.getfName()+"'s last name: "); student.setlName(input.next()); System.out.print("How many grades will you be entering for "+student.getfName()+" "+student.getlName()+"? "); int gradeSize = input.nextInt(); double[] grades = new double[gradeSize]; System.out.print("Plz enter the grades for "+student.getfName()+" "+student.getlName()+"\n"); for (int j = 0; j <grades.length ; j++) { grades[j] = input.nextDouble(); } student.setGrades(grades); students[i] = student; System.out.println(students[i]); } } }
//Output
//If you need any help regarding this solution .......... please leave a comment ...... thanks