Skip to content

Instantly share code, notes, and snippets.

@kurtkaiser
Last active August 12, 2018 18:15
Show Gist options
  • Save kurtkaiser/6e9f297142d0b099e8e3151f87913ba3 to your computer and use it in GitHub Desktop.
Save kurtkaiser/6e9f297142d0b099e8e3151f87913ba3 to your computer and use it in GitHub Desktop.
This was an assignment for an advance java class at my local community college. I was under a bit of a time crunch, so this program is somewhat thrown together. However, if it works it works.
/*
* Kurt Kaiser
* CTIM 168 E40
* 7.13.2018
*
*/
import java.util.Objects;
public class BreedingHorse extends Horse
{
private String mother;
private String father;
public BreedingHorse() {
this.mother = "";
this.father = "";
}
public BreedingHorse(String name, String color, int birthYear, String mother, String father) {
super(name, color, birthYear);
this.mother = mother;
this.father = father;
}
public String getMother() {
return mother;
}
public void setMother(String mother) {
this.mother = mother;
}
public String getFather() {
return father;
}
public void setFather(String father) {
this.father = father;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BreedingHorse that = (BreedingHorse) o;
return Objects.equals(mother, that.mother) &&
Objects.equals(father, that.father);
}
@Override
public String toString() {
return super.toString() +
", Mother: " + mother +
", Father: " + father;
}
}
/*
* Kurt Kaiser
* CTIM 168 E40
* 7.13.2018
*
*/
import java.util.Scanner;
public class DemoHorses {
public static void main(String[] args) {
String name;
String color;
int birthYear;
int races;
int wins;
String mother;
String father;
int count = 1;
Horse [] horseArray = new Horse[6];
for (int i = 0; i < 6; i++) {
System.out.println("--- Horse " + count + " ---");
// Name
System.out.println("Enter the horse's name.");
Scanner scan = new Scanner(System.in);
name = scan.nextLine();
// Color
System.out.println("Enter the color of " + name + ".");
scan = new Scanner(System.in);
color = scan.nextLine();
// Birth year
System.out.println("Enter " + name + "'s birth year.");
scan = new Scanner(System.in);
birthYear = scan.nextInt();
if (i < 3) {
// Races
System.out.println("Enter the number of races.");
scan = new Scanner(System.in);
races = scan.nextInt();
System.out.println("Enter the number of wins.");
scan = new Scanner(System.in);
wins = scan.nextInt();
horseArray[i] = new RaceHorse(name, color, birthYear, races, wins);
// Cast to access the setRaces method
((RaceHorse)horseArray[i]).setRaces(races);
} else {
System.out.println("Enter the " + name + "'s mother's name.");
scan = new Scanner(System.in);
mother = scan.nextLine();
// Color
System.out.println("Enter the " + name + "'s father's name.");
scan = new Scanner(System.in);
father = scan.nextLine();
horseArray[i] = new BreedingHorse(name, color, birthYear, mother, father);
}
horseArray[i].setBirthYear(birthYear);
count ++;
}
// Output data entered
count = 1;
System.out.println("\n Data Entered");
for (int n = 0; n < 6; n++){
System.out.println("Horse " + count + "\n" + horseArray[n].toString());
count ++;
}
}
}
/*
* Kurt Kaiser
* CTIM 168 E40
* 7.13.2018
*
*/
import java.util.Objects;
import java.util.Scanner;
public class Horse
{
//instance variables
private String name;
private String color;
private int birthYear;
public Horse() {
this.name = "";
this.color = "";
this.birthYear = 0;
}
public Horse(String name, String color, int birthYear) {
this.name = name;
this.color = color;
this.birthYear = birthYear;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getBirthYear() {
return birthYear;
}
public void testing(int num){
System.out.println(!(num > 1 && num < 5));
}
public void setBirthYear(int birthYear) {
while(birthYear < 1988 || birthYear > 2018) {
System.out.println("Year must be after 1988 and before 2018.");
Scanner scan = new Scanner(System.in);
birthYear = scan.nextInt();
}
this.birthYear = birthYear;
}
public String toString() {
return "Name: " + name +
", Color: " + color +
", Birth year: " + birthYear;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Horse horse = (Horse) o;
return birthYear == horse.birthYear &&
Objects.equals(name, horse.name) &&
Objects.equals(color, horse.color);
}
@Override
public int hashCode() {
return Objects.hash(name, color, birthYear);
}
}
/*
* Kurt Kaiser
* CTIM 168 E40
* 7.13.2018
*
*/
import java.util.Objects;
import java.util.Scanner;
public class RaceHorse extends Horse
{
private int races;
private int wins;
// Constructors
public RaceHorse() {
this.races = 0;
this.wins = 0;
}
public RaceHorse(String name, String color, int birthYear, int races, int wins) {
super(name, color, birthYear);
this.races = races;
this.wins = wins;
}
public int getRaces() {
return races;
}
public void setRaces(int races) {
while (races < 0 || races < wins){
System.out.println("Races must be greater than 0 and greater than the number of wins.");
Scanner scan = new Scanner(System.in);
races = scan.nextInt();
}
this.races = races;
}
public int getWins() {
return wins;
}
public void setWins(int wins) {
this.wins = wins;
}
@Override
public String toString() {
return super.toString() +
", Races: " + races +
", Wins: " + wins;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment