// Question over Arrays // Arrays // ====== // No Methods // length as a // public final int length = ???; // Use [index] // new? // String [] cats = new String[20]; // cats[0] = "Tom"; // cats.length // int [] numbers = new int[10]; // numbers[0] = 98; // numbers.length is 10 // (or a super class or interface) // Horse [] horses = new Horse[10]; // Access // horses[0] = new Horse("Silver", 15); // Things you should do: // READ the question 2 or 3 times // Make notes, circle and/or box things // Circle your instance variables // Box off the method to write and note the pre and post conditions // Draw pictures or draw on their pictures // Think first and understand, then write your code. // MAKE SURE THAT YOU WRITE YOUR CODE IN THE CORRECT PLACE // Given: // (You can not new an interface or abstract // class! // An abstract class contains at // least 1 abstract method (A method without // any code). // An interface is simply a list of // methods (with no code) that must be // implemented. // What does the word implements mean? // Think of the Paint program. // There is a AbstractShape class that // holds all of the basics of any object // that you may want to draw. // A Point, A Line, A Rectange, An Ellipse // (What do all of these have in common? public class Horse { // instance variables not shown public Horse(String name, int age) { // implementation not shown } public String getName() { // implementation not shown } public String getAge() { // implementation not shown } } public class HorseBarn { // This is NOT inheritance! // This is an example of encapsulation. // The HorseBarn CONTAINS OR HAS A list // of Horse objects. // Inheritance uses the phrase IS A // From Pong: // A Ball IS A Block. (Inheritance) // A Paddle IS A Block. (Inheritance) // From BlackJack: // A BlackJackCard IS A Card. (Inheritance) // A Dealer HAS A Deck. (Encapsulation) // From Paint: // A Point IS A Shape. (inheritance) // A Rectangle IS A Shape. (Inheritance) // An Oval IS A Shape. (Inheritance) // Polymorphism // The ability of a general object // to behave at runtime as a // specific object. // (by overriding methods) // A block can behave like a // Paddle or a Ball // See the Paint program!!!!! // A Shape can behave like a // Rectangle or a Point or an // Ellipse or a ... // We could make an array or an // ArrayList of Shape objects. // We can use the superclass so // that the Array has all the same // types. // Shape [] shapes = new Shape[100]; // It will call the methods of the // Point or Rectange or Ellipse! private Horse [] horses; // constructor not shown // Question Part A: // Complete the method findHorse. // return the index position of the horse if found. // return -1 if you do not find the horse. // precondition: // horses has at least one horse // Some positions may be null. // There are no duplicate horse names. public int findHorse(String horseName) { // to be completed for part A // NOT THE BEST WAY especially // with an ArrayList, since you can't change // the size of the ArrayList, so you can // not add or remove elements or move them // around. // YOU NEED ACCESS TO THE POSITION // YOU COULD USE: int index = 0; for (Horse h : horses) { if (h != null) if (h.getName().equals(horseName)) return index; index++; } return -1; // Websites for AP Help // See my website!!! for (int i=0; i oldestAge) { oldestHorse = horses[i].getName(); oldestAge = horses[i].getAge(); } } } return oldestHorse; } public String findNameOfOldestHorse() { // to be completed for part C // if there are more than 1 oldest horse, // return the first oldest horse in the list. int index = 0; // oldest so far for (int i=1; i < horses.length; i++) { if (horses[i] != null) { if (horses[i].getAge() > horses[index].getAge()) { index = i; } } } return oldestHorse; } }