• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ejercice Farm Horse

 
Diego Kazehaya
Greenhorn
Posts: 3
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I request your support for the following :



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.
The Horse class has methods to set and to read the maximum working hours.
It has another 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).


I need help.
I realized this problem, but does not compile my program, I mark error when build time. anyone can tell me what is wrong, or I'm doing wrong?

the error is : "A java exception has occured".

My code is:









 
Campbell Ritchie
Sheriff
Pie
Posts: 49823
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never write == true or == false. It is not if (b == true) ... nor if (b == false)... It is if (b)... and if (!b)...
Using == is very error‑prone; you might write = by mistake.
You are not allowed to write package names beginning with java or javax. You will have to choose a different package name.
 
Diego Kazehaya
Greenhorn
Posts: 3
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Never write == true or == false. It is not if (b == true) ... nor if (b == false)... It is if (b)... and if (!b)...
Using == is very error‑prone; you might write = by mistake.
You are not allowed to write package names beginning with java or javax. You will have to choose a different package name.


Ok. works !!

my program has compiled and result throws 36.

The program is correct? or missing something?

I am new to programming apologize for any inconvenience
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say it prints 36 or outputs 36. Throws is used for exceptions and errors.

I think the assignment is asking you to validate the input data, and not allow bad inputs. You've created a horse that has already worked more than its maximum hours per week, but haven't indicated any error. I believe that violates the requirement from the assignment: "If the Horse class detects an attempts to set incorrect data, the data remains unchanged (and you print a message to help you debugging)."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic