On Aug 31, 7:52 am, "Stephen Hunter" <[EMAIL PROTECTED]> wrote:
> Okay, I am pretty sure that I have done everything that is expected to be
> done, except I am having trouble with the studentId.
> I need suggestions, help, answers, code snip-its; I just need help
>
> Here is what is asked to do in the homework:
>
> 1. The homework is to either modify MyStudentRecordExampleProject NetBeans
> project you've done in Exercise 1 above or create a new project as
> following. (You might want to create a new project by copying the
> MyStudentRecordExampleProject project. You can name the homework project in
> any way you want but here I am going to call it MyOwnProject.)
>
> - Create a Student class as following:
> - The Student class has StudentRecord class as an instance variable.
> Name it as studentRecord.
You have not a StudentRecord instance invariable in Student, that is
you create a new StudentRecord, that is not the same.
Just put it as did with StudentId (though see below the remark about
StudentId).
> - You can use the StudentRecord class from the
> MyStudentRecordExampleProject above or you can create a new
> one - the only
> requirement is that it has to have at least one instance
> variable of its
> own.
> - The Student class has studentId instance variable whose
> type is Integer
> type.
You have not an Integer type in your Student class, but an int. That
is not the same.
Replace int by Integer and it would be OK.
Also don't initialize it to 0.
A note: keep together the private instances, then protected instances,
then static instances, then your constructors, then the methods which
you can in turn organize by public, static, private.
It would save you a lot of troubles, and help you understand the logic
behind it.
Another note: avoid initialize variables of a class, unless absolutely
needed, which is generally the case for static variables.
You are missing also constructors for Student, you have only the
default one, which impedes you to initialize directly from the Main
class (that is StudentRecordExample) any Student correctly
initialized.
Well, you can do as you did, but then you loose the benefit of the
exercise, which is to understand how you can play with constructors,
sort of beginning to understand how the classes hierarchy works.
So you may add some constructors (something like this, adapt it) :
public Student (Integer studentId, String name, double mathGrade,
double englishGrade, double scienceGrade) {
this.studentId = studentId;
this.studentRecord = new StudentRecord(name, mathGrade,
englishGrade, scienceGrade);
increaseStudentCount();
}
Note that it is more logical to increase the student count inside the
constructor, as then you can make it private, as any other variables.
It is safer too.
Add also a method to display the student numbers based on studentcount
in Student.
And move your method to print student details from StudentRecord to
Student. You don't want to print studentrecord details, but student
details.
Intialize in StudentRecordExample three students with the new full
constructor you added in Student, then use the print method you moved
from StudentRecord to Student applied on each students you initialized
to print details, and finally use the method you wrote to display
student numbers to print the numbers of students.
All in one, StudentRecordExample with this new organization has 8
lines (except comments). Much more readable, much more efficient, much
more logical.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---