Quote:
Originally Posted by hEad
Student class (30 marks)
A student has a student number (8 digits, numbers only), first name (String), last name(String), gender (male or female)(String), date of birth (Gregorian Calendar), contact phone (10 digits) and the year (eg. 2009) they commenced the program (degree).
You should have get and set methods for all these data members. In addition to this, you must have a method printDetails() in the Student class.
|
OK here they tell you what to do, now you should wonder why they say that. They ask you to have get/set methods. Some people would call this ugly, but there's a reason why. Namely, these methods should be the only way of accessing the data... See below in commented code.
Quote:
Enrolment class (30 marks)
The Enrolment class needs to talk to the Student class. This class must have three methods, namely:
• addStudent()
• getStudent()
• printEnrolment()
|
That and nothing more I suppose.
Quote:
Admin class (30 marks)
The Admin class should drive the system. It should have a method fillData() . This method should create an instance of enrolment, populate the enrolment with at least 5 students and finally print it. It should also have a method called searchStudent(). This method allows searching for a student based on user input. This user input is asked at runtime on the command line, no menu implementation is needed. This method is repeated until a student is found in the enrolment. If a student is found, the program will terminate.
Finally, all classes except the driver class must have a constructor.
|
Ok so driver class will have the main() method, whose body is explained in English in the last sentences.
Quote:
This is what I have for Student so far...
public class Student {
public String studentID;
public String firstName;
public String lastName;
public String gender;
public String dateOfBirth;
public String phoneNumber;
public String yearCommenced;
|
NEVER EVER EVER use public data members.
They tell you to have set/get methods. So make these data private and the get/set methods public.
Why?
Because you may want to change the implementation of your Student class (typically you'll plug it to a different database). If you have public data members, you must rewrite the whole front-end, i.e. those who use the Student class.
Quote:
public Student(String sId, String fn, String ln, String sex, String dOB, String pn, String yc){
sId = studentID;
fn = firstName;
ln = lastName;
sex = gender;
dOB = dateOfBirth;
pn = phoneNumber;
yc = yearCommenced;
}
|
As already noted, it's studentID = sId; You should at least feed your class to a compiler, it would tell you some of your errors such as this one. Compilers are actually there to help you write code that works. Some may even say they are jsut limited testing programs.
Quote:
public String getStudentID(){
return studentID;
}
|
That's good, but now you also need to be able to set the student id.
Try to get all the set methods, and make sure your class compiles.
I'll simplify your assignment a bit, as you've already been simplifying it yourself without noticing:
1) Correct the class so that it compiles (namely, put the data members on the good sign of the "=" in the constructor)
2) Make all those data members private
3) Create one setXXX method for each data member and getXXX method you have. You can jsut have all of them manipulate strings for the moment.
4) Compile the thing again.
Once you have that, your Student class will be rich enough that it's usable. There are lots of things missing, like checking whether gender is 'male' or 'female', etc. but don't worry about these. Those should be the last things you do.
Now it would be nice if you could have a program that did something.
Personnally I'd start doing the driver class, but the Enrolment class is not that hard to do.
Create an Enrolment.java file and write it:
They tell you all it must have: A constructor (you don't need any data member initialisation so it's quite straightforward) and 3 methods. Not 4, not 2, three. You have their names but the guy who wrote the assignment was nasty in writing parenthesis afterwards.
In the summary table, you can read for instance that getStudent() retrieves a student given its index in an array. So getStudent must actually return a student and have an arrya index as argument.
Something like this:
Student getStudent( WhateverAnArrayIndexTypeMAyBe index) {WriteSomeWeirdCode();return theGoodStuden;}
Now they're telling you to store students in an array, so that should give you clues about the data members of the class (remember data members should be private).
Now you may have written Student and Enrolment class, but you won't have any idea if they work so you should start your Admin class and write a main() method. The main is described in the assignment: It must create an Enrolment, then call fillData on it. Just create ONE student to begin with.
If you manage to do that and have it work, the rest of the main method will be easy to write. Stop there, compile and try to run the program, fix it until it stops crashing. It won't do anything visible, but if it doesn't crash, it's already a good step forward.
Next step is to tell the main() to print the students data on screen. Note printEnrolment prints stuff on the screen so you may want to call that method. Compile. Run. Fix until it no longer crashes and until it outputs the data you want on the command line.
If you get to that point, you will have advanced a lot. You'll then have to make sure
1) you can handle several students
2) you implement the searchStudent method
3) It prints what you want, no more, no less and not some other student, in answer to searchStudent
4) You can start making sure that genders are male/female, phone numbers are numbers and not made of 4 digits and other type-checks.
Next you'll want to call