1)For this question, a list of integers called list is passed in as a parameter. How to...
Question:
1)For this question, a list of integers called list is passed in as a parameter.
How to Implement this method so that it creates an returns a new list containing the items in the original list in reverse order.
For the result, how to create a new ArrayList
2)The method is given a list of strings called list and a string value.
How to Implement it so that it returns the index of the last occurrence of value in list.
For example, given ["cat", "dog", "goose", "dog", "cat"] and "dog", this method would return 3 (the index where the second occurrence of "dog" appears).
If list does not contain the value, this method should return -1.
For example, given ["cat", "dog", "goose", "dog", "cat"] and "salamander", this method would return -1.
Hint: Remember that when comparing Strings you must use string1.equals(string2), not string1 == string2.
3)How to Implement the following method that takes a Picture object called pic so that it turns the bottom half of the image white.
How to Do this using nested for loops, using getPixel() to access individual pixels by coordinate location. please Remember that white is (255, 255, 255).
4) This method takes a list of strings called list. How to Implement it so that it returns the total number of vowels ('a', 'e', 'i', 'o', and 'u') in all strings in the list.
For example, given ["ear", "prune", "plot", "try"], it should return 5.
5) Given a string, if the same 2-character substring appears at both its beginning and end, return the contents of the string without the 2-character substring at the beginning.
For example, "HelloHe" yields "lloHe".
Kindly Note that the 2-character substring at the beginning may overlap with the one at the end, so "Hi" yields "".
If the two characters at the start and end of the string do not match each other, return the original string unchanged.
6)The method below takes a string as a parameter. The string contains a name in the form of "
For example:
- If the input string is "John,Smith", this method would return "Smith,John"
- If the input string is "Kathy,Bee", this method would return "Bee,Kathy"
public String swapNameFormatting(String name) { }
7) The method below takes an int parameter n. How to Implement the methodd so that it returns a string containing the numbers from 1 to n concatenated together.
For example:
- If n is 3, this method would return "123"
- If n is 10, this method would return "12345678910"
Kindly Note: Use a numeric for loop, and think about what value you want to start with, as well as what value you want to end with. Your answer will be simpler if you do not follow the normal pattern for the starting value and loop condition.
public String createNumberString(int n) { } 8) How to Write a method that will return a string composed of the last n characters of wholeString.
For example:
- Running getSuffix("reading", 3) should return "ing"
- Running getSuffix("Magic", 1) should return "c"
public String getSuffix(String wholeString, int n) { }
9) How to Implement the following method so that it returns the index of the last space (" ") in the string.
For example:
- If we call this method with the string "I'm making Waffles", this method would return 10, which is the index of the " " character between the 'g' in "making" and the 'W' in "Waffles".
public int indexOfLastSpace(String phrase) { }
10) The method below takes a String parameter called str. How to Complete the method so that it returns a new string where each each character in the original string has been repeated 3 times. For example if the string given is "cat", the string returned should be "cccaaattt". How to Finish the nested for loops so the method behaves correctly.
public String tripleLetters(String str) { String newString = ""; // Use a for loop to traverse the characters in the string for (int i = 0; // Finish this for loop ) { char letter = str.charAt(i); // perform the action in the body of the loop 3 times for (int j = 0; // Finish this for loop ) { newString = newString + letter; } }
return newString; } 11) The following method takes a list of strings as a parameter and returns the number of times the character 'e' appears in all of the strings together. It uses an outer loop to repeat over all the positions in the list, and an inner loop to repeat over all the character positions in one string. How to Complete the two loops so the method operates correctly. For example, if the list given is ["peter", "enter", "new"], this method would return 5.
public int countEs(ArrayList
12) This method below currently sets the color of all of the pixels in the given Picture to black. How to Modify the appropriate loop so that instead, it only sets the pixels on the left half of the image to black. In other words, all pixels with an x coordinate less than half the width should be set to black. How to finish the for loops below so that the method.
public void setLeftHalfOfPicture(Picture pic) { for (int x = 0; x < pic.getWidth(); x++) { for (int y = 0; y < pic.getHeight(); y++) { pic.getPixel(x, y).setColor(Color.BLACK); } } }
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates