views:

188

answers:

3

I got this exercise, is not a homework, I just trying to solve:

We manage a farm with horses that have to work on the field.

A horse has

  • a name,
  • a maximum amount of working hours per week,
  • the amount of hours actually worked and
  • a field to indicate if she is lazy or hard-working.

All the attributes of the Horse class are private and they have no setters. Initial values are passed through the constructor.

The Horse class has a method to add one hour of actual work. That method is called every hour (that the horse works). At the begin of the next week, we reset that counter to 0, by calling another method taking no parameter.

A lazy horse cannot work more than 34 hours/week, while a hard-working horse can work up to 80 hours.

Code a Horse class that is shielded against wrong working hours data.

Your main method will create an horse and call its methods, but the data must never be corrupted, ie. the working hour limits must be respected. For example, a lazy horse's maximum hours cannot be set above 34 and the number of hours worked cannot be greater than the maximum.

If the Horse class detects an attempts to set incorrect data, the data remains unchanged (and you print a message to help you debugging).

Example of correct data:

Name: "Blacky" 
lazy: no
max hours / week = 70
actual hours this week = 61

Name: "Sultan" 
lazy: yes
max hours / week = 30
actual hours this week = 1

Example of corrupted data (your code should make such a situation impossible to reach)

Name: "Georges" 
lazy: yes
max hours / week = 50 (wrong because lazy horses work max 34h/week)
actual hours this week = 51 (wrong because 51 > 50).

This is my code:

public class Horse {

    private String name;
    private int maximumAmount;
    private int amountWorked;
    private boolean isLazy;

    public Horse(String name, int maximumAmount, int amountWorked, boolean lasyOrHardworking) {
        this.name = name;
        this.maximumAmount = maximumAmount;
        this.amountWorked = amountWorked;
        this.isLazy = lasyOrHardworking;
    }

    void everyHour(){
        amountWorked = amountWorked + 1;
        System.out.println(amountWorked);

        if((isLazy == true)&&(amountWorked <= 34)){
            resetToZero();
        }
        if((isLazy == false)&&(amountWorked <= 80)){
            resetToZero();
        }
    }

    void resetToZero(){
        this.amountWorked = 0;
    }
}

and my main class

public class MainHorse {
    public static void main(String args[]){
        Horse one = new Horse("Blacky", 34,35,true);
        one.everyHour();
    }
}

my question is how can I get that my method everyhour do the reset method, and in general, what is wrong in my code?

I hoper you can help me

+1  A: 

My question is how can I get that my method every hour do the reset method, and in general, what is wrong in my code?

Try to come up with something with java.util.Timer

Minor details:

Typos and spelling inconsistencies will come back to bite you someday (less likely in Java than in languages that do not require explicit declaration), you should stick to one correct spelling of the word "lazy".

this.isLazy = lasyOrHardworking;

Why not use isLazy in the parameter as well, instead of lasyOrHardworking

this.isLazy = isLazy;

I think bool == true isn't adding any value, this looks better to me:

if(isLazy && (amountWorked <= 34))
Bakkal
thanks for the advise
GeorgeBecj
+1  A: 

I think what you are doing wrong is calling resetToZero() from inside the everyHour() function. I would have every hour return a bool to indicate success or failure. If failure, it doesn't increment the horse's amount worked, but prints the debug message. The main code could then check to see if it is violating the horses limits. The main code would also call resetToZero when the week ended.

thanks I get it
GeorgeBecj
+1  A: 

You're not supposed to go back to zero, just not to increase the hoursworked. Also, if this isn't homework, you have some freedom, so I would not have independent variables for max hours and lazy - that just gives you an opportunity to get them out of sync. I would use named constants for maxLazyhours and maxHardworkinghours.

Then have a function called IncrementHours that just says

if (lazy && hours < maxLazyhours) || hours < maxHardworkinghours)
   hours++;

It's simpler, right?

Kate Gregory
yeah its simpler, I need to correct my code thanks for answering
GeorgeBecj