Results 1 to 2 of 2
Thread: Need Help With Method
- 08-23-2012, 06:31 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 9
- Rep Power
- 0
Need Help With Method
I have 2 different classes that are calling the same method... getPay()
The problem I am having is that I want one class to use the payRate 7.5 and the other to use the payRate 17.0
The payRate variable is defined inside the method that they are both calling. I want the method to be able to change the payRate for whichever class is calling it. Here is an example output that I want...
Demonstrating regular pay calculations
40 hours at 7.5 is 300.0
40 hours at 7.5 is 450.0
Demonstrating pay calculations for hazard pay.
40.0 hours at 17 is 680.0
40.0 hours at 17 is 1020.0
Here is my code for the classes.
Java Code:abstract public class PayCalculator { private static double payRate = 0; private static int hours = 0; public static double getPay() { double pay = 0; int hours = 40; double payRate = 7.5; return pay = hours * payRate; } public void changeRate(){ double newRate = 0; } public static void main(String[] args) { // TODO Auto-generated method stub RegularPay newRegPay = new RegularPay(); HazardPay newHazPay = new HazardPay(); newRegPay.regPay(); int hours = 40; double payRate = 7.5; PayCalculator.getPay(); System.out.println(+hours+" hours at "+payRate+ " is " + PayCalculator.getPay()); System.out.println(""); newHazPay.hazPay(); } }
Here is RegularPay.java
Java Code:public class RegularPay extends PayCalculator { public void regPay(){ System.out.println("Demonstrating regular pay calculations"); } }
Java Code:public class HazardPay extends PayCalculator { public void hazPay(){ System.out.println("Demonstrating calculations for hazard pay"); } }
- 08-23-2012, 07:32 AM #2
Re: Need Help With Method
You could pass the pay rate as an argument to the getPay method. Or you could make it a field of PayCalculator. Since the pay is calculated the same way in both cases - only the rate is different - there is really no reason for you to subclass PayCalculator.
Get in the habit of using standard Java naming conventions!
Similar Threads
-
Creating recursion method to use Newton's method for square roots
By bdl1127 in forum New To JavaReplies: 2Last Post: 03-23-2012, 05:53 AM -
Whats the different between package.class.method and super.method?
By Pojahn_M in forum New To JavaReplies: 1Last Post: 10-17-2011, 02:00 AM -
Can i Pass a variable as a parameter from one method to other method
By Anagha in forum New To JavaReplies: 18Last Post: 04-18-2011, 06:39 AM -
Thread problem, calling method in run method
By majk in forum Threads and SynchronizationReplies: 4Last Post: 09-27-2010, 12:40 PM -
ArrayLists compareTo method, equals method
By random0munky in forum New To JavaReplies: 2Last Post: 10-26-2009, 08:20 PM
Bookmarks