Build a Java horse race simulation using OOP principles. Create Horse
and Race
classes, use constructors and methods, and simulate movement with random seeds. Learn object-oriented design, arrays of objects, and track output formatting in a console-based race. Includes complete Java code example and explanation.
Overview
Use the principles of Object Oriented Design (OOD) and Object Oriented
Programming (OOP) to re-build the horseRace assignment using object-oriented
programming. Each horse will be an object, and the race will be another object
that contains a list of horses and manages the race. Along the way, you will
experiment with UML, create classes, review access modifiers, build member
variables, add access methods, and create constructors. The Project Build a
program that simulates a horse race. The race works just like the last
assignment, and the user may not ever see the difference. But this time, the
underlying design will use objects. You will have two objects in the game.
There will be an array of five horse objects. The horse will 'know' how to
advance according to a random flip, and to return its position. The other
primary entity in this game is the race (or the track, if you prefer.) This
object will contain a series of horses. It will also have the ability to start
the race. When the race is running, the race class will 'tell' each horse to advance,
and will print out a track diagram showing the relative positions of the
horses. In this case we may do automated testing against your program. In order
to accommodate this you will prompt (ask for input) for a random seed and use
it to seed your random number generator. This will allow you to test your
program against several seeds. And we may test your program with a series of
these random seeds. While this will not be your entire grade, but your ability
to pass these tests could be a consideration in your grade. This is a common
industry practice and one we will try to introduce you. Note that you will not
need the automation feature nor a GUI. This just gives you an idea of the race
concept. Code Organization While you have already written this program in a
procedural fashion, this program is a perfect candidate for the object-oriented
paradigm
Answer
import
java.util.Random;
public class Horse{
int position ;
public Horse(){
position =0;
}
public void advance(){
Random rand = new
Random();
int chance =
rand.nextInt(2) % 2;
if
( chance == 1 ) {
position++;
}
}
public int
getPosition(){
return position ;
}
}
// Race.java
import java.util.*;
public class
Race{
Horse h[] = new Horse[5]
;
int length ;
public Race() {
length = 15 ;
for(int i = 0 ; i
< 5 ; i++){
h[i] = new Horse();
}
}
public Race(int
length){
this.length =length;
for(int i = 0 ; i <
5 ; i++){
h[i] = new Horse();
}
}
public void
printLane(int horseNum){
for
(int i = 0; i < length; i++) {
int
loc = h[horseNum].getPosition();
if
(i == loc) {
System.out.print (
horseNum );
}
else {
System.out.print
(".");
}
}
System.out.println("")
;
}
public void start(){
boolean
continueon = true;
for
(int n = 0; n < 5; n++) {
printLane(n);
}
System.out.println("")
;
int wins =0;
while
(continueon) {
for
(int number = 0; number < 5; number++) {
h[number].advance();
printLane(number);
if
(h[number].getPosition() == length ) {
continueon
= false;
wins
= number;
}
}
System.out.println("");
}
System.out.println("Horse
" +( wins ) + " wins!" );
}
public static void
main(String[] args ) {
Scanner
input = new Scanner(System.in);
System.out.print("Please
enter a random seed:");
int
seed = input.nextInt();
Random
rand = new Random(seed);
Race
race = new Race();
race.start();
}
}