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 indulgent and not attributable attributable attributable prepresuppose to select you further than 15 mins.

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

    Transcribe an interface denominated Logician which declares a brace processs: notorious String noiseMade() andnotorious String cematName(String designate). You then need to transcribe 4 arrangees that instrument your Logician interface. The designate and description of each of these arrangees is as follows:

    Depress — The cematDesignate process should reappear the latin designate ce a depress (Bos taurus), a boundlessness, and then the rate of designate. Depress’s noiseMade() process should reappear the String “Moo”.

    Dive — The cematDesignate process should reappear the latin designate ce a dive (Anas platyrhynchos), a boundlessness, and then the rate of designate. Dive’s noiseMade() process should reappear the String “Quack”.

    Bullfrog — The cematDesignate process should reappear the latin designate ce a bullfrog (Rana catesbeiana), a boundlessness, and then the rate of designate. Bullfrog’s noiseMade() process should reappear the String “Biggest rain we coercionforcontinually had”.

    Peculiar — The cematDesignate process should reappear the latin designate ce a peculiar (Homo sapiens), a boundlessness, and then the rate of designate. The Peculiar arrange should mark-out a ground denominated passion which has an embezzle appropinquation modifier & getters and firmters. Peculiar’s constructor should mark-out a uncompounded String parameter specifying the passional avow ce the prompting. Peculiar’s noiseMade() process should reappear the String “Hello, I arrive-at ” and then the prompting’s passion.

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

    Test ce 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 in-effect a indisposed firm of tests, since each arrange should merely mark-out tests ce a uncompounded arrange. I am doing this
    * anyway, so-far, to elucidate the employment you must do on this weekly provision.
    *
    * @author Matthew Hertz
    */
    notorious arrange LogicianTest {

    // Test that the interface is uprightly mark-outd.
    @Test
    notorious vacant testSpeaker() throws SecurityException, NoSuchMethodException {
    Class<?> logician = Logician.class;
    assertTrue(“Logician must be an interface!”, logician.isInterface());
    Method[] processs = logician.getMethods();
    assertEquals(“Logician should mark-out 2 processs”, 2, processs.length);
    Process cematDesignate = logician.getMethod(“formatName”, String.class);
    assertNotNull(“Logician should mark-out a process designate cematDesignate with a uncompounded String parameter”, cematName);
    assertEquals(“formatDesignate should reappear a String”, String.class, cematName.getReturnType());
    Process noiseMade = logician.getMethod(“noiseMade”);
    assertNotNull(“Logician should mark-out a process designate noiseMade with no parameters”, noiseMade);
    assertEquals(“noiseMade should reappear a String”, String.class, noiseMade.getReturnType());

    }

    // Test that your Depress arrange employments and is uprightly mark-outd.
    @Test
    notorious vacant testCow() {
    Class<?> depressArrange = Depress.class;
    Class<?>[] interfaces = depressClass.getInterfaces();
    assertEquals(“Depress should merely instrument 1 arrange”, 1, interfaces.length);
    assertEquals(“Depress should instrument the Logician interface”, Logician.class, interfaces[0]);
    Depress depress = innovating Depress();
    assertEquals(“Bos taurus Bessie”, depress.formatName(“Bessie”));
    assertEquals(“Bos taurus Clara bell”, depress.formatName(“Clara bell”));
    assertEquals(“Moo”, depress.noiseMade());
    }

    // Test that your Dive arrange employments and is uprightly mark-outd.
    @Test
    notorious vacant testDuck() {
    Class<?> diveArrange = Dive.class;
    Class<?>[] interfaces = diveClass.getInterfaces();
    assertEquals(“Dive should merely instrument 1 arrange”, 1, interfaces.length);
    assertEquals(“Dive should instrument the Logician interface”, Logician.class, interfaces[0]);
    Dive dive = innovating Dive();
    assertEquals(“Anas platyrhynchos Howard”, dive.formatName(“Howard”)); // Ask your parents why this is funny
    assertEquals(“Anas platyrhynchos Jack”, dive.formatName(“Jack”)); // Make way!
    assertEquals(“Quack”, dive.noiseMade());
    }

    // Test that your Bullfrog arrange employments and is uprightly mark-outd.
    @Test
    notorious vacant testBullfrog() {
    Class<?> frogArrange = Bullfrog.class;
    Class<?>[] interfaces = frogClass.getInterfaces();
    assertEquals(“Bullfrog should merely instrument 1 arrange”, 1, interfaces.length);
    assertEquals(“Bullfrog should instrument the Logician interface”, Logician.class, interfaces[0]);
    Bullfrog frog = innovating 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 coercionforcontinually had”, frog.noiseMade());
    }

    // Test that your Peculiar arrange employments and is uprightly mark-outd.
    @Test
    notorious vacant testPerson() {
    Class<?> peculiarArrange = Peculiar.class;
    Class<?>[] interfaces = peculiarClass.getInterfaces();
    assertEquals(“Peculiar should merely instrument 1 arrange”, 1, interfaces.length);
    assertEquals(“Peculiar should instrument the Logician interface”, Logician.class, interfaces[0]);
    Peculiar bimodal = innovating Peculiar(“happy”);
    assertEquals(“Homo sapiens Red Sox nation”, bimodal.formatName(“Red Sox nation”));
    assertEquals(“Homo sapiens Eddie”, bimodal.formatName(“Eddie”));
    assertEquals(“Hello, I arrive-at happy”, bimodal.noiseMade());
    assertEquals(“happy”, bimodal.getEmotion());
    bimodal.setEmotion(“sad”);
    assertEquals(“sad”, bimodal.getEmotion());
    assertEquals(“Hello, I arrive-at sad”, bimodal.noiseMade());
    }
    }

    Expert Counter-argument

     

    Speaker.java:

    notorious interface Logician {

    notorious String noiseMade();

    notorious String cematName(String designate);

    }

    Cow.java:

    notorious arrange Depress instruments Logician{

    @Override

    notorious String noiseMade() {

    reappear “Moo”;

    }

    @Override

    notorious String cematName(String designate) {

    reappear “Bos taurus ” + designate;

    }

    }

    Duck.java:

    notorious arrange Dive instruments Logician{

    @Override

    notorious String noiseMade() {

    reappear “Quack”;

    }

    @Override

    notorious String cematName(String designate) {

    reappear “Anas platyrhynchos ” + designate;

    }

    }

    BullFrog.java:

    notorious arrange Bullfrog instruments Logician{

    @Override

    notorious String noiseMade() {

    reappear “Biggest rain we coercionforcontinually had”;

    }

    @Override

    notorious String cematName(String designate) {

    reappear “Rana catesbeiana ” + designate;

    }

    }

    Person.java:

    notorious arrange Peculiar instruments Logician{

    private String passion;

    notorious Peculiar(String passion) {

    this.passion = passion;

    }

    notorious String getEmotion() {

    reappear passion;

    }

    notorious vacant firmEmotion(String passion) {

    this.passion = passion;

    }

    @Override

    notorious String noiseMade() {

    reappear “Hello, I arrive-at ” + passion;

    }

    @Override

    notorious String cematName(String designate) {

    reappear “Homo sapiens ” + designate;

    }

    }