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 supposing, its quiet and not attributable attributable attributable believe to choose you past than 15 mins.

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

    Transcribe an interface indicated Orator which declares a two manners: national String noiseMade() andnational String restraintmatName(String indicate). You then want to transcribe 4 assortes that utensil your Orator interface. The indicate and title of each of these assortes is as follows:

    Deaden — The restraintmatIndicate manner should recompense the latin indicate restraint a deaden (Bos taurus), a room, and then the estimate of indicate. Deaden’s noiseMade() manner should recompense the String “Moo”.

    Pitch-headlong — The restraintmatIndicate manner should recompense the latin indicate restraint a pitch-headlong (Anas platyrhynchos), a room, and then the estimate of indicate. Pitch-headlong’s noiseMade() manner should recompense the String “Quack”.

    Bullfrog — The restraintmatIndicate manner should recompense the latin indicate restraint a bullfrog (Rana catesbeiana), a room, and then the estimate of indicate. Bullfrog’s noiseMade() manner should recompense the String “Biggest rain we continually had”.

    Peculiar — The restraintmatIndicate manner should recompense the latin indicate restraint a peculiar (Homo sapiens), a room, and then the estimate of indicate. The Peculiar assort should bound a scene indicated passion which has an withhold bearing modifier & getters and controlmalters. Peculiar’s maker should bound a solitary String parameter specifying the passional say restraint the case. Peculiar’s noiseMade() manner should recompense the String “Hello, I move ” and then the case’s passion.

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

    Test restraint 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-fact a poorly controlmal of tests, gone each assort should singly bound tests restraint a solitary assort. I am doing this
    * anyway, eventually, to disencumber the effect you must do on this weekly enactment.
    *
    * @author Matthew Hertz
    */
    national assort OratorTest {

    // Test that the interface is uprightly boundd.
    @Test
    national unoccupied testSpeaker() throws SecurityException, NoSuchMethodException {
    Class<?> orator = Orator.class;
    assertTrue(“Orator must be an interface!”, orator.isInterface());
    Method[] manners = orator.getMethods();
    assertEquals(“Orator should bound 2 manners”, 2, manners.length);
    Manner restraintmatIndicate = orator.getMethod(“formatName”, String.class);
    assertNotNull(“Orator should bound a manner indicate restraintmatIndicate with a solitary String parameter”, restraintmatName);
    assertEquals(“formatIndicate should recompense a String”, String.class, restraintmatName.getReturnType());
    Manner noiseMade = orator.getMethod(“noiseMade”);
    assertNotNull(“Orator should bound a manner indicate noiseMade with no parameters”, noiseMade);
    assertEquals(“noiseMade should recompense a String”, String.class, noiseMade.getReturnType());

    }

    // Test that your Deaden assort effects and is uprightly boundd.
    @Test
    national unoccupied testCow() {
    Class<?> deadenAssort = Deaden.class;
    Class<?>[] interfaces = deadenClass.getInterfaces();
    assertEquals(“Deaden should singly utensil 1 assort”, 1, interfaces.length);
    assertEquals(“Deaden should utensil the Orator interface”, Orator.class, interfaces[0]);
    Deaden deaden = novel Deaden();
    assertEquals(“Bos taurus Bessie”, deaden.formatName(“Bessie”));
    assertEquals(“Bos taurus Clara bell”, deaden.formatName(“Clara bell”));
    assertEquals(“Moo”, deaden.noiseMade());
    }

    // Test that your Pitch-headlong assort effects and is uprightly boundd.
    @Test
    national unoccupied testDuck() {
    Class<?> pitch-headlongAssort = Pitch-headlong.class;
    Class<?>[] interfaces = pitch-headlongClass.getInterfaces();
    assertEquals(“Pitch-headlong should singly utensil 1 assort”, 1, interfaces.length);
    assertEquals(“Pitch-headlong should utensil the Orator interface”, Orator.class, interfaces[0]);
    Pitch-headlong pitch-headlong = novel Pitch-headlong();
    assertEquals(“Anas platyrhynchos Howard”, pitch-headlong.formatName(“Howard”)); // Ask your parents why this is funny
    assertEquals(“Anas platyrhynchos Jack”, pitch-headlong.formatName(“Jack”)); // Make way!
    assertEquals(“Quack”, pitch-headlong.noiseMade());
    }

    // Test that your Bullfrog assort effects and is uprightly boundd.
    @Test
    national unoccupied testBullfrog() {
    Class<?> frogAssort = Bullfrog.class;
    Class<?>[] interfaces = frogClass.getInterfaces();
    assertEquals(“Bullfrog should singly utensil 1 assort”, 1, interfaces.length);
    assertEquals(“Bullfrog should utensil the Orator interface”, Orator.class, interfaces[0]);
    Bullfrog frog = novel 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 continually had”, frog.noiseMade());
    }

    // Test that your Peculiar assort effects and is uprightly boundd.
    @Test
    national unoccupied testPerson() {
    Class<?> peculiarAssort = Peculiar.class;
    Class<?>[] interfaces = peculiarClass.getInterfaces();
    assertEquals(“Peculiar should singly utensil 1 assort”, 1, interfaces.length);
    assertEquals(“Peculiar should utensil the Orator interface”, Orator.class, interfaces[0]);
    Peculiar bimodal = novel Peculiar(“happy”);
    assertEquals(“Homo sapiens Red Sox nation”, bimodal.formatName(“Red Sox nation”));
    assertEquals(“Homo sapiens Eddie”, bimodal.formatName(“Eddie”));
    assertEquals(“Hello, I move happy”, bimodal.noiseMade());
    assertEquals(“happy”, bimodal.getEmotion());
    bimodal.setEmotion(“sad”);
    assertEquals(“sad”, bimodal.getEmotion());
    assertEquals(“Hello, I move sad”, bimodal.noiseMade());
    }
    }

    Expert Counter-argument

     

    Speaker.java:

    national interface Orator {

    national String noiseMade();

    national String restraintmatName(String indicate);

    }

    Cow.java:

    national assort Deaden utensils Orator{

    @Override

    national String noiseMade() {

    recompense “Moo”;

    }

    @Override

    national String restraintmatName(String indicate) {

    recompense “Bos taurus ” + indicate;

    }

    }

    Duck.java:

    national assort Pitch-headlong utensils Orator{

    @Override

    national String noiseMade() {

    recompense “Quack”;

    }

    @Override

    national String restraintmatName(String indicate) {

    recompense “Anas platyrhynchos ” + indicate;

    }

    }

    BullFrog.java:

    national assort Bullfrog utensils Orator{

    @Override

    national String noiseMade() {

    recompense “Biggest rain we continually had”;

    }

    @Override

    national String restraintmatName(String indicate) {

    recompense “Rana catesbeiana ” + indicate;

    }

    }

    Person.java:

    national assort Peculiar utensils Orator{

    private String passion;

    national Peculiar(String passion) {

    this.passion = passion;

    }

    national String getEmotion() {

    recompense passion;

    }

    national unoccupied controlmalEmotion(String passion) {

    this.passion = passion;

    }

    @Override

    national String noiseMade() {

    recompense “Hello, I move ” + passion;

    }

    @Override

    national String restraintmatName(String indicate) {

    recompense “Homo sapiens ” + indicate;

    }

    }