Question

In Java, you'll find that it contains several methods that have not yet been finished. For now, the methods contain...

In Java, you'll find that it contains several methods that have not yet been finished. For now, the methods contain only a placeholder return value so that the code will compile. Fill in your own implementation.

public class SomePracticeStringMethods {
/*
* returns true if c is a letter in the word "temple" or false otherwise
*/
public static boolean inTU(char c) {
/* placeholder just so that the function
* compiles. fill in your implementation here.
*
* there is a similar placeholder
* for each of the remaining functions */
return true;
}
  
/*
* returns the index of the first occurrence of in s
* of a letter in the word "temple" or
* -1 if s contains no letters in the word "temple"
*/
public static int indexOfFirstTULetter(String s) {
return -1;
}

/*
* returns the index of the first occurrence of a letter in "temple" in s starting
* from index startPosition or -1 if there are none at index
* startPosition or later. Notice that this method has the same name as the
* previous one, but that it takes a different number of arguments. This is
* perfectly legal in Java. It's called "method overloading"
*/
public static int indexOfFirstTULetter(String s, int startPosition) {
return -1;
}

/*
* returns the index of the last occurrence of a letter in the word "temple"
* in s or -1 if s
* contains none
*/
public static int indexOfLastTULetter(String s) {
return -1;
}

/* returns true if every letter in s is a letter
* in the word "temple" or false otherwise */
public static boolean allTempleLetters(String s) {
return true;
}
  
/* returns true if no letter in s is a letter
* in the word "temple" or false otherwise */
public static boolean noTempleLetters(String s) {
return true;
}

/*
* returns a new String which is the same as s, but with all of the letters
* in the word "temple" removed.
*/
public static String withoutTULetters(String s) {
return "ADD ME";
}

/*
* returns s in reverse. For example, if s is "Temple", the method returns the
* String "elpmeT"
*/
public static String reversed(String s) {
return "ADD ME";
}

/*
* returns the number of times that n occurs in h. For example, if h is
* "Mississippi" and n is "ss" the method returns 2.
*/
public static int numOccurrences(String h, String n) {
return 0;
}

/*
* returns true if s is the same backwards and forwards and false otherwise
*/
public static boolean sameInReverse(String s) {
return true;
}


/*
* Returns a new String that looks like base appended with suffix. If base
* already ends with suffix, it returns base.
*
* For example, if base is "lightning" and suffix is "bug", returns
* "lightningbug".
*
* If base is "lightningbug" and suffix is "bug", it also returns
* "lightningbug".
*/
public static String appendIfMissing(String base, String suffix) {
return "ADD ME";
}
}

Please use the test code to test it!!!

package stringPractice1068;

import static org.junit.Assert.*;

import org.junit.Test;

public class SomePracticeStringMethodsTest {
   @Test
   public void testInTU() {
       assertTrue(SomePracticeStringMethods.inTU('T'));
       assertTrue(SomePracticeStringMethods.inTU('t'));
       assertTrue(SomePracticeStringMethods.inTU('e'));
       assertTrue(SomePracticeStringMethods.inTU('m'));
       assertTrue(SomePracticeStringMethods.inTU('P'));
       assertFalse(SomePracticeStringMethods.inTU('x'));
       assertFalse(SomePracticeStringMethods.inTU('.'));
       assertFalse(SomePracticeStringMethods.inTU('0'));
   }

   @Test
   public void testIndexOfFirstTULetterString() {
      
       assertEquals(SomePracticeStringMethods.indexOfFirstTULetter("atom"), 1);
       assertEquals(SomePracticeStringMethods.indexOfFirstTULetter("aTom"), 1);
       assertEquals(SomePracticeStringMethods.indexOfFirstTULetter("Opus"), 1);
       assertEquals(SomePracticeStringMethods.indexOfFirstTULetter("elephant"), 0);
       assertEquals(SomePracticeStringMethods.indexOfFirstTULetter(""), -1);
   }

   @Test
   public void testIndexOfFirstTULetterStringInt() {
       assertEquals(SomePracticeStringMethods.indexOfFirstTULetter("atom", 1), 1);
       assertEquals(SomePracticeStringMethods.indexOfFirstTULetter("atom", 2), 3);
       assertEquals(SomePracticeStringMethods.indexOfFirstTULetter("aTom", 1), 1);
       assertEquals(SomePracticeStringMethods.indexOfFirstTULetter("Opus", 2), -1);
       assertEquals(SomePracticeStringMethods.indexOfFirstTULetter("elephant", 2), 2);
       assertEquals(SomePracticeStringMethods.indexOfFirstTULetter("", 1), -1);
   }

   @Test
   public void testIndexOfLastTULetter() {
       assertEquals(SomePracticeStringMethods.indexOfLastTULetter("atom"), 3);
       assertEquals(SomePracticeStringMethods.indexOfLastTULetter("aTom"), 3);
       assertEquals(SomePracticeStringMethods.indexOfLastTULetter("Opus"), 1);
       assertEquals(SomePracticeStringMethods.indexOfLastTULetter("elephant"), 7);
       assertEquals(SomePracticeStringMethods.indexOfLastTULetter(""), -1);
   }

   @Test
   public void testNoTempleLetters() {
       assertTrue(SomePracticeStringMethods.noTempleLetters("aardvark"));
       assertFalse(SomePracticeStringMethods.noTempleLetters("LioN"));
       assertTrue(SomePracticeStringMethods.noTempleLetters("cougar"));
       assertFalse(SomePracticeStringMethods.noTempleLetters("Winnebago"));
       assertTrue(SomePracticeStringMethods.noTempleLetters("workbook"));
       assertTrue(SomePracticeStringMethods.noTempleLetters(""));
   }

   @Test
   public void testAllTempleLetters() {
       assertTrue(SomePracticeStringMethods.allTempleLetters("meet"));
       assertTrue(SomePracticeStringMethods.allTempleLetters("Elm"));
       assertTrue(SomePracticeStringMethods.allTempleLetters("eel"));
       assertTrue(SomePracticeStringMethods.allTempleLetters("EMMETT"));
       assertTrue(SomePracticeStringMethods.allTempleLetters("melee"));
       assertFalse(SomePracticeStringMethods.allTempleLetters("melees"));
   }

   @Test
   public void testWithoutTULetters() {
       assertEquals(SomePracticeStringMethods.withoutTULetters("Tom"), "o");
       assertEquals(SomePracticeStringMethods.withoutTULetters("put"), "u");
       assertEquals(SomePracticeStringMethods.withoutTULetters("all"), "a");
       assertEquals(SomePracticeStringMethods.withoutTULetters("of"), "of");
       assertEquals(SomePracticeStringMethods.withoutTULetters("my"), "y");
       assertEquals(SomePracticeStringMethods.withoutTULetters("records"), "rcords");
       assertEquals(SomePracticeStringMethods.withoutTULetters("into"), "ino");
       assertEquals(SomePracticeStringMethods.withoutTULetters("this"), "his");
       assertEquals(SomePracticeStringMethods.withoutTULetters("rectangle"), "rcang");
   }

   @Test
   public void testReversed() {
       assertEquals("cba", SomePracticeStringMethods.reversed("abc"));
       assertEquals("a", SomePracticeStringMethods.reversed("a"));
       assertEquals("dcba", SomePracticeStringMethods.reversed("abcd"));
   }

   @Test
   public void testSameInReverse() {
       assertTrue(SomePracticeStringMethods.sameInReverse("peep"));
       assertTrue(SomePracticeStringMethods.sameInReverse("madam"));
       assertTrue(SomePracticeStringMethods.sameInReverse("rotator"));
       assertFalse(SomePracticeStringMethods.sameInReverse("blue"));
       assertFalse(SomePracticeStringMethods.sameInReverse("aspirin"));
       assertFalse(SomePracticeStringMethods.sameInReverse("ab"));
       assertTrue(SomePracticeStringMethods.sameInReverse("a"));
   }

   @Test
   public void testNumOccurrences() {
       assertEquals(2, SomePracticeStringMethods.numOccurrences("banana", "na"));
       assertEquals(2, SomePracticeStringMethods.numOccurrences("mississippi", "ss"));
       assertEquals(0, SomePracticeStringMethods.numOccurrences("undertow", "sushi"));
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// SomePracticeStringMethods.java

public class SomePracticeStringMethods {

      /*

      * returns true if c is a letter in the word "temple" or false otherwise

      */

      public static boolean inTU(char c) {

            // converting c to lower case

            c = Character.toLowerCase(c);

            // defining a word 'temple'

            String word = "temple";

            // looping through each character in 'temple' and checking if c is

            // included.

            for (int i = 0; i < word.length(); i++) {

                  if (word.charAt(i) == c) {

                        // found

                        return true;

                  }

            }

            // not found

            return false;

      }

      /*

      * returns the index of the first occurrence of in s of a letter in the word

      * "temple" or -1 if s contains no letters in the word "temple"

      */

      public static int indexOfFirstTULetter(String s) {

            // using inTU method, finding the first letter in s that returns true

            for (int i = 0; i < s.length(); i++) {

                  if (inTU(s.charAt(i))) {

                        // found, returning index

                        return i;

                  }

            }

            return -1; // not found

      }

      /*

      * returns the index of the first occurrence of a letter in "temple" in s

      * starting from index startPosition or -1 if there are none at index

      * startPosition or later. Notice that this method has the same name as the

      * previous one, but that it takes a different number of arguments. This is

      * perfectly legal in Java. It's called "method overloading"

      */

      public static int indexOfFirstTULetter(String s, int startPosition) {

            // same as previous method, but the value of i starts from startPosition

            for (int i = startPosition; i < s.length(); i++) {

                  if (inTU(s.charAt(i))) {

                        return i;

                  }

            }

            return -1;

      }

      /*

      * returns the index of the last occurrence of a letter in the word "temple"

      * in s or -1 if s contains none

      */

      public static int indexOfLastTULetter(String s) {

            // looping in reverse

            for (int i = s.length() - 1; i >= 0; i--) {

                  if (inTU(s.charAt(i))) {

                        return i;

                  }

            }

            return -1;

      }

      /*

      * returns true if every letter in s is a letter in the word "temple" or

      * false otherwise

      */

      public static boolean allTempleLetters(String s) {

            // looping and checking if any letter in s that returns false when inTU

            // method is called.

            for (int i = 0; i < s.length(); i++) {

                  if (!inTU(s.charAt(i))) {

                        return false; // found one

                  }

            }

            return true; // every letter in s is a letter in word 'temple'

      }

      /*

      * returns true if no letter in s is a letter in the word "temple" or false

      * otherwise

      */

      public static boolean noTempleLetters(String s) {

            // checking if any letter in s returns true using inTU method

            for (int i = 0; i < s.length(); i++) {

                  if (inTU(s.charAt(i))) {

                        // found

                        return false;

                  }

            }

            // no letter in s is a letter in the word "temple"

            return true;

      }

      /*

      * returns a new String which is the same as s, but with all of the letters

      * in the word "temple" removed.

      */

      public static String withoutTULetters(String s) {

            // creating a string

            String result = "";

            // appending all characters to result if the character returns false

            // when inTU is called.

            for (int i = 0; i < s.length(); i++) {

                  if (!inTU(s.charAt(i))) {

                        result += s.charAt(i);

                  }

            }

            return result;

      }

      /*

      * returns s in reverse. For example, if s is "Temple", the method returns

      * the String "elpmeT"

      */

      public static String reversed(String s) {

            String result = "";

            // looping

            for (int i = 0; i < s.length(); i++) {

                  // appending char before result

                  result = s.charAt(i) + result;

            }

            return result;

      }

      /*

      * returns the number of times that n occurs in h. For example, if h is

      * "Mississippi" and n is "ss" the method returns 2.

      */

      public static int numOccurrences(String h, String n) {

            int count = 0;

            int index = 0;

            // finding the first index of n in h

            index = h.indexOf(n);

            // if index is not -1, looping

            while (index != -1) {

                  // incrementing count

                  count++;

                  // finding the next index of n in h, starting from position

                  // index+n.length()

                  index = h.indexOf(n, index + n.length());

            }

            return count;

      }

      /*

      * returns true if s is the same backwards and forwards and false otherwise

      */

      public static boolean sameInReverse(String s) {

            //using reversed method, checking if string is same when reversed

            return s.equals(reversed(s));

      }

      /*

      * Returns a new String that looks like base appended with suffix. If base

      * already ends with suffix, it returns base.

      *

      * For example, if base is "lightning" and suffix is "bug", returns

      * "lightningbug".

      *

      * If base is "lightningbug" and suffix is "bug", it also returns

      * "lightningbug".

      */

      public static String appendIfMissing(String base, String suffix) {

            //if base not ends with suffix, appending suffix

            if (!base.endsWith(suffix)) {

                  base += suffix;

            }

            //returning modified base

            return base;

      }

}

/*OUTPUT of given JUnit test*/


Runs: 10/10 0 Errors: 0 stm品品品品品品品品品 Failures: 0 stringmethods.SomePracticeStringMethodsTest [Ru testinTU (0.000 s) testindex

Add a comment
Know the answer?
Add Answer to:
In Java, you'll find that it contains several methods that have not yet been finished. For now, the methods contain...
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
  •    /**    * Returns true if and only if each string in the supplied list...

       /**    * Returns true if and only if each string in the supplied list of strings    * starts with an uppercase letter. If the list is empty, returns false.    *    * @param l a non-null list of strings    * @return true iff each string starts with an uppercase letter    */    public static boolean allCapitalizedWords(List<String> l) {       } Here are the tests: @Test    public void testAllCapitalizedWordsEmptyList() {        assertFalse(ListExercises.allCapitalizedWords(Arrays.asList()));...

  • Java code tasks: Please help with a single class (class Coord) in this program. This project...

    Java code tasks: Please help with a single class (class Coord) in this program. This project is called Panic! We will describe a single-level floorplan of a building, filled with different kinds of people and different kinds of threats. We will simulate how the people seek to evacuate the building to safety, and how the threats spread throughout the building. Various events will be announced, such as person movements, threats spawning, and people reaching safety or succumbing to the threats....

  • Need help with a number guessing game in java 1) You should store prior guessses in...

    Need help with a number guessing game in java 1) You should store prior guessses in a linked list, and the nodes in the list must follow the order of prior guesses. For example, if the prior guesses are 1000, 2111, 3222 in that order, the nodes must follow the same order 2) You should store the candidate numbers also in a linked list, and the nodes must follow the order of numbers (i.e. from the smallest to the largest)....

  • I am not passing some of the code when i run the testing . PriorityQueue public...

    I am not passing some of the code when i run the testing . PriorityQueue public class PriorityQueue<T extends Comparable<T>> implements IPriorityQueue<T> {    private Vector<T> theVector = new Vector<>(0, 1);    private int size = 0;    @Override    public void insert(T element) {        theVector.pushBack(element);        size++;        bubbleUp(); }    @Override    public T peekTop() throws java.util.NoSuchElementException {        if (isEmpty()) {            throw new java.util.NoSuchElementException();        }       ...

  • Need some assistance coding this assembler exercise. I have included the test file. Thank you very...

    Need some assistance coding this assembler exercise. I have included the test file. Thank you very much. package sequencer; import java.util.List; public class Assembler {    /**    * Creates a new Assembler containing a list of fragments.    *    * The list is copied into this assembler so that the original list will not    * be modified by the actions of this assembler.    *    * @param fragments    */    public Assembler(List<Fragment> fragments) {   ...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • 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...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • 26-ary tree for spell checker in JAVA You are asked to write some functionalities for a...

    26-ary tree for spell checker in JAVA You are asked to write some functionalities for a spelling checker inside Tree.java that has at least the following methods: /*Adds a word to a spelling checker’s collection of correctly spelled words*/ void add(String word) /*Returns true if the given word is spelled correctly*/ boolean check(String word) /*Returns back all words in the tree in alphabetical order*/ public String getDictionaryInAlphabeticalOrder() Store the collection of correctly spelled words in a 26-ary tree. The number...

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