@Test public void testBirdWalkingIsSame() { // Find the animals in the wild. Animal animal = new Animal(); Bird bird = new Bird(); // Observe the way they walk. animal.walk(); bird.walk(); // If they walk the same, maybe they are the same. String[] actual = stdout.toString().split("\n"); assertEquals(actual[0], actual[1]); }
// The main method is the point of entry into the program... public static void main(String[] args) { Animal a = new Animal(); System.out.println(a.numberOfLegs); System.out.println(a.hasWings); a.talk(); // a.fly(); method doesn't exist in superclass Bird b = new Bird(); System.out.println(b.numberOfLegs); System.out.println(b.hasWings); System.out.println(b.canFly); // System.out.println(b.numberOfKills); variable doesn't exist in superclass b.talk(); // b.attack(); method doesn't exist in superclass Eagle e = new Eagle(); // System.out.println(e.numberOfKills); variable is private System.out.println(e.numberOfLegs); System.out.println(e.hasWings); e.talk(); e.attack(); a = b; a.talk(); // a.fly(); b = a; b.talk(); b.fly(); }
@Test public void testBasicFetch() throws Exception { this.testRegistrationForwards(); Animal a = new Animal(); a.name = "Bob"; Animal a2 = ofy().saveClearLoad(a); assert a.name.equals(a2.name); Mammal m = new Mammal(); m.name = "Bob"; m.longHair = true; Mammal m2 = ofy().saveClearLoad(m); assert m.name.equals(m2.name); assert m.longHair == m2.longHair; Cat c = new Cat(); c.name = "Bob"; c.longHair = true; c.hypoallergenic = true; Cat c2 = ofy().saveClearLoad(c); assert c.name.equals(c2.name); assert c.longHair == c2.longHair; assert c.hypoallergenic == c2.hypoallergenic; }
public Animal getElement(int index) { Animal ani = animals.get(index); switch (ani.getSort()) { case DEER: Deer deer = (Deer) ani; return (Deer) deer.clone(); case DOG: Dog dog = (Dog) ani; return (Dog) dog.clone(); case GIFRAFFE: Gifraffe gifraffe = (Gifraffe) ani; return (Gifraffe) gifraffe.clone(); case HORSE: Horse horse = (Horse) ani; return (Horse) horse.clone(); case LION: Lion lion = (Lion) ani; return (Lion) lion.clone(); case WOLF: Wolf wolf = (Wolf) ani; return (Wolf) wolf.clone(); case CHEETAH: Cheetah cheetah = (Cheetah) ani; return (Cheetah) cheetah.clone(); default: // Don't know that type of creature. return (Animal) ani.clone(); } }
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Animal animal = animals.get(position); Toast.makeText( getApplicationContext(), "Animal clicked : " + animal.getName(), Toast.LENGTH_SHORT) .show(); }
/** @return the animals */ public ArrayList<Animal> getAnimals() { ArrayList<Animal> a = new ArrayList<>(); for (Animal animal : animals) { a.add((Animal) animal.clone()); } return a; }
public static void main(String[] args) { Animal a = new Animal(); Animal b = new Dog(); // b 是Animal 的一个引用 不用调用父类没有的方法 要想调用自己的方法 则需要创建本类的对象 a.move(); b.move(); Dog dog = new Dog(); dog.bark(); }
public void testParameterCanBePassedToConstructor() throws Exception { DefaultPicoContainer pico = new DefaultPicoContainer(); pico.addComponent(Animal.class, Dino.class, new ConstantParameter("bones")); Animal animal = pico.getComponent(Animal.class); assertNotNull("Component not null", animal); assertEquals("bones", animal.getFood()); }
public void testParameterCanBePrimitive() throws Exception { DefaultPicoContainer pico = new DefaultPicoContainer(); pico.addComponent(Animal.class, Dino2.class, new ConstantParameter(22)); Animal animal = pico.getComponent(Animal.class); assertNotNull("Component not null", animal); assertEquals("22", animal.getFood()); }
public void addElement(int index, Animal animal) { if (index < 0 || index >= animals.size()) { index = 0; } AnimalSort sort = animal.getSort(); animal.setID(sort.getAbbrv() + (++animalCount[sort.ordinal()])); this.animals.add(index, animal); }
public void testMultipleParametersCanBePassed() throws Exception { DefaultPicoContainer pico = new DefaultPicoContainer(); pico.addComponent( Animal.class, Dino3.class, new ConstantParameter("a"), new ConstantParameter("b")); Animal animal = pico.getComponent(Animal.class); assertNotNull("Component not null", animal); assertEquals("ab", animal.getFood()); }
// ==============================add_amigo public void add_amigo(Animal a) { if (a.get_classe() != "Mamifero") { super.add_amigo(a); } else { System.out.printf( "Eu %s, um %s não gosto de %s, %s não será meu amigo!\n", nome, especie, a.get_classe(), a.get_nome()); } }
public static String talk(Animal a) { if (a instanceof Cow) { return "I am a " + a.kind + " and I go " + a.speak(); } else if (a instanceof Cat) { return "I am a " + a.kind + " and I go " + a.speak(); } else if (a instanceof Pig) { return "I am a " + a.kind + " and I go " + a.speak(); } else { return "I don't know what I am"; } }
/** * This seems reasonable behavior, better than filtering mismatched values out of the result. We * like this behavior better because it provides a more clear explanation to the user of what went * wrong - the CCE is pretty explicit about the classes involved. If we returned null folks would * think the data wasn't in the db. */ @Test(expectedExceptions = ClassCastException.class) public void testFetchMismatch() throws Exception { this.testRegistrationForwards(); Animal a = new Animal(); a.name = "Bob"; ofy().save().entity(a).now(); // This should exclude the value @SuppressWarnings("unused") Mammal m = ofy().load().type(Mammal.class).id(a.id).now(); }
public static void main(String[] args) { Collection<Animal> animals = new ArrayList<Animal>(); animals.add(new Cat("Cat", "Meow Meow")); animals.add(new Dog("Dog", "Arf Arf Arf")); animals.add(new Dog("Dog", "Arffff Arf Arf")); for (Animal a : animals) { System.out.println(a.getName() + " " + a.talk()); } // Cat a = new Cat("Cat","Meow Meow"); // Dog b = new Dog("Dog","Arf Arf Arf"); // System.out.println(a.getName()+" "+a.talk() ); // System.out.println(b.getName()+" "+b.talk() ); }
public void testParametersCanBeMixedWithComponentsCanBePassed() throws Exception { DefaultPicoContainer pico = new DefaultPicoContainer(); pico.addComponent(Touchable.class, SimpleTouchable.class); pico.addComponent( Animal.class, Dino4.class, new ConstantParameter("a"), new ConstantParameter(3), new ConstantParameter("b"), ComponentParameter.DEFAULT); Animal animal = pico.getComponent(Animal.class); assertNotNull("Component not null", animal); assertEquals("a3b org.picocontainer.testmodel.SimpleTouchable", animal.getFood()); }
public static void advanceAll(ArrayList<Animal> animals) { ArrayList<Scorpion> scorps = new ArrayList<Scorpion>(); for (Animal a : animals) { a.advance(); if (a.getClass() == Scorpion.class) { scorps.add((Scorpion) a); } } for (Scorpion s : scorps) { if (s.unluckyFellow != -1) { System.out.println(animals.get(s.unluckyFellow).getName() + " has been stung!"); animals.remove(s.unluckyFellow); } } }
public void vision(SimState state, SparseGrid2D grid) { // System.out.println("Direction: " + direction); // Visual Processor Int2D cord = grid.getObjectLocation(this); assert (vP != null && cord != null); assert (direction != 0); // System.out.println(this + "was at location: " + cord); if (cord != null) { seen = vP.sight(cord.x, cord.y, state, direction); Bag locations = new Bag(); // Testing Print Statements for (int s = 0; s < seen.size(); s++) { Object j = seen.get(s); // System.out.print(s + "saw " + seen.get(s)); Int2D obLoc = grid.getObjectLocation(j); locations.add(obLoc); // System.out.println(" at location:" + obLoc); // if(j.equals(Prey.class)) // System.out.println("****" + seen.get(s)); } // end of for loop this.behaviorProb(locations, seen); // Move every timestep super.move(grid, state); } } // end of vision
public Client(Animal animal) { Animal newAnimal = animal.createCopy(); System.out.println(animal); System.out.println(newAnimal); }
public static void function(Animal a) { a.eat(); if (a instanceof Cat) { Cat c = (Cat) a; // 强制将父类的引用,转成子类类型。向下转型。 c.catchMouse(); } }
/** @param args */ public static void main(String[] args) { Animal animal1 = new Animal("Wuffi", "dog", 12, Gender.MALE); System.out.println("Geschlecht: " + animal1.getGender()); System.out.println("Aktuelles Alter :" + animal1.getAge()); System.out.println("Und nach einem Jahr: " + animal1.birthday()); Animal animal2 = new Animal("Lala", "dog", 5, Gender.FEMALE); System.out.println("Geschlecht: " + animal2.getGender()); System.out.println("Aktuelles Alter: " + animal2.getAge()); System.out.println("Und nach einem Jahr: " + animal2.birthday()); }
@Test @Category(RequiresCloudant.class) public void QuorumTests() { db.save(new Animal("human"), 2); Animal h = db.find(Animal.class, "human", new com.cloudant.client.api.model.Params().readQuorum(2)); assertNotNull(h); assertEquals("human", h.getId()); db.update(h.setClass("inhuman"), 2); h = db.find(Animal.class, "human", new com.cloudant.client.api.model.Params().readQuorum(2)); assertEquals("inhuman", h.getclass()); db.post(new Animal("test"), 2); h = db.find(Animal.class, "test", new com.cloudant.client.api.model.Params().readQuorum(3)); assertEquals("test", h.getId()); }
public void enqueue(Animal a) { a.setOrder(order); order++; if (a instanceof Dog) { dogs.addLast((Dog) a); } else if (a instanceof Cat) { cats.addLast((Cat) a); } }
@Test public void testUpdateOnAnimal() { TestData data = new TestData(); data.prepare(); Session s = openSession(); Transaction t = s.beginTransaction(); int count = s.createQuery("update Animal set description = description where description = :desc") .setString("desc", data.frog.getDescription()) .executeUpdate(); assertEquals("Incorrect entity-updated count", 1, count); count = s.createQuery("update Animal set description = :newDesc where description = :desc") .setString("desc", data.polliwog.getDescription()) .setString("newDesc", "Tadpole") .executeUpdate(); assertEquals("Incorrect entity-updated count", 1, count); Animal tadpole = (Animal) s.load(Animal.class, data.polliwog.getId()); assertEquals("Update did not take effect", "Tadpole", tadpole.getDescription()); count = s.createQuery("update Animal set bodyWeight = bodyWeight + :w1 + :w2") .setDouble("w1", 1) .setDouble("w2", 2) .executeUpdate(); assertEquals("incorrect count on 'complex' update assignment", count, 6); if (!(getDialect() instanceof MySQLDialect)) { // MySQL does not support (even un-correlated) subqueries against the update-mutating table s.createQuery("update Animal set bodyWeight = ( select max(bodyWeight) from Animal )") .executeUpdate(); } t.commit(); s.close(); data.cleanup(); }
private void tryIndividualsWithConfiguration(JSONConfiguration configuration) throws Exception { final JSONJAXBContext ctx = new JSONJAXBContext(configuration, AnimalList.class, Animal.class, Dog.class, Cat.class); final JSONMarshaller jm = ctx.createJSONMarshaller(); final JSONUnmarshaller ju = ctx.createJSONUnmarshaller(); Animal animalTwo; for (int i = 0; i < one.animals.size(); i++) { final StringWriter sw = new StringWriter(); Animal animalOne = one.animals.get(i); jm.marshallToJSON(animalOne, sw); System.out.println(String.format("Marshalled: %s", sw)); animalTwo = ju.unmarshalFromJSON(new StringReader(sw.toString()), Animal.class); assertEquals(animalOne, animalTwo); System.out.println( String.format( "class one = %s; class two = %s", animalOne.getClass(), animalTwo.getClass())); assertEquals(animalOne.getClass(), animalTwo.getClass()); } }
public static void main(String args[]) { animals.add(new Dog()); animals.add(new Cat("Sam")); animals.add(new Cat("Ajax")); animals.add(new Bird("Alphonsus")); animals.add(new Bird("Clive")); animals.add(new Turtle("Herm")); animals.add(new Hare("Harry")); animals.add(new Horse("Horsey")); animals.add(new Scorpion("Scorp")); boolean running = true; while (running) { Scanner scan = new Scanner(System.in); System.out.println("--------------------------------------------------------------"); for (Animal a : animals) { for (int j = 0; j < 40; j++) { if (a.getPosition() == j) { System.out.print("*"); } else { System.out.print(" "); } } System.out.print("|"); System.out.print(a.getName() + "\n"); } System.out.println("--------------------------------------------------------------"); int winner = checkWinner(animals); if (winner != -1) { System.out.println(animals.get(winner).getName() + " wins!"); running = false; } scan.nextLine(); advanceAll(animals); } }
public static void main(String[] args) { // Upcast Cat cat = new Cat(); Animal animal = cat; animal.sing(); // Downcast Animal a = new Cat(); Cat c = (Cat) a; c.sing(); c.eat(); // 编译错误 // 用父类引用调用父类不存在的方法 // Animal a1 = new Cat(); // a1.eat(); // 编译错误 // 向下类型转换时只能转向指向的对象类型 // Animal a2 = new Cat(); // Cat c2 = (Dog)a2; }
public static void main(String[] args) { Animal a = new Animal("Pluto", 10); Cat c = new Cat("Garfield", 6); Dog d = new Dog("Fido", 4); a.greet(); // (A) "Animal Pluto says: Huh? c.greet(); // (B) Cat Garfield says: Meow!______________________ // c = Cat@54387593 d.greet(); // (C) Dog Fido says: WOOF! a = c; // a = Cat@54387593 ((Cat) a).greet(); // (D) Cat Garfield says: Meow! // a's static type: Animal // a's dynamic type: Cat a.greet(); // (E) Cat Garfield says: Meow! a = new Dog("Hieronymus", 10); d = (Dog) a; a = new Dog("Spot", 10); d = a; }
@Override @Before public void setUp() throws Exception { super.setUp(); elephant = new Animal(); elephant.setId(1); elephant.setName("Elephant"); elephant.setType("Mammal"); spider = new Animal(); spider.setId(2); spider.setName("Spider"); spider.setType("Insect"); bear = new Animal(); bear.setId(3); bear.setName("Bear"); bear.setType("Mammal"); }
/** Create the frame. */ public AnimalListGUI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JTextArea txtrAnimals = new JTextArea(); txtrAnimals.setText(""); contentPane.add(txtrAnimals, BorderLayout.CENTER); // Manual addition of animals to a new ArrayList ArrayList<Animal> animals = new ArrayList<Animal>(); animals.add(new Dog("Canis latrans", "Fido", 3, true)); animals.add(new Snake("Python regius", "Otto", true)); animals.add(new Dog("Canis latrans", "Molly", 3, false)); animals.add(new Cat("Lynx pardinus", "Julia", 4, 99)); animals.add(new Cat("Lynx Lynx", "Felcia", 4, 8)); // Print list of animals in text field. for (Animal animal : animals) { txtrAnimals.append(animal.getInfo() + "\n"); } }