Question: Make a program that has three methods public static void main(String [] args) public static boolean search (int [] arr, int num) public static void
Make a program that has three methods
public static void main(String [] args)
public static boolean search (int [] arr, int num)
public static void sort(int [] arr)
In main
1)Make an array of 101 random integers between 0,100. Print out first, middle and last value
2)Pass the array to the sort method. Review what it means to use an array and pass a reference as the parameter vs when you use primitive and pass value
3)Print out the first , middle and last value from the array after the return
4)Prompt the user to enter the value they want to search for
5)Pass the array and the number to the search method
6) Using an if statement print out if the value searched for is found
B. Make Search method
public static boolean search(int[] a, int b) { if (a.length == 0) { return false; } int low = 0; int high = a.length-1; while(low <= high ) { int middle = (low+high) /2; if (b> a[middle] ){ low = middle +1; } else if (b< a[middle]){ high = middle -1; } else { // The element has been found return true; } } return false; }
C. Make Sort method
public static void sort(int a[]) {
int n = a.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (a[j] < a[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
int temp = a[min_idx];
a[min_idx] = a[i];
a[i] = temp;
}
}
D. Add to the class import java.util.Arrays;
Look over the API(Application programmer Interface) for java Arrays(Arrays) .
E. Add to your program a print statement using Arrays print
Part 2 Luhns Algorithm
Read this description of Luhns algorthim. Think about how you would solve it.
.Make a program that has a main method where you ask the user to enter 11 digits between 0 and 9 and store these number in array. Pass array to a method that implements the Luhns algorithm . Test what gets returned -
Suggestions:
How store the 11 digit number?
2. How test if index is odd or even - if it is odd double value
3. How decide if new doubled value is greater than or equal to 10. If so, split digits apart and add values
4. Sum values(not including the last value)
5. How would you test if (sum*9) %10 equals the last value
6. If 5. is true it is a valid credit card number
Sample Output for Part 2
----jGRASP exec: java Luhn
enter num
7
enter num
9
enter num
9
enter num
2
enter num
7
enter num
3
enter num
9
enter num
6
enter num
7
enter num
1
enter num
3
Number entered is :[7, 9, 9, 2, 7, 3, 9, 6, 7, 1, 3]
Number is invalid
----jGRASP: operation complete.
----jGRASP exec: java Luhn
enter num
7
enter num
9
enter num
9
enter num
2
enter num
7
enter num
3
enter num
9
enter num
8
enter num
7
enter num
1
enter num
3
Number entered is :[7, 9, 9, 2, 7, 3, 9, 8, 7, 1, 3]
Number is valid
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
