Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

When I am dealing with this project, I have lots of bugs and all office hours are canceled due to the Spring break. Could anyone

When I am dealing with this project, I have lots of bugs and all office hours are canceled due to the Spring break. Could anyone writes this to help me debug please?

IV. Java Programming (60% of your grade)

Guidelines for the program:

  • All methods listed below must be public and static.
  • If your code is using a loop to modify or create a string, you need to use the StringBuilder class from the API.
  • Keep the loops simple but also efficient. Remember that you want each loop to do only one "task" while also avoiding unnecessary traversals of the data.
  • No additional methods are needed. However, you may write additional private helper methods, but you still need to have efficient and simple algorithms. Do not write helper methods that do a significant number of unnecessary traversals of the data.
  • Important: you must not use either break or continue in your code. These two commands almost always are used to compensate for a poorly designed loop. Likewise, you must not write code that mimics what break does. Instead, re-write your loop so that the loop logic does not need break-type behavior.
  • While it may be tempting to hunt through the API to find classes and methods that can shorten your code, you may not do that. The first reason is that this homework is an exercise in writing loops, not in using the API. The second reason is that in a lot of cases, the API methods may shorten the writing of your code but increase its running time. The only classes and methods you can use are listed below. Note: if a method you want to use from the API is not listed, you should not implement the method yourself so you can use it. Rather, you shoud look for the solution that does not need that method.

    You are allowed to use the following from the Java API:

    • class String
      • length
      • charAt
    • class StringBuilder
      • length
      • charAt
      • append
      • toString
    • class Character
      • any method

Create a class called HW2 that contains the following methods:

  1. onlyEnglishLetters takes a String as input and returns a boolean: The method should return true if every character in the input string is an English letter. It should return false if there exists a character that is not an English letter.

    > HW2.onlyEnglishLetters("abDkfdoFRs") true > HW2.onlyEnglishLetters("lFKe5aaa") false 
     

  2. replaceKth takes two chars, an int and a String as input and returns a String: The int input represents a number k, and the method should replace the kth occurrence of the first char input with the second char input. If the first char does not occur at least k times, then nothing is replaced.

    > HW2.replaceKth('a', 'x', 3, "abcaaa") "abcaxa" > HW2.replaceKth('a', 'x', 6, "aaaaa") "aaaaa" 
     

  3. interleave takes two Strings as input and returns a String: The output should have the first character of the first input string followed by the first character of the second input string, and then the second character of the first string followed by the second character of the second string. If one string has fewer characters than the other, the rest of the characters of the longer string should appear at the end of the output.

    > HW2.interleave("abcde", "ABC") "aAbBcCde" 

  4. blankWords takes a Strings as input and returns a String: Any word of the input (here a word is defined to be consecutive letters) has all but the first and last letters replaced by the '_' character.

    HW2.blankWords("This is a Test.") "T__s is a T__t." 

  5. nthWord takes an int and a String as input and returns a String: The input int represents a number n that is assumed to be positive, and the output string contains every nth word of the input string, starting with the first word, separated by a single space. {\em For this method, a word is defined to be a sequence of non-space characters.} There should be no space at the end of the output string.

    > HW2.nthWord(3, "zero one two three four five six seven") "zero three six" 

  6. truncateAfter takes an int and a String as input and returns a String: The input string may contain hyphens and spaces that mark appropriate places to truncate the string. The output string will be a truncated version of the input string, and the input int value is the desired length of the output string. The output string should truncate the input string at the first legal spot such that the output string will have at least the desired length. If the truncation happens at a space, the space is not included in the output, but if the truncation happens at a hyphen, the hyphen is included. No other hyphens are included in the output, but the other spaces are. (If the input string does not have enough characters to meet the desired minimum, then the output should be the entire input string without the hyphens.)

    > HW2.truncateAfter(5, "La-te-ly the-re.") "Late-" > HW2.truncateAfter(6, "La-te-ly the-re.") "Lately" > HW2.truncateAfter(7, "La-te-ly the-re.") "Lately the-" 

  7. Extra Credit: truncateBefore: the same as the truncateAfter method, but now the input int value is the maximum, and the string should be truncated at the latest possible point such that the resulting string has no more than the desired length.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And SQL

Authors: Lucy Scott

3rd Edition

1087899699, 978-1087899695

More Books

Students also viewed these Databases questions

Question

If both ESS TRUs fail, what is powering the DC essential bus?

Answered: 1 week ago

Question

Evaluate the importance of the employee handbook.

Answered: 1 week ago

Question

Discuss the steps in the progressive discipline approach.

Answered: 1 week ago