Question

Given this JUnit test, write a SentenceCounter class that makes the tests pass: import static org.junit.Assert.*;...

Given this JUnit test, write a SentenceCounter class that makes the tests pass:

import static org.junit.Assert.*;

import org.junit.Before;

import org.junit.Test;

/**

* Tests that will make us practice writing loops

*/

public class TestSentenceCounter

{

private static final String SENTENCE1 =

"This is my sentence.";

private static final String SENTENCE2 =

"These words make another sentence that is longer";

private SentenceCounter sc1;

private SentenceCounter sc2;

  

/**

* Create two instances we can play with

*/

@Before

public void setup()

{

sc1 = new SentenceCounter(SENTENCE1);

sc2 = new SentenceCounter(SENTENCE2);

}

  

/**

* Make sure the instance variable is correct

*/

@Test

public void testConstructor()

{

assertEquals(SENTENCE1, sc1.getSentence());

assertEquals(SENTENCE2, sc2.getSentence());

}

  

/**

* Returns an integer containing the number of blanks

* in the String.

*/

@Test

public void testCounterBlanks()

{

assertEquals(3, sc1.countBlanks());

assertEquals(7, sc2.countBlanks());

}

  

12F69FDA-3060-48CB-8A87-025E20A8464C.jpg
0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class SentenceCounter {

    private String sentence;

    public SentenceCounter(String sentence) {
        this.sentence = sentence;
    }

    public String getSentence() {
        return sentence;
    }
}
Add a comment
Know the answer?
Add Answer to:
Given this JUnit test, write a SentenceCounter class that makes the tests pass: import static org.junit.Assert.*;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How do I test this with JUnit? I watched a video on YouTube about how to...

    How do I test this with JUnit? I watched a video on YouTube about how to create and run a Simple JUnit test in Eclipse IDE but I still don't understand. Here's what I need to test: public class ReverseDomainName {    public static void main(String[] args)    {        String domain = "cs.princeton.edu";        System.out.println("domain is " + domain);        System.out.println("reverse of domain is " + rev_domain(domain));    }       public static String rev_domain(String domain)...

  • Please help complete the items marked TODO in the code and get the tests to pass:...

    Please help complete the items marked TODO in the code and get the tests to pass: import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestIterator { private List<Integer> list; // See the Java List Interface documentation to understand what all the List methods do ... @Before public void setUp() throws Exception { list = new ArrayList<Integer>(); // TODO also try with a...

  • Write one JUnit test in Java for the following: Essay class: import java.util.Scanner; public class Essay implements IAn...

    Write one JUnit test in Java for the following: Essay class: import java.util.Scanner; public class Essay implements IAnswer{ private String question; public Essay(String q){ this.question = q; } //This function returns question text public String getQuestionText() {    return question; } //This function takes answer from user public void answer(String userAnswer) {    // Take care of answer } @Override public String getAnswer() { System.out.println(question); Scanner scan = new Scanner(System.in); System.out.print("Answer: "); String ans =scan.nextLine(); scan.close(); if(ans.length() <=140){ return ans; }else{ return...

  • The following code computes the radius of a circle. Using static methods from the Math class...

    The following code computes the radius of a circle. Using static methods from the Math class complete the method that computes the radius of a circle using the formula  r2=(x-h)2 +(y-k)2 , given the (x,y) coordinates of one point on its circumference and the (h,k) coordinates of its center. public class Circle {    public static void main(String[] C)    {        double x1 =14.25;        double y1 =13.68;        double xCenter = 25.678;        double yCenter...

  • Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the...

    Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the ADT -----ArrayQueue.java------- public class ArrayQueue {    private static final int INITIAL_CAPACITY = 2; // to permit easier testing    private Object[] contents;    private int front, rear;       /**    * Create an empty queue with an initial capacity.    */    public ArrayQueue() {        contents = new Object[INITIAL_CAPACITY];    }       /**    * Add an element to...

  • Please help complete the items marked TODO in the code and get the tests to pass:...

    Please help complete the items marked TODO in the code and get the tests to pass: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestPerformance { // TODO run test and record running times for SIZE = 10, 100, 1000, 10000, ... // (choose in conjunction with REPS below up to an upper limit where the clock // running time is in the tens of seconds) // TODO (optional) refactor to DRY // TODO...

  • Please help complete the items marked TODO in the code and get the tests to pass:...

    Please help complete the items marked TODO in the code and get the tests to pass: Someone already answered it, but it was incorrect! import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestPerformance { // TODO run test and record running times for SIZE = 10, 100, 1000, 10000, ... // (choose in conjunction with REPS below up to an upper limit where the clock // running time is in the tens of seconds)...

  • This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = '...

    This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = ' '; private static final char UPPER_BOUND = '_'; private static final int RANGE = UPPER_BOUND - LOWER_BOUND + 1; /** * This method determines if a string is within the allowable bounds of ASCII codes * according to the LOWER_BOUND and UPPER_BOUND characters * @param plainText a string to be encrypted, if it is within the allowable bounds * @return true if all characters...

  • Need help in Java network,    I'm trying to pass Junit 4 tests.       my...

    Need help in Java network,    I'm trying to pass Junit 4 tests.       my Code: public Character findIt( String url){        try {               new URL("myurl").toURI();            System.out.println("pass");            } return null;    } Test case:    @Test    public void testForValidOrNot(){               ValidOrNot validOrNot = new ValidOrNot();                       assertEquals( new Character('pass'), validOrNot.findIt( "my url" ) );    Trying to see why...

  • This is my code for a TicTacToe game. However, I don't know how to write JUnit...

    This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe {    private char currentPlayer;    private char[][] board;    private char winner;       /**    * Default constructor    * Initializes the board to be 3 by 3 and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT