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
Try to come up with something with
java.util.TimerMinor 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”.
Why not use
isLazyin the parameter as well, instead oflasyOrHardworkingI think
bool == trueisn’t adding any value, this looks better to me: