Results 1 to 20 of 20
  1. #1
    Leo11221 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Updating the Instance Variable of a Class

    I am writing a small program that calculates wages based on different factors. One of the classes contains a method that calculates the persons wage based on pay rate*hours worked. Now what I want to do is to get the method calculatewages that caclulates the payrate to update the instance variable weeklywage of the classs. Im having trouble with this, when I test it, the output is just zero so for whatever reason the weekly wage is not updating. Heres the code, would appreciate some help :-)

    class StaffMember extends Person
    {



    public double staffnum, payrate, hoursworked, weeklywage; //Class StaffMember Attributes

    public StaffMember(double staffNumm, double payRatee, double hoursWorkedd, double weeklyWagee)
    {
    this.payrate=payRatee;
    this.staffnum=staffNumm;
    this.hoursworked=hoursWorkedd;
    this.weeklywage=weeklyWagee;

    }



    public double caulculateWages()
    {

    weeklywage=payrate*hoursworked;
    return weeklywage;

    }


    public String toString(){return "Staff member number " +staffnum +" with pay rate " +payrate +" has worked " +hoursworked +" hours, to earn " +weeklywage;}

  2. #2
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: Updating the Instance Variable of a Class

    Please post your main method, which should create a StaffMember and call the toString() method.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Updating the Instance Variable of a Class

    output is just zero so for whatever reason the weekly wage is not updating.
    Where do you assign a value to weeklywage?
    What are the values used to create weeklywage? Add a println to the calculateWages method to print their values out.

  4. #4
    Leo11221 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Updating the Instance Variable of a Class

    Sorry I have another class which tests data for the different classes. I have the same problem with all the classes, I want to get the calculate wage method to update the weekly wage variable in each class.

    public class StaffMemberTest
    {
    public static void main(String[]args)
    {

    Person[]test = new Person[4];
    test[0]= new StaffMember(1, 10, 40, 0);
    test[1]= new SalesPerson(2, 10, 40, 0,0);
    test[2]= new TruckDriver(3, 0, 40, 0, 20, 50);
    test[3]= new OfficeWorker(4, 12, 38, 0);
    for(int i = 0; i<test.length; i++)
    {
    System.out.println(test[i]);
    }



    }
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Updating the Instance Variable of a Class

    Try debugging your code by adding printlns to everyplace you change the values of the variables. Print out the values of the variables so you can see how your code is working. If a variable's value never prints out, then you can expect the variable's value has not changed and it will be zero.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Updating the Instance Variable of a Class

    Where does your code call the calculateWages method? Stuff does not happen by magic in programming.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Updating the Instance Variable of a Class

    If the OP would add the printlns that I suggested be put there, he would see that nothing was printed out.
    Then he should come to the same idea that you have just given him.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Updating the Instance Variable of a Class

    Somethimes you need to give them a bigger nudge. I wouldn't be surprised if OP followed your advice and then posted "Nothing printed" and were still none the wiser.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Updating the Instance Variable of a Class

    You never know. I nudge and nudge and nudge and finally cave in and give a hard push.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Updating the Instance Variable of a Class

    Does that hard push occur when all the nudging has gotten them to the edge of the cliff? ;)

  11. #11
    Leo11221 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Updating the Instance Variable of a Class

    Quote Originally Posted by Junky View Post
    Somethimes you need to give them a bigger nudge. I wouldn't be surprised if OP followed your advice and then posted "Nothing printed" and were still none the wiser.
    I think I'd be able to figure that out....... thanks for the replies anyway, should the method be called in the StaffMember Class or the test class? sorry still kind of new to this!

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Updating the Instance Variable of a Class

    You would call it when it needs to be called.

  13. #13
    Leo11221 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Updating the Instance Variable of a Class

    Ok cheers, I'll give it another shot in the morning.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Updating the Instance Variable of a Class

    gotten them to the edge of the cliff?
    If only you could tell where they are.

  15. #15
    Leo11221 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Updating the Instance Variable of a Class

    Ok I followed your advice used System.out.println and the method is not being called. Now I am still unsure about how to call the method using the StaffMemberTest Class. I created another test class and it works fine and updates the weeklywage instance:

    public class test2
    {
    public static void main(String[]args)
    {
    StaffMember leo = new StaffMember(1,2,8,4);
    double wagess = leo.calculateWages();
    System.out.println(leo);
    }
    }

    I am just unsure of how to do this with an array in my StaffMemberTest Class?

    public class StaffMemberTest
    {
    public static void main(String[]args)
    {

    StaffMember[]test = new StaffMember[3];


    test[0]= new SalesPerson(2, 10, 40, 0,3000);
    test[1]= new TruckDriver(3, 0, 40, 0, 20, 50);
    test[2]= new OfficeWorker(4, 12, 38, 0);


    for(int i = 0; i<test.length; i++)


    {
    System.out.println(test[i]);
    }


    }
    }

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Updating the Instance Variable of a Class

    Is there still a problem or have you solved it?

  17. #17
    Leo11221 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Updating the Instance Variable of a Class

    Yea there is please see the previous post, thanks.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Updating the Instance Variable of a Class

    Please explain what the problem is. The end of your post says:
    it works fine and updates the weeklywage instance:

  19. #19
    Leo11221 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Updating the Instance Variable of a Class

    That was the test2 class that was working fine, its the class with the arrays I was having trouble with. I've solved it now anyway its all working grand, thanks for the help :-)

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Updating the Instance Variable of a Class

    Call it on each of the instances of the class in the array?
    You're already looping over it...but not knowing your requirements this is something of a guess.

Similar Threads

  1. Replies: 3
    Last Post: 12-16-2013, 06:35 PM
  2. Replies: 20
    Last Post: 06-03-2009, 06:33 PM
  3. create new instance of variable class
    By Fedor in forum New To Java
    Replies: 5
    Last Post: 04-12-2009, 09:13 PM
  4. Naming a class instance with a variable
    By pikalex88 in forum New To Java
    Replies: 3
    Last Post: 09-30-2008, 07:27 PM
  5. Instance variable
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 05:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •