Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have lots of bugs for this project. Could anyone writes this in order to help me check my bugs and renew my understanding of

I have lots of bugs for this project. Could anyone writes this in order to help me check my bugs and renew my understanding of these knowledge?????

I. Overview

This second programming assignment is to give you practice writing loops and manipulating strings. Arrays are not needed, and and you should not use arrays in your assignment.

II. Code Readability (20% of your project grade)

As with the last project, you are required to have code that is easy for another person to read and understand. To accomplish that, the class will have certain rules about how your code should look.

To receive the full readability marks, your code must follow the following guideline:

  • You should place a comment at the top of the file that contains your name and a short description of the purpose of the class.
  • You should place a short comment before (directly above) each method describing the method. The comment should be only describe what the method does, not how it does it. Do not simply copy the descriptions below for your comments.
  • You should place a short comment directly above each field (should you really have any?) indicating the purpose of the variable.
  • You should place a short comment above or to the right of each local variable explaining the purpose of the variable. Variables that are only used as loop indexes do not need to have comments.
  • You should place a short comment above each loop explaining how the loop works. Ideally, you should list the goal of the loop, any required precondition for the loop, and if you can, a good iteration subgoal for the loop.
  • Any other complicated code such as code the has lots of if statements or variables should contain short comments to help the reader. The comments can either be above the code fragment or to the right, aligned in a column.
  • Remember to use good style: everything should be indented nicely, variables should have good names, there should be a blank line between each method.

III. Program Testing Document (20% of your project grade)

As with the previous project, you are required to submit a document demonstrating that you thoroughly tested your program. In this case, it means documenting tests for each of the methods listed below. If you are unable to complete a method above, you should still describe tests that would test the method had it been completed.

Hints for testing loops. Your tests need to, at the minimum cover the following cases:

  1. Test 0, test 1, test many: This means you have to test cases where the parameters, if they are integers, are 0, 1 or some value other than 1. If the parameters are strings, you have to test strings of length 0, 1, and more than 1. If the strings must contain certain data, you need to test cases where they contain 0, 1, and more than 1 of the desired data.
  2. Test first, last, and middle: In cases where you have to search in or modify a string, you need to test cases where the item to be found or modified is the first character of the string, the last character of the string, or somewhere in the middle of the string.

What must go in the report: For each method below, your report should describe, in English, what "test 0, 1, many" and "test first, middle, last" mean for each of the methods. Then, you should list the specific tests that you will do, what the expected output is, and then (if you completed the method) a cut-and-paste from DrJava showing the actual test.

JUnit: you will be learning about JUnit soon in a lab. JUnit will be required for future homeworks, and you are welcome to use it with this homework. If you choose to write JUnit tests for your code, you do not need to include the actual tests in your report. Your JUnit file should include comments with each test that link to the report and indicate what you are testing. For example, if your report indicates that the method requires a test with a string of length 0, your JUnit class should have such a test and a comment on the test noting that it is the test of a length 0 string that your report described. Try to organize the JUnit class and report to make it easy for a reader to jump back and forth between the report and the tests.

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

More Books

Students also viewed these Databases questions