Question
Race Horse Lab Part 1: Write a class Horse which has the following components: Private Data Fields: location the horses location on a 15-unit track.
Race Horse Lab
Part 1: Write a class Horse which has the following components:
Private Data Fields: location the horses location on a 15-unit track.
Must be an integer between 1-15. i.e., if location is 1 the
horse is at the start, at 8 the horse is halfway down, at 15
the horse is at the finish line.
index the identifying number of the horse.
***the Horses data fields MUST be declared as private.
Methods: Horse() - a Default Constructor that initializes a horse to location 1 and index 0.
Horse(int loc, int i) - a Constructor that initializes a horses data fields to the two integer arguments sent to it.
getLocation() an Accessor method that returns the horses current location.
getIndex() an Accessor method that returns the horses index.
advance() a Mutator method that adds 1 to the horses location as long as it is less than 15 (the track size).
raceStride() this might make the horse move forward based on a mathematical probability. Use random to generate a random integer from 1 to 100. The horse will advance (towards the finish line) if the number is less than 50.
toString() this method needs to be overloaded so that it shows the track and the horses current location on the track (identified by its index). If a horse with index 3 is halfway down the track, writing out the horse will look like this: |-------3-------|
When the horse class is complete, write a short HorseDriver program (in another file) which will create and test a horse object.
Part 2: Expand the HorseDriver program to create three instances of horse objects. Each horse index will be a consecutive number from 0 to 2. Showing each horse as they race down the track might look like this:
|----0----------|
|--1------------|
|------2--------|
A method in the driver should allow for one iteration of the race to occur, in which each horse has a chance of advancing (based on the random). This can be called in a loop until
one or more horses crosses the finish line, thus ending the race. Print the name of the winner(s) of the race.
If everything works and only a single winner is declared even if it is a tie, the program will net 95%.
If a tie situation reports the index of every horse that won, the program will net 100%.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started