Skip to content

Instantly share code, notes, and snippets.

@thorpj
Created October 7, 2018 12:27
Show Gist options
  • Save thorpj/27ababdbb3a475211fba00a18b5ec160 to your computer and use it in GitHub Desktop.
Save thorpj/27ababdbb3a475211fba00a18b5ec160 to your computer and use it in GitHub Desktop.
// Allows us to use Scanner (which is for user input)
import java.util.*;
// Don't worry about why we need this. HorseTracker is the name of this program,
// which is why we used it here
class HorseTracker
{
public static String[] getHorseData(int horseCount)
{
// Defines a bucket called sc, of type Scanner, which contains a scanner that we can use to get user input
Scanner sc = new Scanner(System.in);
String[] horseNames = new String[horseCount];
String horseName;
// for loop
for (int i = 0; i < horseCount; i++)
{
System.out.print("Horse Name: ");
// this is how we get a string from the user
horseName = sc.nextLine();
horseNames[i] = horseName;
}
return horseNames;
}
public static int[] getRaceData(int horseCount)
{
Scanner sc = new Scanner(System.in);
int[] horseFinishTimes = new int[horseCount];
int horseFinishTime;
for (int i = 0; i < horseCount; i++)
{
horseFinishTime = 0;
while (horseFinishTime == 0)
{
try
{
System.out.print("Horse Finish time: ");
// this is how we get an integer from the user. In java you don't need to convert string input to integer to get an integer
horseFinishTime = sc.nextInt();
horseFinishTimes[i] = horseFinishTime;
}
catch (InputMismatchException e)
{
System.out.println("Please enter the horse finish time correctly");
horseFinishTime = 0;
}
}
}
return horseFinishTimes;
}
public static int calcWinner(int[] horseFinishTimes)
{
int lowestTime = 10000;
int finishTime;
int horseNumber = 0;
int fastestHorseNumber;
for (int i = 0; i < horseFinishTimes.length; i++)
{
finishTime = horseFinishTimes[i];
if (finishTime < lowestTime)
{
lowestTime = finishTime;
horseNumber = i;
}
}
fastestHorseNumber = horseNumber;
System.out.println("Fastest horse is " + fastestHorseNumber);
return fastestHorseNumber;
}
public static void displayTimes(String[] horseNames, int[] horseFinishTimes)
{
for (int i = 0; i < horseNames.length; i++)
{
System.out.println("Horse with number " + i + " Name: " + horseNames[i] + "Finish time: " + horseFinishTimes[i]);
}
}
public static void findHorseName(int horseNumber, String[] horseNames)
{
String horseName = horseNames[horseNumber];
System.out.println("Horse with number " + horseNumber + "is " + horseName);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String[] horseNames;
int[] horseFinishTimes;
int winningHorseNumber;
int horseCount = 0;
while (horseCount == 0)
{
try
{
System.out.print("Horse count: ");
horseCount = sc.nextInt();
}
catch (InputMismatchException e)
{
System.out.println("Please enter the horse count correctly");
horseCount = 0;
}
}
horseNames = getHorseData(horseCount);
horseFinishTimes = getRaceData(horseCount);
displayTimes(horseNames, horseFinishTimes);
winningHorseNumber = calcWinner(horseFinishTimes);
findHorseName(winningHorseNumber, horseNames);
}
}
def get_horse_data(horse_count):
horse_names = []
for i in range(0, horse_count):
horse_name = input("Horse Name: ")
horse_names.append(horse_name)
return horse_names
def get_race_data(horse_count):
horse_finish_times = []
for i in range(0, horse_count):
horse_finish_time = ""
while horse_finish_time == "":
try:
horse_finish_time = input("Horse Finish Time: ")
horse_finish_time = int(horse_finish_time)
except ValueError:
print("Please enter the horse finish time correctly.")
horse_finish_time = ""
horse_finish_times.append(horse_finish_time)
return horse_finish_times
def calc_winner(horse_finish_times):
lowest_time = 10000
for i in range(0, len(horse_finish_times)):
finish_time = horse_finish_times[i]
if finish_time < lowest_time:
lowest_time = finish_time
horse_number = i
fastest_horse_number = horse_number
print("Fastest horse is {}".format(fastest_horse_number))
return fastest_horse_number
def display_times(horse_names, horse_finish_times):
for i in range(0, len(horse_names)):
print("Number: {} Name: {} Finish Time: {}".format(i, horse_names[i], horse_finish_times[i]))
def find_horse_name(horse_number, horse_names):
horse_name = horse_names[horse_number]
print("Horse with number {} is {}".format(horse_number, horse_name))
return
horse_names = []
horse_finish_times = []
horse_count = ""
while horse_count == "":
try:
horse_count = input("Horse Count: ")
horse_count = int(horse_count)
except ValueError:
print("Please enter the horse count correctly.")
horse_count = ""
horse_names = get_horse_data(horse_count)
horse_finish_times = get_race_data(horse_count)
display_times(horse_names, horse_finish_times)
winning_horse_number = calc_winner(horse_finish_times)
find_horse_name(winning_horse_number, horse_names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment