package string.exercises; public class StringExercises { /** * Searches for \"Ma
ID: 3888049 • Letter: P
Question
package string.exercises;
public class StringExercises {
/**
* Searches for "Marc" in a string.
*
* @param string a non-null string
* @return the index of the first occurrence of "Marc" in string, or -1 if not found
*/
public static int findMarc(String string) {
return -2;
}
/**
* Searches for a substring within a string.
* @param string a non-null string
* @param substring a non-null string
* @return the index of the first occurrence of the substring within the string, or -1 if not found
*/
public static int findSubstring(String string, String substring) {
return -2;
}
/**
* Returns true if and only if the string contains the substring.
* @param string a non-null string
* @param substring a non-null string
* @return true if and only if the string contains the substring
*/
public static boolean contains(String string, String substring) {
return false;
}
/**
* Splits a string into words, using whitespace to delimit the words.
*
* See the assignment writeup for the magic argument to split().
*
* @param string a non-null string
* @return an array representing the words in the string.
*/
public static String[] splitIntoWords(String string) {
return null;
}
/**
* Returns the substring representing the first four characters of the string.
* @param string a non-null string of length >= 4
* @return the substring representing the first four characters of the string
*/
public static String firstFour(String string) {
return null;
}
/**
* Returns the substring representing the first n characters of the string.
* @param string a non-null string of length >= n
* @param n an integer >= 0
* @return the substring representing the first n characters of the string
*/
public static String firstN(String string, int n) {
return null;
}
/**
* Returns the substring representing the last four characters of the string.
* @param string a non-null string of length >= 4
* @return the substring representing the last four characters of the string
*/
public static String lastFour(String string) {
return null;
}
/**
* Returns the substring representing the last n characters of the string.
* @param string a non-null string of length >= n
* @param n an integer >= 0
* @return the substring representing the last n characters of the string
*/
public static String lastN(String string, int n) {
return null;
}
}
you shouldn’t need to write any loops to complete this file. Finally, note you can use the String.split method
===========================================================================================
package list.exercises;
import java.util.List;
public class ListExercises {
/**
* Counts the number of characters in total across all strings in the supplied list;
* in other words, the sum of the lengths of the all the strings.
* @param l a non-null list of strings
* @return the number of characters
*/
public static int countCharacters(List<String> l) {
return 0;
}
/**
* Splits a string into words and returns a list of the words.
* If the string is empty, split returns a list containing an empty string.
*
* @param s a non-null string of zero or more words
* @return a list of words
*/
public static List<String> split(String s) {
return null;
}
/**
* Returns a copy of the list of strings where each string has been
* uppercased (as by String.toUpperCase).
*
* The original list is unchanged.
*
* @param l a non-null list of strings
* @return a list of uppercased strings
*/
public static List<String> uppercased(List<String> l) {
return null;
}
/**
* 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) {
return false;
}
/**
* Returns a list of strings selected from a supplied list, which contain the character c.
*
* The returned list is in the same order as the original list, but it omits all strings
* that do not contain c.
*
* The original list is unmodified.
*
* @param l a non-null list of strings
* @param c the character to filter on
* @return a list of strings containing the character c, selected from l
*/
public static List<String> filterContaining(List<String> l, char c) {
return null;
}
/**
* Inserts a string into a sorted list of strings, maintaining the sorted property of the list.
*
* @param s the string to insert
* @param l a non-null, sorted list of strings
*/
public static void insertInOrder(String s, List<String> l) {
}
}
you’ll be using get, set, add, size and the like instead of the array index operators []
Translate written descriptions of behavior into code.
thank you!
Explanation / Answer
Using String
//Class StringExercise definition
public class StringExercise
{
/**
* Searches for "Marc" in a string.
*
* @param string a non-null string
* @return the index of the first occurrence of "Marc" in string, or -1 if not found
*/
public static int findMarc(String string)
{
//Checks for the match for the constant "Marc" if found returns index position otherwise returns -1
int index = string.indexOf("Marc");
//Returns index position
return index;
}//End of method
/**
* Searches for a substring within a string.
* @param string a non-null string
* @param substring a non-null string
* @return the index of the first occurrence of the substring within the string, or -1 if not found
*/
public static int findSubstring(String string, String substring)
{
//Checks for the match for the substring if found returns index position otherwise returns -1
int index = string.indexOf(substring);
//Returns index position
return index;
}//End of method
/**
* Returns true if and only if the string contains the substring.
* @param string a non-null string
* @param substring a non-null string
* @return true if and only if the string contains the substring
*/
public static boolean contains(String string, String substring)
{
//Checks for the match for the constant "Marc" if found returns index position otherwise returns -1
int index = string.indexOf(substring);
//If index position is not -1 substring found so, return true
//Otherwise return false
if(index != -1)
return true;
else
return false;
}//End of method
/**
* Splits a string into words, using whitespace to delimit the words.
*
* See the assignment write up for the magic argument to split().
*
* @param string a non-null string
* @return an array representing the words in the string.
*/
public static String[] splitIntoWords(String string)
{
//Split the string with space and store it in splitString array
String splitString[] = string.split(" ");
//Returns the string array
return splitString;
}//End of method
/**
* Returns the substring representing the first four characters of the string.
* @param string a non-null string of length >= 4
* @return the substring representing the first four characters of the string
*/
public static String firstFour(String string)
{
//Extracts first four characters from the given string
String result = string.substring(0, 4);
//Returns the extracted string
return result;
}//End of method
/**
* Returns the substring representing the first n characters of the string.
* @param string a non-null string of length >= n
* @param n an integer >= 0
* @return the substring representing the first n characters of the string
*/
public static String firstN(String string, int n)
{
//Extracts first n characters from the given string
String result = string.substring(0, n);
//Returns the extracted string
return result;
}//End of method
/**
* Returns the substring representing the last four characters of the string.
* @param string a non-null string of length >= 4
* @return the substring representing the last four characters of the string
*/
public static String lastFour(String string)
{
//Extracts last 4 characters from the given string
//Second parameter is by default length
String result = string.substring(string.length()-4);
//Returns the extracted string
return result;
}//End of method
/**
* Returns the substring representing the last n characters of the string.
* @param string a non-null string of length >= n
* @param n an integer >= 0
* @return the substring representing the last n characters of the string
*/
public static String lastN(String string, int n)
{
//Extracts last N characters from the given string
//Second parameter is by default length
String result = string.substring(string.length()-n);
//Returns the extracted string
return result;
}//End of method
public static void main(String[] args)
{
System.out.println(" Finds Marc");
System.out.println("Index position of Marc " + findMarc("This is Marc one"));
System.out.println("Index position of Marc " + findMarc("This is Darc one"));
System.out.println(" Finds Substring");
System.out.println("Finds substring one " + findSubstring("This one is Demo one", "one"));
System.out.println("Finds substring one " + findSubstring("This is Demo", "one"));
System.out.println(" Check Substring contains or not");
System.out.println("Checks substring two " + contains("This two is Demo two", "two"));
System.out.println("Checks substring two " + contains("This is Demo", "two"));
System.out.println(" Split string with space");
String res[] = splitIntoWords("This two is Demo two");
for(String data : res)
System.out.println(data);
System.out.println(" Displays first four characters of the string");
System.out.println("First four characters of the string: " + firstFour("There is two Demo"));
System.out.println(" Displays first N characters of the string");
System.out.println("First N characters of the string: " + firstN("There is two Demo", 8));
System.out.println(" Displays last 4 characters of the string");
System.out.println("Last 4 characters of the string: " + lastFour("There is two Demos"));
System.out.println(" Displays last N characters of the string");
System.out.println("Last N characters of the string: " + lastN("There is two Demos", 7));
}//End of main method
}//End of class
Sample Run:
Finds Marc
Index position of Marc 8
Index position of Marc -1
Finds Substring
Finds substring one 5
Finds substring one -1
Check Substring contains or not
Checks substring two true
Checks substring two false
Split string with space
This
two
is
Demo
two
Displays first four characters of the string
First four characters of the string: Ther
Displays first N characters of the string
First N characters of the string: There is
Displays last 4 characters of the string
Last 4 characters of the string: emos
Displays last N characters of the string
Last N characters of the string: o Demos
Using List
import java.util.*;
//Class ListExercise definition
public class ListExercise
{
/**
* Counts the number of characters in total across all strings in the supplied list;
* in other words, the sum of the lengths of the all the strings.
* @param l a non-null list of strings
* @return the number of characters
*/
public static int countCharacters(List<String> l)
{
//Counter is initialized to zero
int numberOfCharacters = 0;
//Loops till end of list
for(String st: l)
{
//Converts the string to character array and extract the characters
for(@SuppressWarnings("unused") char ch : st.toCharArray())
//Increase the counter by one
numberOfCharacters++;
}//End of for loop
//Returns the counter
return numberOfCharacters;
}//End of method
/**
* Splits a string into words and returns a list of the words.
* If the string is empty, split returns a list containing an empty string.
*
* @param s a non-null string of zero or more words
* @return a list of words
*/
public static List<String> split(String s)
{
//Creates a array list of type string
ArrayList<String> splitString = new ArrayList<String>();
//Checks whether the string is empty or not
if (s.isEmpty())
{
splitString.add(s);
return splitString;
} //End of if
//Checks whether s not contains null
if (!(s.contains("")))
{
splitString.add(s);
return splitString;
} //End of if
//Creates a string array to store the splitted data
String[] res = s.split("\s+");
//Loops till end of the string array
for (String part : res)
{
//Adds each word to the list
splitString.add(part);
}//End of for loop
return splitString;
}//End of method
/**
* Returns a copy of the list of strings where each string has been
* uppercased (as by String.toUpperCase).
*
* The original list is unchanged.
*
* @param l a non-null list of strings
* @return a list of uppercased strings
*/
public static List<String> uppercased(List<String> l)
{
//Creates a list
List<String> upperString = new ArrayList<String>();
//Loops till end of the list
for(String st: l)
//Converts the string to upper case and add it to the list
upperString.add(st.toUpperCase());
//Returns the upper case list
return upperString;
}//End of method
/**
* 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)
{
//Initializes the flag to zero
int flag = 0;
//Loops till end of the list
for(String st: l)
{
//Extracts first character from the string
char ch = st.charAt(0);
//Checks for not upper chase character
if(!Character.isUpperCase(ch))
{
//Set the flag to one
flag = 1;
//Come out of the loop
break;
}//End of if
}//End of for loop
//Checks if the flag value is zero return true
if(flag == 0)
return true;
//Otherwise return false
else
return false;
}//End of method
/**
* Returns a list of strings selected from a supplied list, which contain the character c.
*
* The returned list is in the same order as the original list, but it omits all strings
* that do not contain c.
*
* The original list is unmodified.
*
* @param l a non-null list of strings
* @param c the character to filter on
* @return a list of strings containing the character c, selected from l
*/
public static List<String> filterContaining(List<String> l, char c)
{
//Creates a list
List<String> charC = new ArrayList<String>();
//Initializes the flag to zero
int flag = 0;
//Loops till end of list and extracts the string
for(String st: l)
{
//Loops till end of the string
for(int counter = 0; counter < st.length(); counter++)
{
//Checks whether the character at the index position is equal to the specified character or not
if(st.charAt(counter) == c)
{
//Set the flag to one
flag = 1;
//Come out of the loop
break;
}//End of if
}//End of inner for loop
//Checks if the flag is one add the string to the list
if(flag == 1)
//Adds the string to the list
charC.add(st);
//Re-set the flat to zero for the next string
flag = 0;
}//End of outer for loop
//Returns the list
return charC;
}//End of method
/**
* Inserts a string into a sorted list of strings, maintaining the sorted property of the list.
*
* @param s the string to insert
* @param l a non-null, sorted list of strings
*/
public static void insertInOrder(String s, List<String> l) {
}
//Main method definition
public static void main(String[] args)
{
//Creates a list of type string
List<String> list = new ArrayList<String>();
//Creates a list to store result
List<String> res;
//Adds the strings to the list
list.add("This is Demo");
list.add("Check this");
list.add("try this");
list.add("Do this for me");
System.out.println("Count characters in the list: " + countCharacters(list));
res = split("This is demo");
System.out.println("Count characters in the list: " + res);
System.out.println("Before Upper case list: " + list);
res = uppercased(list);
System.out.println("After Upper case list: " + res);
System.out.println("Each string in the supplied list of strings starts with an uppercase letter: " + allCapitalizedWords(list));
res = filterContaining(list, 'D');
System.out.println("List contains C character: " + res);
}//End of main method
}//End of class
Sample Run:
Count characters in the list: 44
Count characters in the list: [This, is, demo]
Before Upper case list: [This is Demo, Check this, try this, Do this for me]
After Upper case list: [THIS IS DEMO, CHECK THIS, TRY THIS, DO THIS FOR ME]
Each string in the supplied list of strings starts with an uppercase letter: false
List contains C character: [This is Demo, Do this for me]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.