Reputation: 19
I have a Main.java file and inside this class I have created an ArrayList of student objects. I want to pass these objects to another class, in another package called IntroSentenceHigh.
Here is my code from my Main.java file:
public class Main
{
public static void main(String[] args)
{
ArrayList<Student> StudentList = new ArrayList<Student>();
// Student details --- Name, Year Level, Gender, Average Test Score, Exam Score, Classwork, Homework, Practical Work, Behaviour
Student student1 = new Student("Bob", 12, "M", 100, 100, 1, 1, 1, 1);
Student student1 = new Student("John", 12, "M", 100, 100, 1, 1, 1, 1);
StudentList.add(student1);
StudentList.add(student2);
// Loop through all student data in the array
for (int i = 0; i < StudentList.size(); i++)
{
System.out.println(StudentList.get(i).viewStudentInfo());
}
IntroSentenceHigh introSentenceHigh = new IntroSentenceHigh();
I have a Student.java class which contains the following:
public class Student
{
private String name;
private int yearLevel;
private String gender;
// Student marks
private int testAverage;
private int exam;
// Other student data
private int classwork;
private int homework;
private int practicalWork;
private int behaviour;
public Student(String name, int yearLevel, String gender, int testAverage, int exam, int classwork,
int homework, int practicalWork, int behaviour)
{
this.name = name;
this.yearLevel = yearLevel;
this.gender = gender;
this.testAverage = testAverage;
this.exam = exam;
// Student's are given scores from 1 to 3.
// 1 is best, 3 is worst
this.classwork = classwork;
this.homework = homework;
this.practicalWork = practicalWork;
this.behaviour = behaviour;
}
public String getName()
{
return name;
}
public int getYearLevel()
{
return yearLevel;
}
public String getGender()
{
return gender;
}
public int getTestAverage()
{
return testAverage;
}
public int getExam()
{
return exam;
}
public int getClasswork()
{
return classwork;
}
public int getHomework()
{
return homework;
}
public int getPracticalWork()
{
return practicalWork;
}
public int getBehaviour()
{
return behaviour;
}
public String viewStudentInfo()
{
String studentInfo;
studentInfo = "Student name: " + getName() + "\n" +
"Year: " + getYearLevel() + "\n" +
"Gender: " + getGender() + "\n" +
"Test Average: " + getTestAverage() + "\n" +
"Exam result: " + getExam()+
"\n";
return studentInfo;
}
}
Finally, I have a IntroSentenceHigh.java class with the following code:
public class IntroSentenceHigh
{
private String introSentenceHigh1 = "Student statement 1";
private String introSentenceHigh2 = "Student statement 2";
public String getIntroSentenceHigh1()
{
return introSentenceHigh1;
}
public String getIntroSentenceHigh2()
{
return introSentenceHigh2;
}
Basically, I want to access a Student name and gender within the IntroSentenceHigh class, but I don't know how to get it from my Main.java class where I created the ArrayList. I looked for answers on the same topic but as I'm a beginner I don't know how to modify my code to make it work.
Any help would be awesome. Thanks!
Upvotes: 1
Views: 133
Reputation: 186
There are many ways to do exactly what you want, the easiest probably being the use of a parameterized constructer. This means adding an ArrayList parameter to your IntroSentenceHigh class (which is not a very intuitive name by the way), and setting a global variable in that class equal to the ArrayList. This would be one way to do it:
public Class IntroSentenceHigh {
private ArrayList<Student> studentList;
public IntroSentenceHigh(ArrayList<Student> studentList) {
this.studentList = studentList
}
Now, your global variable studentList contains the ArrayList of Students you created in Main. You can then use the getters you declared in your Student class to receive the required information. An example being:
String aName = studentList.get(0).getName();
Remember to add the ArrayList parameter when you create an instance of IntroSentenceHigh (luckily, the compiler will remind you of that requirement as well ;))
Upvotes: 1
Reputation: 2968
Add a constructor to IntroSentenceHigh
with a paramater : your ArrayList
and create an instance of IntroSentenceHigh
as follow new IntroSentenceHigh(StudentList )
Upvotes: 0