3 Replies - 546 Views - Last Post: 23 October 2012 - 08:24 PM Rate Topic: -----

#1 shyone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 16-October 12

Add a call to a method in a specific class default constructor

Posted 23 October 2012 - 07:57 PM

My instructions tell me that my next step is to 'add a call to createStudents in the Roster default constuctor.' This is what I have in class Roster right now.
public Roster() {
	// Instantiate the ArrayList of students
	ArrayList<Student> students= new ArrayList<Student>();
		
		
	}

	
	//
	// Operations
	//

	
	
	//
	// Helper methods
	//
	
	private void createStudents() {
		// Create students for the ArrayList
		Student aStudent= new Student("Bugs","Bunny",98);
		Student bStudent= new Student("Daffy","Duck",77);
		Student cStudent= new Student("Elmer", "Fudd",85);
		Student dStudent= new Student();
		
		//Add each student to the students ArrayList
		students.add(aStudent);
		students.add(bStudent);
		students.add(cStudent);
		students.add(dStudent);
		
		// Use the mutator methods of the Student class to set the fourth Student
		// to be "Porky", "Pig", 92
		students.get(3).setFirstName("Porky");
		students.get(3).setLastName("Pig");
		students.get(3).setStudGrade(92);
		
	

	} 


My problem is, I have no idea what he is asking me to do here.
The student class presently looks like this...
public class Student {

	//
	// Data members 
	//
	private String firstName;
	private String lastName;
	private int studGrade;
	//
	// Constructors
	//
	
	/**
	 * Initializes the student data to null or 0.
	 */
	public Student() {
		this.firstName = null;
		this.lastName = null;
		this.studGrade = 0;
	}

	/**
	 * Initializes the first and last name of the student to 
	 * the value of the corresponding parameters.
	 * 
	 * @param firstName The student's first name.
	 * @param lastName  The student's last name.
	 * @param studGrade The student's grade.
	 */
	public Student(String firstName, String lastName, int studGrade) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.studGrade = studGrade;


	}
	
	// 
	// Operations
	//
	
	
	//
	// Accessors and mutators
	//

	/**
	 * Accessor for student's first name.
	 * 
	 * @return The first name of the student.
	 */
	public String getFirstName() {
		return this.firstName;
	}

	/**
	 * Mutator to set the students first name.
	 * 
	 * @param firstName The first name to set.
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * Accessor for student's last name.
	 * 
	 * @return The last name of the student.
	 */
	public String getLastName() {
		return this.lastName;
	}

	/**
	 * Mutator to set the students last name.
	 * 
	 * @param lastName The last name to set.
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	/**
	 * Accessor for student's grade.
	 * 
	 * @return The grade for the student.
	 */
	public int getStudGrade() {
		return this.studGrade;
	}
	/**
	 * Mutator to set the student's grade.
	 * 
	 * @param studGrade The grade to set.
	 */
	public void setStudGrade(int studGrade) {
		this.studGrade = studGrade;
	}

} 


I know this is problem a simple thing, but again, I am clueless as to what to put in before I can move on with this task.

Is This A Good Question/Topic? 0
  • +

Replies To: Add a call to a method in a specific class default constructor

#2 fromTheSprawl   User is offline

  • Bloodborne
  • member icon

Reputation: 523
  • View blog
  • Posts: 2,102
  • Joined: 28-December 10

Re: Add a call to a method in a specific class default constructor

Posted 23 October 2012 - 08:04 PM

In your constructor just add the createStudents method. Basically, it's just one line that needs to be added. This will create the students for the class to use when the object is instantiated. It's the same as calling a method in your main method, or in other methods. ^^
Was This Post Helpful? 1
  • +
  • -

#3 shyone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 16-October 12

Re: Add a call to a method in a specific class default constructor

Posted 23 October 2012 - 08:10 PM

I mean am I just going to code
createStudents();



Or is there something more to it. I don't really understand still what they mean by 'call.' Our textbook covers ArrayList in 1.5 short pages front and back with one code example so...yeah...google is my friend but I am still unsure of what to do.
Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl   User is offline

  • Bloodborne
  • member icon

Reputation: 523
  • View blog
  • Posts: 2,102
  • Joined: 28-December 10

Re: Add a call to a method in a specific class default constructor

Posted 23 October 2012 - 08:24 PM

createStudents already exist on the same class, you only need to put the line you posted to your constructor. That's it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1