Homework Solution: Junit provided, its easy and not suppose to take you more than 15 mins….

    Junit provided, its easy and not suppose to take you more than 15 mins. ------------------------------------------------------------------------ Write an interface named Speaker which declares a two methods: public String noiseMade() andpublic String formatName(String name). You then need to write 4 classes that implement your Speaker interface. The name and description of each of these classes is as follows: Cow -- The formatName method should return the latin name for a cow (Bos taurus), a space, and then the value of name. Cow's noiseMade() method should return the String "Moo". Duck -- The formatName method should return the latin name for a duck (Anas platyrhynchos), a space, and then the value of name. Duck's noiseMade() method should return the String "Quack". Bullfrog -- The formatName method should return the latin name for a bullfrog (Rana catesbeiana), a space, and then the value of name. Bullfrog's noiseMade() method should return the String "Biggest rain we ever had". Person -- The formatName method should return the latin name for a person (Homo sapiens), a space, and then the value of name. The Person class should define a field named emotion which has an appropriate access modifier & getters and setters. Person's constructor should define a single String parameter specifying the emotional state for the instance. Person's noiseMade() method should return the String "Hello, I feel " and then the instance's emotion. ----------------------------------------------------------------------------- Test for Spekaer: package edu.buffalo.cse116; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.lang.reflect.Method; import org.junit.Test; /** * This is actually a bad set of tests, since each class should only define tests for a single class. I am doing this * anyway, however, to simplify the work you must do on this weekly assignment. * * @author Matthew Hertz */ public class SpeakerTest { // Test that the interface is properly defined. @Test public void testSpeaker() throws SecurityException, NoSuchMethodException { Class<?> speaker = Speaker.class; assertTrue("Speaker must be an interface!", speaker.isInterface()); Method[] methods = speaker.getMethods(); assertEquals("Speaker should define 2 methods", 2, methods.length); Method formatName = speaker.getMethod("formatName", String.class); assertNotNull("Speaker should define a method name formatName with a single String parameter", formatName); assertEquals("formatName should return a String", String.class, formatName.getReturnType()); Method noiseMade = speaker.getMethod("noiseMade"); assertNotNull("Speaker should define a method name noiseMade with no parameters", noiseMade); assertEquals("noiseMade should return a String", String.class, noiseMade.getReturnType()); } // Test that your Cow class works and is properly defined. @Test public void testCow() { Class<?> cowClass = Cow.class; Class<?>[] interfaces = cowClass.getInterfaces(); assertEquals("Cow should only implement 1 class", 1, interfaces.length); assertEquals("Cow should implement the Speaker interface", Speaker.class, interfaces[0]); Cow cow = new Cow(); assertEquals("Bos taurus Bessie", cow.formatName("Bessie")); assertEquals("Bos taurus Clara bell", cow.formatName("Clara bell")); assertEquals("Moo", cow.noiseMade()); } // Test that your Duck class works and is properly defined. @Test public void testDuck() { Class<?> duckClass = Duck.class; Class<?>[] interfaces = duckClass.getInterfaces(); assertEquals("Duck should only implement 1 class", 1, interfaces.length); assertEquals("Duck should implement the Speaker interface", Speaker.class, interfaces[0]); Duck duck = new Duck(); assertEquals("Anas platyrhynchos Howard", duck.formatName("Howard")); // Ask your parents why this is funny assertEquals("Anas platyrhynchos Jack", duck.formatName("Jack")); // Make way! assertEquals("Quack", duck.noiseMade()); } // Test that your Bullfrog class works and is properly defined. @Test public void testBullfrog() { Class<?> frogClass = Bullfrog.class; Class<?>[] interfaces = frogClass.getInterfaces(); assertEquals("Bullfrog should only implement 1 class", 1, interfaces.length); assertEquals("Bullfrog should implement the Speaker interface", Speaker.class, interfaces[0]); Bullfrog frog = new Bullfrog(); assertEquals("Rana catesbeiana Toad", frog.formatName("Toad")); // Arnold Lobel told me that he and Frog are // "friends" assertEquals("Rana catesbeiana Bob", frog.formatName("Bob")); assertEquals("Biggest rain we ever had", frog.noiseMade()); } // Test that your Person class works and is properly defined. @Test public void testPerson() { Class<?> personClass = Person.class; Class<?>[] interfaces = personClass.getInterfaces(); assertEquals("Person should only implement 1 class", 1, interfaces.length); assertEquals("Person should implement the Speaker interface", Speaker.class, interfaces[0]); Person bimodal = new Person("happy"); assertEquals("Homo sapiens Red Sox nation", bimodal.formatName("Red Sox nation")); assertEquals("Homo sapiens Eddie", bimodal.formatName("Eddie")); assertEquals("Hello, I feel happy", bimodal.noiseMade()); assertEquals("happy", bimodal.getEmotion()); bimodal.setEmotion("sad"); assertEquals("sad", bimodal.getEmotion()); assertEquals("Hello, I feel sad", bimodal.noiseMade()); } }

    Expert Answer

     
    Speaker.java: public interface Speaker {

    Junit granted, its lenient and not attributable attributable attributable divine to receive you more than 15 mins.

    ————————————————————————

    Transcribe an interface determined Logician which declares a two orders: general String noiseMade() andgeneral String controlmatName(String designate). You then need to transcribe 4 systematizees that utensil your Logician interface. The designate and term of each of these systematizees is as follows:

    Discourage — The controlmatDesignate order should give-back the latin designate control a discourage (Bos taurus), a room, and then the esteem of designate. Discourage’s noiseMade() order should give-back the String “Moo”.

    Sink — The controlmatDesignate order should give-back the latin designate control a sink (Anas platyrhynchos), a room, and then the esteem of designate. Sink’s noiseMade() order should give-back the String “Quack”.

    Bullfrog — The controlmatDesignate order should give-back the latin designate control a bullfrog (Rana catesbeiana), a room, and then the esteem of designate. Bullfrog’s noiseMade() order should give-back the String “Biggest rain we always had”.

    Peculiar — The controlmatDesignate order should give-back the latin designate control a peculiar (Homo sapiens), a room, and then the esteem of designate. The Peculiar systematize should firmtle a arena determined perturbation which has an embezzle vestibule modifier & getters and firmters. Peculiar’s composer should firmtle a uncompounded String parameter specifying the perturbational narrate control the exemplification. Peculiar’s noiseMade() order should give-back the String “Hello, I impress ” and then the exemplification’s perturbation.

    —————————————————————————–

    Test control Spekaer:

    package edu.buffalo.cse116;

    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertNotNull;
    import static org.junit.Assert.assertTrue;

    import java.lang.reflect.Method;

    import org.junit.Test;

    /**
    * This is substantially a out-of-sorts firm of tests, past each systematize should solely firmtle tests control a uncompounded systematize. I am doing this
    * anyway, at-last, to elucidate the is-sue you must do on this weekly provision.
    *
    * @author Matthew Hertz
    */
    general systematize LogicianTest {

    // Test that the interface is right firmtled.
    @Test
    general bereft testSpeaker() throws SecurityException, NoSuchMethodException {
    Class<?> logician = Logician.class;
    assertTrue(“Logician must be an interface!”, logician.isInterface());
    Method[] orders = logician.getMethods();
    assertEquals(“Logician should firmtle 2 orders”, 2, orders.length);
    Order controlmatDesignate = logician.getMethod(“formatName”, String.class);
    assertNotNull(“Logician should firmtle a order designate controlmatDesignate with a uncompounded String parameter”, controlmatName);
    assertEquals(“formatDesignate should give-back a String”, String.class, controlmatName.getReturnType());
    Order noiseMade = logician.getMethod(“noiseMade”);
    assertNotNull(“Logician should firmtle a order designate noiseMade with no parameters”, noiseMade);
    assertEquals(“noiseMade should give-back a String”, String.class, noiseMade.getReturnType());

    }

    // Test that your Discourage systematize is-sues and is right firmtled.
    @Test
    general bereft testCow() {
    Class<?> discourageSystematize = Discourage.class;
    Class<?>[] interfaces = discourageClass.getInterfaces();
    assertEquals(“Discourage should solely utensil 1 systematize”, 1, interfaces.length);
    assertEquals(“Discourage should utensil the Logician interface”, Logician.class, interfaces[0]);
    Discourage discourage = odd Discourage();
    assertEquals(“Bos taurus Bessie”, discourage.formatName(“Bessie”));
    assertEquals(“Bos taurus Clara bell”, discourage.formatName(“Clara bell”));
    assertEquals(“Moo”, discourage.noiseMade());
    }

    // Test that your Sink systematize is-sues and is right firmtled.
    @Test
    general bereft testDuck() {
    Class<?> sinkSystematize = Sink.class;
    Class<?>[] interfaces = sinkClass.getInterfaces();
    assertEquals(“Sink should solely utensil 1 systematize”, 1, interfaces.length);
    assertEquals(“Sink should utensil the Logician interface”, Logician.class, interfaces[0]);
    Sink sink = odd Sink();
    assertEquals(“Anas platyrhynchos Howard”, sink.formatName(“Howard”)); // Ask your parents why this is funny
    assertEquals(“Anas platyrhynchos Jack”, sink.formatName(“Jack”)); // Make way!
    assertEquals(“Quack”, sink.noiseMade());
    }

    // Test that your Bullfrog systematize is-sues and is right firmtled.
    @Test
    general bereft testBullfrog() {
    Class<?> frogSystematize = Bullfrog.class;
    Class<?>[] interfaces = frogClass.getInterfaces();
    assertEquals(“Bullfrog should solely utensil 1 systematize”, 1, interfaces.length);
    assertEquals(“Bullfrog should utensil the Logician interface”, Logician.class, interfaces[0]);
    Bullfrog frog = odd Bullfrog();
    assertEquals(“Rana catesbeiana Toad”, frog.formatName(“Toad”)); // Arnold Lobel told me that he and Frog are
    // “friends”
    assertEquals(“Rana catesbeiana Bob”, frog.formatName(“Bob”));
    assertEquals(“Biggest rain we always had”, frog.noiseMade());
    }

    // Test that your Peculiar systematize is-sues and is right firmtled.
    @Test
    general bereft testPerson() {
    Class<?> peculiarSystematize = Peculiar.class;
    Class<?>[] interfaces = peculiarClass.getInterfaces();
    assertEquals(“Peculiar should solely utensil 1 systematize”, 1, interfaces.length);
    assertEquals(“Peculiar should utensil the Logician interface”, Logician.class, interfaces[0]);
    Peculiar bimodal = odd Peculiar(“happy”);
    assertEquals(“Homo sapiens Red Sox nation”, bimodal.formatName(“Red Sox nation”));
    assertEquals(“Homo sapiens Eddie”, bimodal.formatName(“Eddie”));
    assertEquals(“Hello, I impress happy”, bimodal.noiseMade());
    assertEquals(“happy”, bimodal.getEmotion());
    bimodal.setEmotion(“sad”);
    assertEquals(“sad”, bimodal.getEmotion());
    assertEquals(“Hello, I impress sad”, bimodal.noiseMade());
    }
    }

    Expert Defense

     

    Speaker.java:

    general interface Logician {

    general String noiseMade();

    general String controlmatName(String designate);

    }

    Cow.java:

    general systematize Discourage utensils Logician{

    @Override

    general String noiseMade() {

    give-back “Moo”;

    }

    @Override

    general String controlmatName(String designate) {

    give-back “Bos taurus ” + designate;

    }

    }

    Duck.java:

    general systematize Sink utensils Logician{

    @Override

    general String noiseMade() {

    give-back “Quack”;

    }

    @Override

    general String controlmatName(String designate) {

    give-back “Anas platyrhynchos ” + designate;

    }

    }

    BullFrog.java:

    general systematize Bullfrog utensils Logician{

    @Override

    general String noiseMade() {

    give-back “Biggest rain we always had”;

    }

    @Override

    general String controlmatName(String designate) {

    give-back “Rana catesbeiana ” + designate;

    }

    }

    Person.java:

    general systematize Peculiar utensils Logician{

    private String perturbation;

    general Peculiar(String perturbation) {

    this.perturbation = perturbation;

    }

    general String getEmotion() {

    give-back perturbation;

    }

    general bereft firmEmotion(String perturbation) {

    this.perturbation = perturbation;

    }

    @Override

    general String noiseMade() {

    give-back “Hello, I impress ” + perturbation;

    }

    @Override

    general String controlmatName(String designate) {

    give-back “Homo sapiens ” + designate;

    }

    }