You are allowed to use the following from the Java API:
Create a class called HW2 that contains the following methods:
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"
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-"
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.
[11:01 AM, 3/7/2019] Narasimha: ]Here are the first 4 methods
======================================================================================
public class HW2 {
public static boolean onlyEnglishLetters(String text) {
for (int i = 0; i < text.length(); i++) {
char letter = text.charAt(i);
if (('a' <= letter && letter <= 'z') || ('A' <=
letter && letter <= 'Z')) {
continue;
} else {
return false;
}
}
return true;
}
public static String replaceKth(char find, char replace, int pos, String text) {
int frequency = 0;
int index = -1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == find) {
frequency += 1;
if (frequency == pos) {
index = i;
break;
}
}
}
if (frequency == pos) {
return text.substring(0, index) + String.valueOf(replace) +
text.substring(index + 1);
} else {
return text;
}
}
public static String interleave(String one, String two) {
StringBuilder build = new StringBuilder();
int lengthOne = one.length();
int lengthTwo = two.length();
int max = lengthOne > lengthTwo ? lengthOne : lengthTwo;
for (int i = 0; i < max; i++) {
if (i < one.length()) {
build.append(one.charAt(i));
}
if (i < two.length()) {
build.append(two.charAt(i));
}
}
return build.toString();
}
public static String blankWords(String text) {
String[] words = text.split("\\s+");
StringBuilder builder = new StringBuilder();
for (String word : words) {
if (word.length() <= 2) {
builder.append(word).append(" ");
} else {
for (int i = 0; i < word.length(); i++) {
if (i == 0 || i == word.length() - 1) {
builder.append(word.charAt(i));
} else {
builder.append("-");
}
}
builder.append(" ");
}
}
return builder.substring(0, builder.length() - 1).toString();
}
public static void main(String[] args) {
System.out.println(replaceKth('a', 'x', 6, "aaaaa"));
System.out.println(interleave("abcde", "ABC"));
System.out.println(blankWords("This is a Test ."));
}
}
In Java All methods listed below must be public and static. If your code is using...
In Java Only can use class String length charAt class StringBuilder length charAt append toString class Character any method Create the following methods 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...
Java code about writing two methods: public static String randomStringone(int length) public static String randomStringtwo(int length) This method should return a String of random lowercase letters with the given length by using for loops. To generate a random lowercase letter, use a local Random variable and the method nextInt() to generate a number between 97 and 122, then cast the result to a char. The method nextInt() can be found here: https://www.geeksforgeeks.org/java-util-random-nextint-java/ In randomStringone(), you should use String concatenation...
containsSubSequence takes two Strings as input and returns a boolean: Returns true if the first input string contains all the characters of the second input string, in order, but not necessarily consecutively. > HW2.containsSubSequence("abracadabra", "abcd") true > HW2.containsSubSequence("abracadabra", "abdc") false you must not use either break or continue in your code. You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method I write something...
Programming project in Java: You are allowed to use the following methods 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. isAlphabeticalOrder takes a String as input and returns a boolean: The method returns true if all the letters of the input string are in alphabetical order, regardless of case. The method returns false otherwise. Do not use arrays to...
You will write three static methods to manipulate an input
String in different ways using various String methods. You need to
provide the code for each of the three static methods in class
StringPlay (requirements for each listed below). You will also
change the control statement in the test harness to allow for
variations in the sentinel value. You need to modify the loop
control condition in Lab09.java so that user inputs of ‘finish’,
“FINISH”, “FiniSH”, “fINISH”, etc. will end...
Do anyone knows how to write this in java? You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method moveXDownLeft takes a char and a two dimensional array of char as input and returns nothing: The method should find the first occurrence of the char in the array (searching from "top" to "bottom" and "left" to "right"), and it should slide that character...
*MYST BE I NJAVA* *PLEASE INCORPORATE ALL OF STRING METHODS AND SCANNER METHOD LISTED IN THE DIRECTIONS BELOW* Write a Java class that takes a full name (first and last) as inputted by the user, and outputs the initials. Call the class Initials. The first and last names should be entered on the same input line i.e. there should be only one input to your program. For example, if the name is Jane Doe, the initials outputted will be J...
USING JAVA Consider the following methods: StringBuilder has a method append(). If we run: StringBuilder s = new StringBuilder(); s.append("abc"); The text in the StringBuilder is now "abc" Character has static methods toUpperCase() and to LowerCase(), which convert characters to upper or lower case. If we run Character x = Character.toUpperCase('c');, x is 'C'. Character also has a static isAlphabetic() method, which returns true if a character is an alphabetic character, otherwise returns false. You will also need String's charAt()...
Java Question, I need a program that asks a user to input a string && then asks the user to type in an index value(integer). You will use the charAt( ) method from the string class to find and output the character referenced by that index. Allow the user to repeat these actions by placing this in a loop until the user gives you an empty string. Now realize that If we call the charAt method with a bad value...
public static int countCharacter(String str, char c) { // This recursive method takes a String and a char as parameters and // returns the number of times the char appears in the String. You may // use the function charAt(int i) described below to test if a single // character of the input String matches the input char. // For example, countCharacter(“bobbie”, ‘b’) would return back 3, while // countCharacter(“xyzzy”, ‘y’) would return back 2. // Must be a RECURSIVE...