Learning how to create and use classes and objects in Java is one of the most important concepts you will learn this semester. This is a KEY chapter on learning how to program in an object-oriented la

//-------------------------------------------------------------- // A driver program for a Horse class that students create.

// Save this program in the same location as your Horse class.

// Compile your Horse class. Compile this driver program.

// Run the driver program to test your Horse class.

//-------------------------------------------------------------- import java.util.Scanner; import java.util.Calendar; public class HorseDriver { public static void main (String[] args) { Scanner in = new Scanner (System.in); /*----------------------------------------------------------------------------------------------- New objects are instantiated using the Horse class WindstarHorse shows how to instantiate a single Horse object inBarn is an array of Horse objects. The last entry in the array is a Horse that is instantiate using the default constructor created by students.

//-----------------------------------------------------------------------------------------------*/ Horse WindstarHorse = new Horse("Shiraz", 1340, 62, 2014, 'M'); Horse[ ] inBarn = new Horse[4]; inBarn[0] = new Horse("Silver", 1405, 60, 2016, 'M'); inBarn[1] = new Horse("Lady", 1107, 58, 2016, 'F'); inBarn[2] = new Horse("Baby", 100, 40, 2017, 'F'); inBarn[3] = new Horse (); int horseInBarn = 4; //----------------------------------------------------------------------------------------------- // Variables are declared //----------------------------------------------------------------------------------------------- String name = ""; //For reading gender as a string and then converting to a char String sGender = ""; char gender = '\0'; int height = 0, weight = 0, birthYear = 0, age = -1; String fullGender = ""; int currentYear = Calendar.getInstance().get(Calendar.YEAR); //----------------------------------------------------------------------------------------------- // Data is input for 4th horse in the barn //----------------------------------------------------------------------------------------------- System.out.println("---- Lexington KY Breeding Farm ----\n\n"); System.out.println("The following input is to provide values to a new horse to be boarded in a barn\n\n"); while (name.equals("")) { System.out.print ("What is your horse's name? "); name = in.nextLine(); } while (weight <= 0 || weight >= 2500) { System.out.print ("What is your horse's weight (between 1 and 2500 pounds)? "); weight = in.nextInt(); } while (height <= 20 || height >= 80) { System.out.print ("What is your horse's height (between 20 and 80 whole inches)? "); height = in.nextInt(); } while (birthYear < 1950 || birthYear > currentYear) { System.out.print ("What year was your horse born (4-digit year between 1950 and present) "); birthYear = in.nextInt(); sGender =in.nextLine(); //Clears the buffer } while ( !(gender == 'M' || gender =='F') ) { System.out.print ("What gender is your horse (use M or F)? "); sGender =in.nextLine(); gender = (char) sGender.toUpperCase().charAt(0); } System.out.println("---- End of Input ----\n"); //----------------------------------------------------------------------------------------------- // The data values for the 4th horse in the barn are updated using the setters (mutators) in the // Horse class.

// // That is followed by a statement to update the height of the WindstarHorse // // This verifies all mutators are working properly.

//----------------------------------------------------------------------------------------------- inBarn[3].setName(name); inBarn[3].setWeight(weight); inBarn[3].setHeight(height); inBarn[3].setBirthYear(birthYear); inBarn[3].setGender(gender); WindstarHorse.setHeight(65); //----------------------------------------------------------------------------------------------- // The horse in the barn are displayed.

// This verifies all accessors are working properly.

// Notice how the array and accessors are used in the statements //----------------------------------------------------------------------------------------------- System.out.println("Horses currently in the barn:\n\n"); for (int i = 0; i < inBarn.length; i++) { System.out.println("Name: \t\t" + inBarn[i].getName() + "\t\tWeight: \t" + inBarn[i].getWeight() + " lbs." + "\nGender: \t" + inBarn[i].getGender() + "\t\tHeight: \t" + inBarn[i].getHeight() + " inches" + "\nBirth Year: \t" + inBarn[i].getBirthYear() + "\n\n"); } //----------------------------------------------------------------------------------------------- // The calcAge() and calcHands() methods are tested.

// Notice how the Horse object (not in an array) is used //----------------------------------------------------------------------------------------------- System.out.println("\n\nAge and Height (in hands) of Windstar Horse:"); System.out.println("Name: \t" + WindstarHorse.getName() + "\tAge: \t" + WindstarHorse.calcAge() + "\tHands: \t" + WindstarHorse.calcHands()); //----------------------------------------------------------------------------------------------- // Examples of using the toString( ) method to display instance data from a class //----------------------------------------------------------------------------------------------- System.out.println ("\nOutput from toString: \n" + WindstarHorse + "\n\n"); } }