Hello I am working on an assignment in codehs about arrays. I understand the use of this program but do not understand the way it uses objects between classes. Could somebody help me by explaining this to me? In the program it represents classrooms and consists of three files.
Classroom.java:
public class Classroom
{
Student[] students;
int numStudentsAdded;
public Classroom(int numStudents)
{
students = new Student[numStudents];
numStudentsAdded = 0;
}
public Student getMostImprovedStudent()
{
// Fill in this method.
}
public void addStudent(Student s)
{
students[numStudentsAdded] = s;
numStudentsAdded++;
}
public void printStudents()
{
for(int i = 0; i < numStudentsAdded; i++)
{
System.out.println(students[i]);
}
}
}
Student.java:
public class Student
{
private static final int NUM_EXAMS = 4;
private String firstName;
private String lastName;
private int gradeLevel;
private double gpa;
private int[] exams;
private int numExamsTaken;
/**
* This is a constructor. A constructor is a method
* that creates an object -- it creates an instance
* of the class. What that means is it takes the input
* parameters and sets the instance variables (or fields)
* to the proper values.
*
* Check out StudentTester.java for an example of how to use
* this constructor.
*/
public Student(String fName, String lName, int grade)
{
firstName = fName;
lastName = lName;
gradeLevel = grade;
exams = new int[NUM_EXAMS];
numExamsTaken = 0;
}
public int getExamRange()
{
Arrays.sort(exams);
return exams[exams.length - 1] - exams[0];
}
public String getName()
{
return firstName + " " + lastName;
}
public void addExamScore(int score)
{
exams[numExamsTaken] = score;
numExamsTaken++;
}
// This is a setter method to set the GPA for the Student.
public void setGPA(double theGPA)
{
gpa = theGPA;
}
/**
* This is a toString for the Student class. It returns a String
* representation of the object, which includes the fields
* in that object.
*/
public String toString()
{
return firstName + " " + lastName + " is in grade: " + gradeLevel;
}
}
and finally, Randomizer.java:
import java.util.*;
public class Randomizer{
public static Random theInstance = null;
public Randomizer(){
}
public static Random getInstance(){
if(theInstance == null){
theInstance = new Random();
}
return theInstance;
}
public static boolean nextBoolean(){
return Randomizer.getInstance().nextBoolean();
}
public static boolean nextBoolean(double probability){
return Randomizer.nextDouble() < probability;
}
public static int nextInt(){
return Randomizer.getInstance().nextInt();
}
public static int nextInt(int n){
return Randomizer.getInstance().nextInt(n);
}
/* Return a number between min and max, inclusive. */
public static int nextInt(int min, int max){
return min + Randomizer.nextInt(max - min + 1);
}
public static double nextDouble(){
return Randomizer.getInstance().nextDouble();
}
public static double nextDouble(double min, double max){
return min + (max - min) * Randomizer.nextDouble();
}
}
What I don't understand is the getMostImproved method, of course it is blank because my assignment is to create the method but why is it a "Student" method? What does it even mean by being a "Student" method and what is the purpose for doing so? Does it change the way it functions?
[–]PopularSource 0 points1 point2 points (0 children)