Search code examples
javaarraysarraylistconstructorencapsulation

Java calling an object into an arraylist of another class


Okay, before anything, I would like to mention that this is for school so please DO NOT write any code that fixes my code as that will not teach me anything. Instead what I am looking for is references, explanations and proper terminologies if I use incorrect terminology.

So I am having a few issues here. Here is what I need to do,

*Include the following methods in the Student class: a. an accessor (i.e., getter) for each instance variable from part B1 b. a mutator (i.e., setter) for each instance variable from part B1

Note: All access and change to the instance variables of the Student class should be through accessor and mutator methods.

c. constructor using all of the input parameters d. print() to print specific student data (e.g., student ID, first name, last name) using accessors (i.e., getters)

Create a student Roster class with the following methods that contain all ArrayList method calls: a. public static void remove(String studentID) that removes students from the roster by student ID

Note: If the student ID doesn’t exist, the method should print an error message indicating that it is not found.

b. public static void print_all() that prints a complete tab-separated list of student data using accessor methods

Note: Tabs can be formatted as such: 1 [tab] First Name: John [tab] Last Name: Smith [tab] Age: 20 [tab] Grades: {88, 79, 59}. The print_all() method should loop through all the students in the student array list and call the print() method for each student.

c. public static void print_average_grade(String studentID) that correctly prints a student’s average grade by student ID d. public static void print_invalid_emails() that verifies student e-mail addresses and displays all invalid e-mail addresses to the use*

That above is where I am having trouble, The arraylist, the constructor and calling the Student class in the Roster class.

here is my code.

Student Class

 /**
  * Write a description of class Student here.
  * 
  * @author (Richard Talcik) 
  * @version (v1 3/5/17)
  */
 public class Student {
  // initialize instance variables
  private String csFirstName;  //I am using cs infront of the name because this is a class variable and a string
  private String csLastName;
  private String csEmail;
  private int ciAge;
  private int ciStudentID;
  private int[] ciaGrades;   //cia for class, integer, array;

  public Student(String sFirstName, String sLastName, String sEmail, int iAge, int iStudentID, String[] grades) {

    //doing the consturctor
    this.setFirstName(sFirstName);
    this.setLastName(sLastName); 
    this.setEmail(sEmail); 
    this.setAge(iAge);
    this.setStudentID(iStudentID);
    this.setGrades(grades);


} 
  /**he methods to get and then followed by set
 */
public String getFirstName()
{
    // put your code here
    return csFirstName; //returning the value of the first name when called.

}
 public String getLastName()
{
    // put your code here
    return csLastName;

}
 public String getEmail()
{
    // put your code here
    return csEmail;

}
 public int getStudentID()
{
    // put your code here
    return ciStudentID;

}
 public int getAge()
{
    // put your code here
    return ciAge;

}
 public int[] getGrades()
{
    // put your code here
    return ciaGrades;

}

// calling the sets from here on out
 public void setFirstName(String newFirstName)
{
    // put your code here
    csFirstName = newFirstName;
    //setting it

}
public void setLastName(String newLastName)
{
    // put your code here
    csLastName = newLastName;
    //setting it

}
public void setEmail(String newEmail)
{
    // put your code here
    csEmail = newEmail;
    //setting it

}
public void setAge(int newAge)
{
    // put your code here
    ciAge = newAge;
    //setting it

}
public void setStudentID(int newStudentID)
{
    // put your code here
    ciStudentID = newStudentID;
    //setting it

}
public void setGrades(int[] newGrades)
{
    // put your code here
    ciaGrades = newGrades;
    //setting it

 }
}

This roster class is asking me to add arguments when I call Student stu = new Student(); But I don't understand what arguments to add into there? I also dont really understand how I am going to incorporate my arraylist to add my methods in there from the student class? (sorry if i used wrong terminology, please correct me if needed)

Roster Class

import java.util.ArrayList;

 /**
  * Write a description of class Roster here.
  * 
  * @author (Richard Talcik) 
  * @version (v1)
  */
 public class Roster {




// instance variables - replace the example below with your own


/**
 * Constructor for objects of class Roster
 */
 public static void main(String args[]) {
    Student stu = new Student("John", "Smith", "John1989@gmail.com", 20, 1, Grades[1]);
    ArrayList<Student> myRoster = new ArrayList<Student>();
    myRoster.add(new Student("Suzan", "Erickson","Erickson_1990@gmailcom", 19, 2, [91,72,85]));

 }
 public static void remove(String studentID)
{
    // put your code here

}

public static void print_all()
{
    //do something
}
public static void print_average_grade(String studentID)
{
    //do something
}

public static void print_invalid_emails()
{
    //do something
}


}

please any tutorials that will help explain this will be well worth it.

Also, I work third shift and my school is fully online. My mentor isn't or tutor isn't available during the hours that I am awake and emails are not really best method of communication as it takes days for a response.


Solution

  • Firstly for a beginner, not bad :) However when using the ArrayList object type in java, the type of the list must be of the object you whish to put inside the ArrayList, i.e. if you want to keep a list of students, then you have to write it as

    AssrayList<Student> = new ArrayList<Student>();
    

    you can also specify the size of the list as a param, but remember, ArraList grows dynamically as things are added and removed from the list.

    As far as you Constructor goes, you have to add the variables you assign values to on the inside as the params of the constructor. also the question states that you have to access all class objects with the use of their accessor and mutator methods, having that the current way you have your constructor at the moment is incorrect as you are directly assigning values to the class onjects of Student, you can change that to be similar to the following:

    constructor (paramType param) {
      mutatorMethod(param)
    }
    

    so your constructor should be in the lines of

    Student(String name, String surname) {
      setName(name)
      setSurname(surname)
    }
    

    Hope this helps :)