Answers
Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================
// Homework.java
public class Homework {
public static void main(String[] arg) {
char[] a = { 'a', 'b', 'c', 'd', 'x', 'y', '1', '2', '3', '4' };
char[] b = { 'p', 'q', '9', '8', '7', '6' };
int[] c = { 6, 0, 1 };
// Testing initializeArray
printArray(a);
initializeArray(a);
printArray(a);
// Testing selectionSort
printArray(b);
selectionSort(b);
printArray(b);
// Testing factorial
System.out.println(factorial(5));
System.out.println(factorial(c[0]));
System.out.println(factorial(c[2]));
}
private static int factorial(int num) {
int res = 0;
if (num == 0) {
res = 1;
} else if (num == 1) {
res = 1;
} else if (num > 1) {
if (num - 1 == 1) {
}
res = num * factorial(num - 1);
}
return res;
}
private static void selectionSort(char[] b) {
int minIndex;
char temp;
int SIZE = b.length;
for (int i = 0; i < SIZE - 1; i++) {
minIndex = i;
for (int j = i + 1; j < SIZE; j++) {
if (b[j] < b[minIndex])
minIndex = j;
}
// swap array
if (minIndex != i) {
temp = b[i];
b[i] = b[minIndex];
b[minIndex] = temp;
}
}
}
private static void initializeArray(char[] a) {
for (int i = 0; i < a.length; i++) {
if ((i + 1) % 2 == 0) {
a[i] = 'b';
} else {
a[i] = 'a';
}
}
}
private static void printArray(char[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
============================
output;
a b c d x y 1 2 3 4
a b a b a b a b a b
p q 9 8 7 6
6 7 8 9 p q
120
720
1
=========================Thank You
.