Create a class that has three int variables (int start,int stop, int step), wher
ID: 3548490 • Letter: C
Question
Create a class that has three int variables (int start,int stop, int step), where start and stop are the bounds of the numbers, and step is the increments between the numbers. This should be added to an array list that displays the information. It must be displayed in an array list For Example (int start, int stop, int step) = (5, 10, 1). Arraylist will display, (5, 6, 7, 8, 9, 10) Example 2 (int start, int stop, int step) (5,20,3) = 5,8,11,14,17,20) Number 2 create a method in the same class that takes in two ArrayList values, such that you return an ArrayList that combines and sorts the two together. For Example, int1[]= 1,3,,5,7,8,9 int2[]= 2,4.6.8.10. so result[] = 1,2,3,4,5,6,7,8,9,10Explanation / Answer
public static int[] merge(int[] a, int[] b) {
int[] answer = new int[a.length + b.length]; int i = 0, j = 0, k = 0; while (i < a.length && j < b.length) { if (a[i] < b[j]) { answer[k] = a[i]; i++; } else { answer[k] = b[j]; j++; } k++; }
while (i < a.length) { answer[k] = a[i]; i++; k++; }
while (j < b.length) { answer[k] = b[j]; j++; k++; }
return answer; } public static int[] merge(int[] a, int[] b) {
int[] answer = new int[a.length + b.length]; int i = 0, j = 0, k = 0; while (i < a.length && j < b.length) { if (a[i] < b[j]) { answer[k] = a[i]; i++; } else { answer[k] = b[j]; j++; } k++; }
while (i < a.length) { answer[k] = a[i]; i++; k++; }
while (j < b.length) { answer[k] = b[j]; j++; k++; }
return answer; } //this can be used for merging into single array
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.