Thanks to all of you
On Fri, Mar 20, 2009 at 9:27 AM, Patrick_Stiady
<[email protected]>wrote:
> On Thu, 2009-03-19 at 14:28 -0700, Ryu wrote:
> > Hi to all,
> > Hope everyone is fine . I have a problem with lab as
> > mentioned in subject regarding (Exercise4 : Constructor) . Actually in
> > the modification it is being described that pass average as a
> > parameter and created annaRecord object with newly constructor . I
> > want to ask how , actually i try the things but it is of no use, I
> > couldn't get the result .
> >
> > What I understand is that I have to pass five parameters
> > to consructor including name, mathGrade, englishGrade, scienceGrade,
> > and average. First i tried this.
> >
> > public StudentRecord(String name, double mGrade, double eGrade,
> > double sGrade, double average){
> > this(name, mGrade, eGrade, sGrade);
> > this.average = average;
> > }
> >
> > and in the ConstructorExample class I call it like
> > this.
> >
> > StudentRecord annaRecord = new StudentRecord("Anna", 10, 20, 30,
> > annaRecord.getAverage());
> >
> > but after it, it shwos the error that " variable
> > annaRecord might not have been initialized ".
> > why this is happened although i made a object annaRecord
> > in the statement?
> >
> > Then I made every field of StudentRecord class static ,
> > also made the getAverage() , getMath() , getEnglish(), and getScience
> > methods static and try this.
> >
> > public StudentRecord(String name, double mGrade, double eGrade,
> > double sGrade, double average){
> > this(name, mGrade, eGrade, sGrade);
> > this.average = average;
> > System.out.println("name = " + name + " and average = " +
> > average);
> > }
> >
> > and call it like this.
> >
> > StudentRecord annaRecord = new StudentRecord("Anna", 10, 20, 30,
> > StudentRecord.getAverage());
> >
> > ok there is no error but when i run the program it shows
> > the output like
> >
> > name = Anna and average = 0.0
> >
> > why this show zero average ? tell me plz.
>
> In creating an object, the Java first call its constructor.
>
> You want to create annaRecord object by calling its constructor using
> new StudentRecord with its parameters, but one of its parameter is a
> method. I never knew whether such a practice is possible. Instead, I
> will pass only the name and its grades (4 parameters instead of 5) and
> create a method inside the constructor to calculate the average and
> assign it to a field to be returned by a getter method.
>
> You can learn more how to create a Java getter method in the tutorial.
>
> Patrick
>
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---