8 Replies - 5919 Views - Last Post: 01 April 2010 - 06:49 AM Rate Topic: -----

#1 need_helpp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

Passing list of array to another class

Posted 31 March 2010 - 11:51 AM

I have a class called Student which has student no, first name, last name.All the instances are private so I have get and set methods for them. I also have a toString method.

I have another class called University.It has university name and address which are private also so I have get and set methods. Also it should have studentList an array of objects of type Student.Create a method called printStudentList that will print the array of students by calling the toString method of Student

And the class UniversityTest should ask user to input student info. Then print the list of students using printStudentList , this will print array of Students by calling toString method in Student object.

I don't know how to pass list of students to University (studentList)

Student
public class Student {
	
private String firstName;
private String lastName;
private String studentNumber;

   	Student(String fName,String lName,String sNumber) 
	{
		firstName=fName;  
		lastName=lName;  
		studentNumber=sNumber;
		
	public Student() 
	{		
	}
	
	public void setFirstName(String fName)   
	{
	firstName=fName;  
	} 
		public String getFirstName()    
	{
		return firstName;
		
	}
	
	public void setLastName(String lName)  
	{
	lastName=lName;   
		
	}	
	
	public String getLastName() 
	{
		return lastName;
		
	}
	
	public void setStudentNumber(String sNumber)  
	{
	studentNumber=sNumber;  
		
	} 
	public String getStudentNumber()
	{
	return studentNumber;
		
	}
	 @Override
	public String toString() 
	{
		return String.format("%-8s %-9s %-8s\n",firstName,lastName,studentNumber );
	}	
}



University

public class University {
	
	private String universityName;
	private String address;
	Student studentList [];
	
    University (String uName,String uAddr) 
    {
	universityName=uName;  
	address=uAddr;  
	
    }
    public University()
    {	
    }
    
    public void setUniversityName(String uName)  
    {
	universityName=uName;  
    } 
    
    public String getUniversityName()   
    {
	return universityName;
	
    }
    public void setAddress(String uAddr)  
    {
    address=uAddr;   
    }

    public String toString() 
    {
	return String.format("%-30s %-20s\n",universityName,address);
    }	
} 



UniversityTest

import java.util.Arrays;
import java.util.Scanner;

public class UniversityTest {
	public static void main( String[] args)
	{   System.out.println("Please enter University Name");
	  
            University university = new University();
		
	    Scanner input=new Scanner(System.in);
	    String uName=input.nextLine();
	    System.out.println("Please enter University Address");
	    String uAddr=input.nextLine();
	    university=new University(uName,uAddr);
            System.out.print(university);
		    
		
		  
		  Student[] students = new Student[3]; // Create 5 new students
		  Scanner inputs = new Scanner(System.in);
		  for(int i = 0 ; i < students.length; i++)
	      { 
	      
	      System.out.println("Please enter Student Number");
	      String sNumber = inputs.nextLine(); 
	      System.out.println("Please enter Student First Name");
	      String fName = inputs.nextLine(); 
	      System.out.println("Please enter Student Last Name");
	      String lName = inputs.nextLine(); 
	      students[i] = new Student(sNumber,fName, lName);
	      	
 }



Is This A Good Question/Topic? 1
  • +

Replies To: Passing list of array to another class

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Passing list of array to another class

Posted 31 March 2010 - 12:32 PM

Just use the University object that you have in the tester and use the printStudentList method which you need to create. It should be a loop in the university class that cycles through the array of students calling each Student objects toString() and printing it.
Was This Post Helpful? 1
  • +
  • -

#3 nbroemm1  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 28-September 09

Re: Passing list of array to another class

Posted 31 March 2010 - 12:56 PM

Pass it in the parameterized constructor of University.

   	public University(String uName, String uAdress, Student[] studentArray) {
         	setUniversityName(uName);
         	setAddress(uAdress);
         	setStudentList(studentArray);
	}

	public void setStudentList(Student[] studentArray) {
        	studentList = new   Student[studentArray.length];
        	for(int i = 0; i < studentArray.length; i++) {
			studentList[i] = studentArray[i];
         	}
	}



There are other problems, but this should get you in the right direction.
Was This Post Helpful? 1
  • +
  • -

#4 need_helpp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

Re: Passing list of array to another class

Posted 31 March 2010 - 01:22 PM

So after adding studentArray in the University class:
public static void main( String[] args)
	{   System.out.println("Please enter University Name");
	   
	        
		    University university = new University();
		
			Scanner input=new Scanner(System.in);
			String uName=input.nextLine();
			System.out.println("Please enter University Address");
			String uAddr=input.nextLine();
			university=new University(uName,uAddr,[b]Student[] studentArray[/b]);
		    System.out.print(university);


how should i fix the test file for three-argument constructor?

This post has been edited by need_helpp: 31 March 2010 - 01:23 PM

Was This Post Helpful? 1
  • +
  • -

#5 nbroemm1  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 28-September 09

Re: Passing list of array to another class

Posted 31 March 2010 - 07:30 PM

Sorry I was at work and couldn't reply.
If you are still stuck it is
University u1 = new University(uName, uAddr, students);

Was This Post Helpful? 1
  • +
  • -

#6 need_helpp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

Re: Passing list of array to another class

Posted 31 March 2010 - 08:38 PM

View Postnbroemm1, on 31 March 2010 - 06:30 PM, said:

Sorry I was at work and couldn't reply.
If you are still stuck it is
University u1 = new University(uName, uAddr, students);


when I put students, it says create a local variable. It doesn't read students.
Was This Post Helpful? 1
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,522
  • Joined: 27-December 08

Re: Passing list of array to another class

Posted 01 April 2010 - 05:29 AM

Can you post your code? Also, can you tell us what code is on the line you are getting an error at?
Was This Post Helpful? 0
  • +
  • -

#8 need_helpp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

Re: Passing list of array to another class

Posted 01 April 2010 - 06:12 AM

View Postmacosxnerd101, on 01 April 2010 - 04:29 AM, said:

Can you post your code? Also, can you tell us what code is on the line you are getting an error at?



University u1 = new University(uName, uAddr, students);

This is the only difference from the previous code. And the students gives an error
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,522
  • Joined: 27-December 08

Re: Passing list of array to another class

Posted 01 April 2010 - 06:49 AM

So where do you declare and initialize students? Are you getting a "cannot find symbol" error? This is why we would like to see more code. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1