Results 1 to 9 of 9
  1. #1
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default Class inheritance question (code provided)

    Hi guys! I have this code that I am working on, and when I try to run it, the program goes into a non ending loop and eclipse stalls........

    I will post the first class - which is HourlyWorker, and I am supposed to derive a subclass called OTWorker. Hourly worker is derived from "Employee" class, so I hope that does not make any issues. Code as followed (both are mine........)


    Java Code:
    import java.util.Date;
    
    
    public class HourlyWorker extends Employee
    {
    
    	private double numHours;
    
    	/**
    	 * precondition: name cannot be null, ssn must be a valid social security
    	 * 	number, birthday must be before current date, rate must be positive
    	 * @param name
    	 * @param ssn
    	 * @param birthday
    	 * @param rate
    	 * @throws IllegalArgumentException if precondition is not true
    	 * postcondition: HourlyWorker object with specifics properties has been created
    	 */
    	public HourlyWorker(String name, String ssn, Date birthday, double rate)
    	{
    		super(name, ssn, birthday, rate);
    		numHours = 0;
    	}
    
    	/**
    	 * @return the numHours
    	 */
    	public double getNumHours()
    	{
    		return numHours;
    	}
    	
    	/**
    	 * preconditions: additionalHours >=0
    	 * @param additionalHours
    	 * @throws IllegalArgumentException if additionalHours < 0
    	 * postcondition: number of hours worked has been increased by additionalHours
    	 */
    	public void addHours (double additionalHours)
    	{
    		if (additionalHours < 0)
    			throw new IllegalArgumentException ("additional hours cannot be negative");
    		numHours += additionalHours; 
    	}
    
    	/* (non-Javadoc)
    	 * @see Employee#pay()
    	 */
    	@Override
    	public double pay()
    	{
    		double total = numHours * getRate();
    		return total;
    	}
    	
    	/**
    	 * postcondition: number of hours worked has been reset to 0
    	 */
    	public void resetNumHours ()
    	{
    		numHours = 0;
    	}
    }




    Java Code:
    public class OTWorker extends HourlyWorker
    {
    	
    	private double numHours;
    	
    
    	public OTWorker(String name, String ssn, Date birthday, int rate) 
    	{
    		super(name, ssn, birthday, rate );
    		
    	}
    
    	
    	//   ***************************************** ALL BELOW ARE OVERRIDDEN************************
    	
    	
    	/**
    	 * preconditions: additionalHours >=0
    	 * @param additionalHours
    	 * @throws IllegalArgumentException if additionalHours < 0
    	 * postcondition: number of hours worked has been increased by additionalHours
    	 */
    	public void addHours (double additionalHours)
    	{
    		if (additionalHours < 0)
    			throw new IllegalArgumentException ("additional hours cannot be negative");
    		numHours += additionalHours; 
    	}
    
    	/* (non-Javadoc)
    	 * @see HourlyWorker#pay()
    	 */
    	@Override
    	public double pay()
    	{
    		double total = numHours * getRate() * 1.5;
    		return total;
    	}
    	
    	/**
    	 * postcondition: number of hours worked has been reset to 0
    	 */
    	public void resetNumHours ()
    	{
    		numHours = 0;
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Adomini View Post
    Hi guys! I have this code that I am working on, and when I try to run it, the program goes into a non ending loop and eclipse stalls........

    I will post the first class - which is HourlyWorker, and I am supposed to derive a subclass called OTWorker. Hourly worker is derived from "Employee" class, so I hope that does not make any issues. Code as followed (both are mine........)
    Hard to tell. I'm betting that your bug is in code that you're not showing us, but again, hard to tell based on these bits of code.

  3. #3
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Hi Fubarable! I posted both codes - the super class (HourlyWorker) and the subclass of Hourly Worker called OTWorker. Better said, as far as inheritance goes, OTWorker inherits all the methods and such from HourlyWorker , and just adds its own method/change.

    It looks ugly (believe me, I know) but I was wondering if it would be an easy thing to spot.... maybe I did not put down super.XXXX(for example)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Adomini View Post
    Hi Fubarable! I posted both codes - the super class (HourlyWorker) and the subclass of Hourly Worker called OTWorker. Better said, as far as inheritance goes, OTWorker inherits all the methods and such from HourlyWorker , and just adds its own method/change.

    It looks ugly (believe me, I know) but I was wondering if it would be an easy thing to spot.... maybe I did not put down super.XXXX(for example)
    Again, we can't spot it if we can't see it. I seriously think that your problem is in code that you're not showing us. Have you tried to debug this by peppering the code with println statements? Do this so you can find out where the infinite loop is. Only then come back with the offending code.

  5. #5
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    I appreciate your response. I do the println statments at the end to see what I come up with.

    I do have one more question though- when you say that you can not see the bugs in the code because I am not showing it..... I am confused because I posted both classes - the original and the subclass of that original. Were you talking about the errors the compiler was giving me?? Either way, no need- I appreciate you being up this late putting up with a complete noobie like myself.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    There's no main() method - so it's a bit hard to know what is happening when you run whatever it is that you run.

    There does seem to be nothing all that strange about the code that you've posted, So if you are getting undesirable runtime behaviour, that is most likely happening somewhere else.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Adomini View Post
    I appreciate your response. I do the println statments at the end to see what I come up with.
    Do them in the body of your code too to test the state of your variables, and to see if any statements get repeated infinitely -- basically to find your infinite loop.

    I do have one more question though- when you say that you can not see the bugs in the code because I am not showing it..... I am confused because I posted both classes - the original and the subclass of that original. Were you talking about the errors the compiler was giving me?? Either way, no need- I appreciate you being up this late putting up with a complete noobie like myself.
    Assuming you have an infinite loop somewhere, I see no code that could possibly generate it. Now, I don't claim to know this with 100% certainty, and without compilable and runnable code, no one can, but I do know that nothing in the code you've posted suggests to me the possibility of an infinite loop, suggesting that it must be somewhere else in the code you're not showing us.

  8. #8
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    I have a "tester" and the original super class (called Employee) - both provided by the instructor, so they should be flawless.

    That is ok - Thanks for everyone looking into this. I will continue working on it and do some more of the nitty gritty work. Have a good night!

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I have a "tester" and the original super class (called Employee) - both provided by the instructor, so they should be flawless.

    Yeah - we see a lot of flawless stuff from instructors here ;)

    Seriously though, you have suspect *everything*.

Similar Threads

  1. Replies: 8
    Last Post: 05-15-2010, 05:05 PM
  2. Replies: 3
    Last Post: 05-06-2010, 08:10 AM
  3. Replies: 3
    Last Post: 01-11-2010, 07:48 AM
  4. Generics & Inheritance Question
    By Lee Rhodes in forum Advanced Java
    Replies: 3
    Last Post: 07-03-2009, 06:04 AM
  5. a question about inheritance
    By litojs in forum New To Java
    Replies: 8
    Last Post: 11-10-2008, 11:07 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
  •