class Student{
public byte count = 0;
public String id;
public String name;
public String score = " ";
public char sex;
public int age;
public int labs[] = new int [12];
public Student(){
}
public void setName(String newName){
name = newName;
}
public void setSex(char newSex){
sex = newSex;
}
public void setAge(int newAge){
if(newAge < 100 && newAge > 0)
age = newAge;
else
System.out.println("Invalid Age");
}
public void setId(String newId){
if(newId.length() != 9)
System.out.println("Invalid ID. Must be 9 digits long");
else
id = newId;
}
public void setScore(byte labNum, byte score){
labs[labNum] = score;
}
}//end of student class
Adding Objects to an array
Page 1 of 14 Replies - 446 Views - Last Post: 30 November 2010 - 11:29 PM
#1
Adding Objects to an array
Posted 30 November 2010 - 10:29 PM
Ok, so i have to write a class where I can create list of students and add them to an array. I was wondering if i could make an array of objects and just add the students to that array or am i going to have to create another class? Thanks in advance.
Replies To: Adding Objects to an array
#2
Re: Adding Objects to an array
Posted 30 November 2010 - 11:09 PM
Best practice would be to design a class, like a School class, to encapsulate a Student[].
#3
Re: Adding Objects to an array
Posted 30 November 2010 - 11:15 PM
Ok, but how will i able to make a student and set the students id and lab scores if I made a School class?
#4
Re: Adding Objects to an array
Posted 30 November 2010 - 11:21 PM
A small example below:
I cover a basic inventory management system in my tutorial Moving Away From Parallel Arrays using a similar setup as yours. You might want to check out my tutorial.
//create your array, which stores numStudentsYouWant
//null variables
Student[] s = new Studen[numStudentsYouWant];
//instantiate a new Student
s[0] = new Student();
//and set it's name to 0
s[0].setName("Name");
I cover a basic inventory management system in my tutorial Moving Away From Parallel Arrays using a similar setup as yours. You might want to check out my tutorial.
#5
Re: Adding Objects to an array
Posted 30 November 2010 - 11:29 PM
Ok, thanks for your help and I will definitely check out your tutorial.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|