Results 1 to 2 of 2
  1. #1
    MjM8082 is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default 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");
    	}
    	
    	
    
    }
    Here is HazardPay.java


    Java Code:
    public class HazardPay extends PayCalculator {
    
    		public void hazPay(){
    			System.out.println("Demonstrating calculations for hazard pay");
    		}
    		
    	
    		}

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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

  1. Replies: 2
    Last Post: 03-23-2012, 04:53 AM
  2. Replies: 1
    Last Post: 10-17-2011, 01:00 AM
  3. Replies: 18
    Last Post: 04-18-2011, 05:39 AM
  4. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  5. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM

Posting Permissions

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