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

     
    import java.io.*; interface Speaker{

    Junit granted, its quiet and not attributable attributable attributable imagine to admit you more than 15 mins.

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

    Transcribe an interface determined Orator which declares a two rules: notorious String noiseMade() andnotorious String coercionmatName(String call). You then insufficiency to transcribe 4 arrangees that instrument your Orator interface. The call and style of each of these arrangees is as follows:

    Depress — The coercionmatCall rule should reappear the latin call coercion a depress (Bos taurus), a illimitableness, and then the appreciate of call. Depress’s noiseMade() rule should reappear the String “Moo”.

    Douse — The coercionmatCall rule should reappear the latin call coercion a douse (Anas platyrhynchos), a illimitableness, and then the appreciate of call. Douse’s noiseMade() rule should reappear the String “Quack”.

    Bullfrog — The coercionmatCall rule should reappear the latin call coercion a bullfrog (Rana catesbeiana), a illimitableness, and then the appreciate of call. Bullfrog’s noiseMade() rule should reappear the String “Biggest rain we always had”.

    Special — The coercionmatCall rule should reappear the latin call coercion a special (Homo sapiens), a illimitableness, and then the appreciate of call. The Special arrange should determinedtle a room determined passion which has an misspend admission modifier & getters and determinedters. Special’s perpetrator should determinedtle a unmarried String parameter specifying the passional narrate coercion the solicitation. Special’s noiseMade() rule should reappear the String “Hello, I reach ” and then the solicitation’s passion.

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

    Test coercion 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 out-of-sorts determined of tests, past each arrange should simply determinedtle tests coercion a unmarried arrange. I am doing this
    * anyway, so-far, to disencumber the toil you must do on this weekly assignment.
    *
    * @author Matthew Hertz
    */
    notorious arrange OratorTest {

    // Test that the interface is rightly determinedtled.
    @Test
    notorious bereft testSpeaker() throws SecurityException, NoSuchMethodException {
    Class<?> orator = Orator.class;
    assertTrue(“Orator must be an interface!”, orator.isInterface());
    Method[] rules = orator.getMethods();
    assertEquals(“Orator should determinedtle 2 rules”, 2, rules.length);
    Rule coercionmatCall = orator.getMethod(“formatName”, String.class);
    assertNotNull(“Orator should determinedtle a rule call coercionmatCall with a unmarried String parameter”, coercionmatName);
    assertEquals(“formatCall should reappear a String”, String.class, coercionmatName.getReturnType());
    Rule noiseMade = orator.getMethod(“noiseMade”);
    assertNotNull(“Orator should determinedtle a rule call noiseMade with no parameters”, noiseMade);
    assertEquals(“noiseMade should reappear a String”, String.class, noiseMade.getReturnType());

    }

    // Test that your Depress arrange toils and is rightly determinedtled.
    @Test
    notorious bereft testCow() {
    Class<?> depressArrange = Depress.class;
    Class<?>[] interfaces = depressClass.getInterfaces();
    assertEquals(“Depress should simply instrument 1 arrange”, 1, interfaces.length);
    assertEquals(“Depress should instrument the Orator interface”, Orator.class, interfaces[0]);
    Depress depress = odd Depress();
    assertEquals(“Bos taurus Bessie”, depress.formatName(“Bessie”));
    assertEquals(“Bos taurus Clara bell”, depress.formatName(“Clara bell”));
    assertEquals(“Moo”, depress.noiseMade());
    }

    // Test that your Douse arrange toils and is rightly determinedtled.
    @Test
    notorious bereft testDuck() {
    Class<?> douseArrange = Douse.class;
    Class<?>[] interfaces = douseClass.getInterfaces();
    assertEquals(“Douse should simply instrument 1 arrange”, 1, interfaces.length);
    assertEquals(“Douse should instrument the Orator interface”, Orator.class, interfaces[0]);
    Douse douse = odd Douse();
    assertEquals(“Anas platyrhynchos Howard”, douse.formatName(“Howard”)); // Ask your parents why this is funny
    assertEquals(“Anas platyrhynchos Jack”, douse.formatName(“Jack”)); // Make way!
    assertEquals(“Quack”, douse.noiseMade());
    }

    // Test that your Bullfrog arrange toils and is rightly determinedtled.
    @Test
    notorious bereft testBullfrog() {
    Class<?> frogArrange = Bullfrog.class;
    Class<?>[] interfaces = frogClass.getInterfaces();
    assertEquals(“Bullfrog should simply instrument 1 arrange”, 1, interfaces.length);
    assertEquals(“Bullfrog should instrument the Orator interface”, Orator.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 Special arrange toils and is rightly determinedtled.
    @Test
    notorious bereft testPerson() {
    Class<?> specialArrange = Special.class;
    Class<?>[] interfaces = specialClass.getInterfaces();
    assertEquals(“Special should simply instrument 1 arrange”, 1, interfaces.length);
    assertEquals(“Special should instrument the Orator interface”, Orator.class, interfaces[0]);
    Special bimodal = odd Special(“happy”);
    assertEquals(“Homo sapiens Red Sox nation”, bimodal.formatName(“Red Sox nation”));
    assertEquals(“Homo sapiens Eddie”, bimodal.formatName(“Eddie”));
    assertEquals(“Hello, I reach happy”, bimodal.noiseMade());
    assertEquals(“happy”, bimodal.getEmotion());
    bimodal.setEmotion(“sad”);
    assertEquals(“sad”, bimodal.getEmotion());
    assertEquals(“Hello, I reach sad”, bimodal.noiseMade());
    }
    }

    Expert Apology

     

    import java.io.*;

    interface Orator{

    notorious String noiseMade();

    notorious String coercionmatName(String Call);

    }

    arrange Depress instruments Orator{

    notorious String noiseMade() reappear “Moo”;

    notorious String coercionmatName(String Call) reappear “Bos taurus”+” “+ Call;

    }

    arrange BullFrog instruments Orator{

    notorious String noiseMade() reappear “Biggest rain we always had”;

    notorious String coercionmatName(String Call) reappear “Rana catesbeiana”+” “+ Call;

    }

    arrange Douse instruments Orator{

    notorious String noiseMade() reappear “Quack”;

    notorious String coercionmatName(String Call) reappear “Anas platyrhynchos”+” “+ Call;

    }

    arrange Special instruments Orator{

    notorious String passion;

    notorious getEmotion() reappear this.emotion;

    notorious determinedEmotion(String e) this.emotion=e;

    notorious String noiseMade() reappear “Hello, I reach ” +this.getEmotion();

    notorious String coercionmatName(String Call) reappear “Homo sapiens”+” “+ Call;

    }