Results 1 to 20 of 20
- 11-30-2011, 09:54 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
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;}
- 11-30-2011, 10:17 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: Updating the Instance Variable of a Class
Please post your main method, which should create a StaffMember and call the toString() method.
- 11-30-2011, 10:30 PM #3
Re: Updating the Instance Variable of a Class
output is just zero so for whatever reason the weekly wage is not updating.
What are the values used to create weeklywage? Add a println to the calculateWages method to print their values out.
- 12-01-2011, 12:23 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
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]);
}
}
}
- 12-01-2011, 12:28 AM #5
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.
- 12-01-2011, 12:41 AM #6
Re: Updating the Instance Variable of a Class
Where does your code call the calculateWages method? Stuff does not happen by magic in programming.
- 12-01-2011, 12:55 AM #7
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.
- 12-01-2011, 01:07 AM #8
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.
- 12-01-2011, 01:15 AM #9
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.
- 12-01-2011, 01:19 AM #10
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? ;)
- 12-01-2011, 01:23 AM #11
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
- 12-01-2011, 01:30 AM #12
Re: Updating the Instance Variable of a Class
You would call it when it needs to be called.
- 12-01-2011, 01:31 AM #13
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Re: Updating the Instance Variable of a Class
Ok cheers, I'll give it another shot in the morning.
- 12-01-2011, 01:40 AM #14
Re: Updating the Instance Variable of a Class
gotten them to the edge of the cliff?
- 12-01-2011, 05:11 PM #15
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
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]);
}
}
}
- 12-01-2011, 05:14 PM #16
Re: Updating the Instance Variable of a Class
Is there still a problem or have you solved it?
- 12-01-2011, 05:19 PM #17
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Re: Updating the Instance Variable of a Class
Yea there is please see the previous post, thanks.
- 12-01-2011, 05:26 PM #18
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:
- 12-01-2011, 05:32 PM #19
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
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 :-)
- 12-01-2011, 06:07 PM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Similar Threads
-
Jlabel and Jbutton class with no instance variable-help
By Fily55 in forum AWT / SwingReplies: 3Last Post: 12-16-2013, 06:35 PM -
Help me finish an instance method which references a class variable?
By trueblue in forum New To JavaReplies: 20Last Post: 06-03-2009, 06:33 PM -
create new instance of variable class
By Fedor in forum New To JavaReplies: 5Last Post: 04-12-2009, 09:13 PM -
Naming a class instance with a variable
By pikalex88 in forum New To JavaReplies: 3Last Post: 09-30-2008, 07:27 PM -
Instance variable
By Jack in forum New To JavaReplies: 2Last Post: 07-04-2007, 05:00 AM
Bookmarks