Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Using collections with user made data types

JesterGorkyJesterGorky Posts: 2Member
Arighty boys 'n' girls,

Im having a wee bito a problem with trying to get the collections to work with my user created data types, Im able to get collections working with just standard data type(ints, floats, etc) but when it comes to applying it to my coursework project I have to say Im at an end. Could anyone help me out with understanding how to get this to work?

See there's 3 parts to the project, the main with is the launcher, theres also a unireg class & a student class, there can only ever be 1 uniReg class object but multiple student objects. We're not permitted to use arrays as my lecturer doesnt believe in them & I agree the are useful for some things but not others, but I digress. Below is the code for the student object as this is where the trouble is...

[code]package uniReg;

import textio.ConsoleIO;

public class uniReg {

student st1 = new student();//Creates new student object

public void addStudent(){//Call to fill student object fields
st1.setMatriculationNumber();
st1.setName();
st1.setAge();
st1.setGender();
st1.setCode();
st1.setTitle();
st1.setStage();
st1.setStudentTutor();
}

public void displayAll(){//Displays all fields of the student object -
//Full version will also display the average age
//of the student population

ConsoleIO.out.print("
Matriculation Number: " + st1.getMatriculationNumber());
ConsoleIO.out.print("
Name: " + st1.getName());
ConsoleIO.out.print("
Age: " + st1.getAge());
ConsoleIO.out.print("
Gender: " + st1.getGender());
ConsoleIO.out.print("
Course Code: " + st1.getCourseCode());
ConsoleIO.out.print("
Course Title: " + st1.getCourseTitle());
ConsoleIO.out.print("
Course Stage: " + st1.getCourseStage());
ConsoleIO.out.print("
Year Tutor: " + st1.getStudentTutor());
}

public void totalNumStudents(){//Calculates the total number of students -
//also displays the gender balance of the student population
/*Once selected the method will run through & count up the number of student objects that are held
* in the register.
*
* 1. Run through student collection - increase counter with each iteration - also counts gender population
* 2. Output total of students
* 3. Output gender population of student body*/
}

public void displayStudentClassList(int programme, int year){//Displays the students of a given
//year & programme of study
/*The user is required to enter a year & a programme of study, once entered users search criteria will then
* be matched to that of any student object resulting in the output which contains the student objects of
* the given year & programme.
*
* 1. Get year
* 2. Get programme
* 3. Search through student object collection
* 4. Flag & output matching student objects*/
}

public void tutorStudentList(String tutorName){//Displays the students who are supervised by the given tutor
/*User is asked to enetr the tutors name, once entered the name will be checked against every student
* object held in the registry. These matching students will then be out[ut to the screen for the user
*
* 1. Get tutor name
* 2. Run through populated collection of student objects
* 3. Output matching students*/
}
}[/code]

As you can see I havent imported the java.util.* yet as I actually butchered the first version tryin to apply my understanding of how the collections work.

At this section I know I need to create the collection

[code]student st1 = new student();//Creates new student object[/code]

And in this method I need to enter the data into the collection but Im unsure of how to go about doing this

[code]public void addStudent(){//Call to fill student object fields
st1.setMatriculationNumber();
st1.setName();
st1.setAge();
st1.setGender();
st1.setCode();
st1.setTitle();
st1.setStage();
st1.setStudentTutor();
}[/code]

I would be highly greatful to any input that anyone can give to this problem.

Also note that the '1' will not be in the name of the finial version & yes I know there's no error checking this is a basic prototype :P hehe.

And incase anyone's wondering Im using eclipse IDE

Anyways thanks in advance

JG

Comments

  • pahpah Posts: 83Member
    : Arighty boys 'n' girls,
    :
    : Im having a wee bito a problem with trying to get the collections to work with my user created data types, Im able to get collections working with just standard data type(ints, floats, etc) but when it comes to applying it to my coursework project I have to say Im at an end. Could anyone help me out with understanding how to get this to work?
    :
    : See there's 3 parts to the project, the main with is the launcher, theres also a unireg class & a student class, there can only ever be 1 uniReg class object but multiple student objects. We're not permitted to use arrays as my lecturer doesnt believe in them & I agree the are useful for some things but not others, but I digress. Below is the code for the student object as this is where the trouble is...
    :
    : [code]package uniReg;
    :
    : import textio.ConsoleIO;
    :
    : public class uniReg {
    :
    : student st1 = new student();//Creates new student object
    :
    : public void addStudent(){//Call to fill student object fields
    : st1.setMatriculationNumber();
    : st1.setName();
    : st1.setAge();
    : st1.setGender();
    : st1.setCode();
    : st1.setTitle();
    : st1.setStage();
    : st1.setStudentTutor();
    : }
    :
    : public void displayAll(){//Displays all fields of the student object -
    : //Full version will also display the average age
    : //of the student population
    :
    : ConsoleIO.out.print("
    Matriculation Number: " + st1.getMatriculationNumber());
    : ConsoleIO.out.print("
    Name: " + st1.getName());
    : ConsoleIO.out.print("
    Age: " + st1.getAge());
    : ConsoleIO.out.print("
    Gender: " + st1.getGender());
    : ConsoleIO.out.print("
    Course Code: " + st1.getCourseCode());
    : ConsoleIO.out.print("
    Course Title: " + st1.getCourseTitle());
    : ConsoleIO.out.print("
    Course Stage: " + st1.getCourseStage());
    : ConsoleIO.out.print("
    Year Tutor: " + st1.getStudentTutor());
    : }
    :
    : public void totalNumStudents(){//Calculates the total number of students -
    : //also displays the gender balance of the student population
    : /*Once selected the method will run through & count up the number of student objects that are held
    : * in the register.
    : *
    : * 1. Run through student collection - increase counter with each iteration - also counts gender population
    : * 2. Output total of students
    : * 3. Output gender population of student body*/
    : }
    :
    : public void displayStudentClassList(int programme, int year){//Displays the students of a given
    : //year & programme of study
    : /*The user is required to enter a year & a programme of study, once entered users search criteria will then
    : * be matched to that of any student object resulting in the output which contains the student objects of
    : * the given year & programme.
    : *
    : * 1. Get year
    : * 2. Get programme
    : * 3. Search through student object collection
    : * 4. Flag & output matching student objects*/
    : }
    :
    : public void tutorStudentList(String tutorName){//Displays the students who are supervised by the given tutor
    : /*User is asked to enetr the tutors name, once entered the name will be checked against every student
    : * object held in the registry. These matching students will then be out[ut to the screen for the user
    : *
    : * 1. Get tutor name
    : * 2. Run through populated collection of student objects
    : * 3. Output matching students*/
    : }
    : }[/code]
    :
    : As you can see I havent imported the java.util.* yet as I actually butchered the first version tryin to apply my understanding of how the collections work.
    :
    : At this section I know I need to create the collection
    [red] As i can see this is a single object not a collection of
    obejcts this means only one real person[/red]
    :
    : [code]student st1 = new student();//Creates new student object[/code]
    :
    : And in this method I need to enter the data into the collection but Im unsure of how to go about doing this
    [red] you have to look in the student object how you do that

    lets say you have private String,int parameters then you have to pass the value in the argument of the funcitons of the student object below.
    st1.setMatriculationNumber(int nMar){
    [blue]this is the private class variable m_nMartricul [/blue] = nMar;
    }
    [/red]
    :
    : [code]public void addStudent(){//Call to fill student object fields
    : st1.setMatriculationNumber();
    : st1.setName();
    : st1.setAge();
    : st1.setGender();
    : st1.setCode();
    : st1.setTitle();
    : st1.setStage();
    : st1.setStudentTutor();
    : }[/code]
    :
    : I would be highly greatful to any input that anyone can give to this problem.
    :
    : Also note that the '1' will not be in the name of the finial version & yes I know there's no error checking this is a basic prototype :P hehe.
    :
    : And incase anyone's wondering Im using eclipse IDE
    :
    : Anyways thanks in advance
    :
    : JG
    :
    If i have undersood it right you want to write a code that handles students ! This means a number of students??!!! If it is so your code generates only one student object this means you have only one student and not a collection of students as you have written. You can generate more than one student object dynamicly but then you habe to collect and to handle this object STUDENT OBJECT because every student is one real person.

    Long story short meaning YOU NEED TO USE
    ----> ARRAYS
    OR
    ----> VECTORS
    OR
    ----> HASHTABLE
    one of this can hanle a object it doesnt metter what kind of object this means a String, or a Student both are objects and can be handled buy this methodes ARRAY, VECTOR, HASHTABLE.

    The only thing you have to look for is the that you need to typecast the object after you want to use them if you take them out of the VECTOR, or HASHTABLE. If you use a array of Students then you dont have to typecast the object!

    This means if your teacher hates arrays, what i cant understand then use the hashtable there you can save the object under a key the key could be the martikal number of the studend or the name of the student if there are not two names the same. Because the key has to be unique.

    [code]
    import java.util.*;

    public class foo
    {
    public static void main(String[] args)
    {
    Hashtable students = new Hashtable();

    //adding of the students how many you want this can be
    //called a collection
    students .put("Student1",new student());
    students .put("Student2",new student());

    //Give the students informations out...
    Enumeration e = students.keys();
    while (e.hasMoreElements()) {
    String a = (String)e.nextElement();
    student st1 = (student) students.get(a);

    System.out.println(
    a + " --> " + st1.getAgeORSOMTHINLIKE THIS
    );
    }
    }
    }
    [/code]

    I hope i have understood the question of you right. And it helps a little bit.
Sign In or Register to comment.