Skip to content

Pull request for updates/review#20

Open
silkycode wants to merge 1 commit intomainfrom
Nicholas_Cudd_CodeReview
Open

Pull request for updates/review#20
silkycode wants to merge 1 commit intomainfrom
Nicholas_Cudd_CodeReview

Conversation

@silkycode
Copy link

No description provided.

@silkycode silkycode changed the title Pull request for updates Pull request for updates/review Jun 15, 2023
@sarafarag sarafarag requested review from Hester1937 and MangoPicante and removed request for MangoPicante June 16, 2023 00:59

@Override
public void setString(String string) {
myString = string;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.myString as it is a property of the class.

public int count() {
return 0;
if (myString == null || myString.length() == 0) {
return 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Input Validation: The code correctly checks if the input string (myString) is null or empty. Returning 0 in these cases seems appropriate, assuming that an empty string should not be counted as a word.

wordCount++;
}
}
return wordCount;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code iterates over the tokens and checks if each token consists only of alphabetical characters using a regular expression pattern. This pattern matches strings that contain only one or more uppercase or lowercase letters. This approach assumes that words are defined as sequences of alphabetic characters without any punctuation or numbers. If this definition aligns with your requirements, then the code is suitable.

} else if (n > myString.length()) {
throw new IndexOutOfBoundsException("n cannot exceed length of parameter String");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throws appropriate exceptions for invalid cases.

throw new IndexOutOfBoundsException("n cannot exceed length of parameter String");
}

ArrayList<Character> characters = new ArrayList<Character>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of new ArrayList(), you can use new ArrayList<>() to let the compiler infer the type.

String[] words = myString.split(" | ");
String[] subWords = new String[numberOfWords];
for (int i = 0, j = startWord - 1; i < numberOfWords; i++, j++) {
subWords[i] = words[j];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a fixed-size array subWords, consider using a List implementation, such as an ArrayList, to allow for dynamic resizing as needed. This eliminates the need to calculate the number of words beforehand.

}
char[] charString = myString.toCharArray();
char[] referenceString = myString.toCharArray();
for (int i = 0; i < charString.length; i++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code creates two separate character arrays, charString and referenceString, from myString.toCharArray(). However, you can directly modify charString in place without the need for the referenceString array.

}

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the test class, add more test cases to cover different scenarios and edge cases.
such as:
assertEquals("bcdea", stringManipulator.restoreString(new int[]{1, 2, 3, 4, 0}));
assertEquals("", stringManipulator.restoreString(new int[]{}));
assertEquals("Hello, World!", stringManipulator.restoreString(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}));
Additional test cases help ensure the code handles different inputs correctly and increase the overall test coverage.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall Recap: These improvements result in a more robust and efficient code implementation, with enhanced error handling and increased test coverage. It's important to thoroughly test the code and consider edge cases to ensure its correctness and reliability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants