Hello,

I am stuck on this issue since like a week now and i have been trying to find out how to do this.

I am trying to copy the ArrayList elements into another class.

I have 4 classes:

registration class (MAIN CLASS)
address class (DONE)
student class (DONE (kinda))
course class (PROBLEM)

I have done everything but just cant seem to copy the arraylist of STUDENT class to the arraylist of the COURSE class. Because COURSE class have to use arraylist from the STUDENT class in order to keep track of how many students have been added to the list.

I am using arraylist in STUDENT class to store student data coming from registration class and then show the students test scores and their names and then i have to pass this to COURSE class so course class can keep track of courses and number of students have been added.

Please help me out i am STUCK. MAINLY on how to pass arraylist of STUDENT CLASS to the arraylist of COURSE CLASS.

Here is the code for all 4 classes.

REGISTRATION CLASS (MAIN CLASS)

import java.util.*;

public class Registration
{
	public static void main(String[] args) 
	{
		Address home1 = new Address("13145", " surrey", "B.C", 12345);
		Address school1 = new Address("72 and 132", " surrey", "B.C", 12345);		
		
		Student student1 = new Student("Burhan1", "Akram1", home1, school1, 100, 100, 100);	
	    Student student2 = new Student("Burhan2", "Akram2", home1, school1, 100, 100, 100);
	    Student student3 = new Student("Burhan3", "Akram3", home1, school1, 100, 100, 100);	
	    Student student4 = new Student("Burhan4", "Akram4", home1, school1, 100, 100, 100);
	    Student student5 = new Student("Burhan5", "Akram5", home1, school1, 100, 100, 100);
	    
	    student1.setTestScore(0, 60);
	    student1.setTestScore(1, 24);
	    student1.setTestScore(2, 94);
	    
	    student2.setTestScore(0, 73);
	    student2.setTestScore(1, 67);
	    student2.setTestScore(2, 81);
	    
	    student3.setTestScore(0, 80);
	    student3.setTestScore(1, 90);
	    student3.setTestScore(2, 59);
	    
	    student4.setTestScore(0, 92);
	    student4.setTestScore(1, 89);
	    student4.setTestScore(2, 63);
	    
	    student5.setTestScore(0, 77);
	    student5.setTestScore(1, 68);
	    student5.setTestScore(2, 83);
	    
		Course course1 = new Course("Physics");
		Course course2 = new Course("Chemistry");
		Course course3 = new Course("Math");
		Course course4 = new Course("Computer Science");
		Course course5 = new Course("Data Managment");
		
	    
	    System.out.println(student1.toString() + "\n" + "Course Taken: " + course1.toString() + "\n");
	    System.out.println(student2.toString() + "\n" + "Course Taken: " + course2.toString() + "\n");
	    System.out.println(student3.toString() + "\n" + "Course Taken: " + course3.toString() + "\n");
	    System.out.println(student4.toString() + "\n" + "Course Taken: " + course4.toString() + "\n");	
	    System.out.println(student5.toString() + "\n" + "Course Taken: " + course5.toString() + "\n");
	}
}

address class (no problem here)

//Programmer: Burhan Akram
// Section: S10
//Purpose: To provide main method information

public class Address // address class
{
	// variables to use in constructors
    private String streetAddress, city, state;
    private long zipCode;

    public Address(String street, String town, String st, long zip) // constructor to accept the values
    {
    	// Initialized values
    	streetAddress = street;
    	city = town;
    	state = st;
    	zipCode = zip;
    }

    public String toString() // toString method to show the results
    {
    	String result;

    	result = streetAddress;
    	result += city + ", " + state + " " + zipCode;

    	return result;
    }
}

Student CLASS (have to pass arraylist from this class to course class)

//Programmer: Burhan Akram
//Section: S10
//Purpose: To provide main method information

 import java.util.*;
 
public class Student // student class
{
	//Variables to use in constructors
	private String firstName, lastName;
	private Address homeAddress, schoolAddress;
	
	private ArrayList<Integer> test;
	

public Student (String first, String last, Address home, Address school, 
               int tst1, int tst2, int tst3) // constructor to accept values
{
	test = new ArrayList<Integer>();
	//variable to initialized the values
	firstName = first;
	lastName = last;
	homeAddress = home;
	schoolAddress = school;
	test.add(tst1);
	test.add(tst2);
	test.add(tst3);
}

public Student() // sets test values to always to ZERO after each cycle of each student
{
	/*for(int count = 0; count < test.size(); count++)
	{
		test.set(count, 0);
	}*/
}

public void setTestScore(int testNumber, int testScore) // accepts test parameter from the main class
{
	// loops through the each test number for each student to change the test score from the original test score
	while(testNumber < test.size())
	{
		test.set(testNumber, testScore);
		
		testNumber++;
	}
	
	// another way to set the tests but not as efficient as with a loop above
	/*if(testNumber == 1)
	{
		test.set(0, testScore);
	}
	else if(testNumber == 2)
	{
		test.set(1, testScore);
	}
	else if(testNumber == 3)
	{
		test.set(2, testScore);
	}
	*/
}

public int getTestScore(int testNumber) // takes the test number and returns the appropriate score
{	
	// loops through the each test number and returns the score
	for(int count = 0; count < test.size(); count++)
	{
		testNumber = test.get(count);
	}
	
	return testNumber;
}

public int average() // returns the average of tests for each student
{ 
	int result = 0; // counter variable
	
	// loops through each test number, add them and divide them with the total number of tests available
	for(int count = 0; count < test.size(); count++)
	{
		result = (test.get(0) + test.get(1) + test.get(2)) / test.size();
	}
	
	return result;
}

public String toString() //toString method to return the results to main class
{
	String result;
	
	result = firstName + " " + lastName + "\n";
	result += "Home Address: " + homeAddress + "\n";
	result += "School Address: " + schoolAddress + "\n";
	result += "Average = " + average() + " " + " Tests: " + test;

	return result;
}
}

COURSE CLASS (TROUBLING CLASS)

import java.util.*;

public class Course 
{
	private String courseName;
	
	private ArrayList<Student> studentList = new ArrayList<Student>();
	
	
	public Course(String crName)
	{
		courseName = crName;
	}
	
	public void addStudent(Student studentAdd) // keep track of students?
	{
		for(int count = 0; count < studentList.size(); count++)
		{
		studentAdd = studentList.get(count);
		}
	}
	
	public int average() // returns the average of all students' test score averages
	{

	}
	
	public void roll() // prints student names
	{

	}
	
	public String toString()
	{
		String result = "";
		
		result = courseName + "\n";
		result += studentList;
		result += "\n" + average();
		
		return result;
	}
}

Thank you in advance.

why would you want to do that? it's not correct and your course class already has that information.
that arrayList is stored in the Student objects you have in your studentList ArrayList

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.