Question 1: Username Method
Write a complete method to generate a username that matches these requirements:
· first initial of first name
· first five characters or last name (or full last name, if less than 5 characters long)
· a two-digit random number
· all lower case
Write code to go in a main method to read in the user’s first and last name and then invoke the method above and print out the generated username.
Question 2: Odd Number Array
Create an array that holds the first 100 odd numbers.
Iterate through the array and identify each number that is divisible by 5. Print the number, then double it.
To clairfy, when the code completes, the array will store [1, 3, 10, 7, 9, 11, 13, 30, …]
Question 3: Negative Array Methods
Write two methods that convert the elements of an array to negative numbers (e.g., convert 3 to -3).
The first method changes the elements in the array passed in as a parameter. The second creates a new array that contains the negative elements of the array passed in as a parameter. In the second method, the array passed in as a parameter should not change.
You can assume the initial array contains all positive numbers. The method headers are:
public void negateElementsInArray(int[] numbers)public int[] createNegativeElementsArray(int[] numbers)
Question 4: SubArray Method
Write a method to create a new array that contains only the elements between specified indices (inclusive). The method header is:
public int[] subArray(int[] numbers, int start, int end)
For example, if the method was invoked with {1, 5, 2, 4, 7, 3}, start = 1, end = 4, the method would return {5, 2, 4, 7}.
Include appropriate error checking for the indices.
Question 5: String Array
Write a method that converts all Strings in an array to upper case. The method header is:
public void convertToUpper(String[] words)
Question 6: Increasing Order Method
Write a complete method to determine whether an array of integers is in increasing order. For example, [4, 6, 8, 10, 10, 12] is increasing order but [4, 2, 6, 1, 1, 3] is not.
Question 7: Arrays and ArrayLists
In your own words, describe two similarities and two differences between arrays and ArrayLists. Describe one situation where you might want to use an array. Describe one situation where you might want to use an ArrayList.
Question 8: ArrayList Method
Write code statements to accomplish the following. Ask the user for a “code word.” Then ask them to enter words repeatedly until they enter “quit” and store those words in an ArrayList. If the word does not contain the “code word,” add it to the list. For example, if the “code word” is “cat,” and the user enters “cater,” “scatter,” “cast,” “quit,” then only “cast” would be stored on the list. Output the number of words on the list.
Question 9: Mode Method (Challenge!)
Write a complete method to find the mode of an integer array. The mode is the number that appears the most frequently. For example, in the array [0, 4, 2, 7, 2, 4, 2], the mode is 2, because it appears three times. For this method, if there is more than one mode, you can return either. For example, for the array [4, 1, 3, 1, 4], you could return either 1 or 4.
Note that this method is more challenging that what I would ask you to do on the homework or an exam!
The method header is:
public int findMode(int[] numbers)
Question 10: String Method Trace A
What is the value of text after each statement below? Also, show output that is printed, when applicable.
String text = “California”;text.toLowerCase();System.out.println(text.toLowerCase());text = text.toLowerCase();System.out.println(text.length());text = text.substring(2,6);System.out.println(text.substring(1));text = text.substring(1,2);System.out.println(text + 1 + 3);System.out.println(1 + 3 + text);System.out.println(text.equals(“i”));System.out.println(text==”i”);System.out.println(text.equalsIgnoreCase(“I”));
Question 11: String Method Trace B
What is the value of name after each statement below? Also, show output that is printed, when applicable.
String name = “Dr. Suess”;System.out.println(name.toLowerCase());name.toUpperCase();name = name.toLowerCase();boolean same1 = name.equals(“dr. suess”);boolean same2 = name==“dr. suess”;System.out.println(same1 + “ and ” + same2);System.out.println(name.length());name.replace(‘s’,’x’);name = name.substring(2, 7);name += 1 + 3 + “author”;
Question 12: String vs String Builder
In your own words, explain the difference in these classes. What are some pros and cons of each class?