Question 1
Write a Java program containing the static methods described in the following paragraphs. You need not include exception handling and validation in your solution. You must not use the following in your solution:
Method 1 public static void printNumbers(int startNum, int endNum, int numPerLine){ // Your code } |
The method will print numbers from startNum to endNum, inclusive of both ends. The method will print numPerLine numbers per line. Example 1: printNumbers(21, 29, 4) will produce the following: 21 22 23 24 25 26 27 28 29 Example 2: printNumbers(4, 12, 6) will produce the following: 4 5 6 7 8 9 10 11 12 You may assume:
You must not use int[] or ArrayList in your solution. |
Method 2 public static boolean hasDuplicateChars(String data){ // Your code } |
The method will return true if there are duplicate characters in data and false otherwise. Examples: hasDuplicateChars("abca1a2") will produce true. hasDuplicateChars("morning") will produce true. hasDuplicateChars("ab$wx$y") will produce true. hasDuplicateChars("35 x 12") will produce true. (2 blank spaces) hasDuplicateChars("methods") will produce false. You may assume the letters: a, b, c … z are in lower case. You must not use char[] or ArrayList in your solution. |
Method 3 public static String replaceWith(String data, char ch1, char ch2){ // Your code } | ||
The method will return an instance of String containing all characters in data but with all occurrences of ch1 replaced by ch2. You must use recursion.
|
|
|
|
|
Method 4 public static int countDigit(int number, int digit){ // Your code } | ||
The method will count and return the number of occurrences of digit within number. You must use recursion.
|
|
|
|
|
You may include additional variables and methods as you deem fit. public class Question1 { public static void main(String[] argv){ // Add statements to call and test the various methods. // The statements in this method will NOT be graded. } public static void printNumbers(int startNum, int endNum, int numPerLine){ // Your code } public static boolean hasDuplicateChars(String data){ // Your code } public static String replaceWith(String data, char ch1, char ch2){ // Your code } public static int countDigit(int number, int digit){ // Your code } } |
Program Structure
You may include additional variables and methods as you deem fit. public class Question1 { public static void main(String[] argv){ // Add statements to call and test the various methods. // The statements in this method will NOT be graded. } public static void printNumbers(int startNum, int endNum, int numPerLine){ // Your code } public static boolean hasDuplicateChars(String data){ // Your code } public static String replaceWith(String data, char ch1, char ch2){ // Your code } public static int countDigit(int number, int digit){ // Your code } } |