Well it works, but the way you have chosen to do the project is the
2nd way I described which I said is actually not a good way of doing it.
As I already mentioned it's better to create the StudentRecord
instances inside the Student class constructor, for example:
public Student (String name, double grade) {
//increase student count
studentCount++;
//create a new student record
studentRecord = new StudentRecord(name, grade);
}
That way you avoid having to supply the StudentRecord instances
whenever you create a new Student instance because it happens
automatically, and in this case the studentCount is also incremented
automatically.
The other thing I noticed was how you access the name field in each
StudentRecord instance. It would be better to declare the name field
as private, and access it like this:
student2.getStudentRecord().getName();
This uses one of the StudentRecord methods that you commented out.
On 20/11/2009, at 7:29 PM, nn roh wrote:
> Hi ,
>
> Can you look at the program ,now it works fine .
>
> Thanks
>
> Nada
>
>
>
>
> On Fri, Nov 20, 2009 at 4:09 AM, Nic Fox <[email protected]> wrote:
> First up, your code only creates 1 StudentRecord instance, which all
> the Student instances use. You could fix that easily and get the
> project working although...
>
> The other issue is that you have failed to use composition e.g. a
> Student has a StudentRecord, so you should really be creating your
> StudentRecord instances to be part of each Student instance by
> putting the code to declare and create a StudentRecord in the
> Student class, not in the StudentRecordExample class where the main
> method is. You already have the studentRecord instance variable
> there in the Student class, you just need to add the instantiating
> statement to a constructor (which is missing at the moment).
>
> Alternatively you could create the instances of each StudentRecord
> in the main method and pass them to each Student instance through
> the Student object constructors or some other method e.g. public
> void addStudentRecord (StudentRecord sR), but that approach isn't a
> very good way of doing it.
>
> The reason I mention this 2nd approach (which is not the best
> approach in my opinion) is that it's good practice to think of a
> number of different ways of coding something, and then evaluate each
> to see what their advantages and disadvantages are. In the above
> case, the second example requires 2 separate tasks to be performed
> and that they're performed in a specific order before you have a
> usable Student instance. It's an example of tight coupling between
> classes, and tight coupling is a bad thing. As you learn more about
> the Object Oriented side of programming and Java you'll come across
> these sorts of topics which are concerned with how to model objects
> and systems.
>
> Hope this all helps and isn't too confusing.
>
>
> On 20/11/2009, at 6:48 AM, Rammohan Vadlamani wrote:
>
>> <MyStudentRecordExampleProject2.zip>
>
>
> --
> To post to this group, send email to
> [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>
> <MyStudentRecordExampleProject2_091120-112744.zip>
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en