Java:
1. Create a class called ArrayReview with one data field of an
integer array. (2 point)
2. Add a constructor to populate the array in the class using Java
random number generator, and a
setter/getter for the array, and toString(). The array length is
passed to the constructor. (2 point)
3. The Big Blue Something Company only sells blue things. The
quality control engineer has set up
a machine to spot check the colors of the items coming off the
assembly line. The machine
checks each item in several locations (from 2 to 100 locations per
item), and it reports the color
at each location. In order for an item to pass quality control, it
must be blue in at least 50% of
the locations. The first line of input contains the number of items
that have been checked. Each
other line contains the colors detected in a single item. The
colors are separated by spaces.
There is no space at the end of the line. You can assume that
colors will be spelled with lower
case letters (a–z). For each item, output whether the item “passed”
or “failed” the quality
control check. (5 points)
Example input:
5
blue red green
blue blue orange red
red green green red blue blue blue red
blue blue red
red orange blue
Example output (corresponding to the example input above):
Failed
Passed
Failed
Passed
failed
4. Add the following methods to the class and use them in the main
method: (10 points)
a. setKthItem(k, item) that set the kth element of the array to the
value given in “item”.
b. pickMaxIndex(arr, start, end) that returns the index of the
largest value of the array
from index start to index end.
c. Swap(arr, index-i, index-j) that swaps the value in index i with
the value in index j.
d. selectionSort() that sorts the array in descending order, i.e.
the largest one comes first
5. Create a main method to test your program. (1 point)
import java.util.Arrays; public class ArrayReview { int size; int data[]; public ArrayReview(int n) { data = new int[n]; for(int i=0; i<n; i++) { data[i] = (int) (Math.random() * (10 * n)); } size = n; } @Override public String toString() { return "ArrayReview [size: " + size + ", data: " + Arrays.toString(data) + "]"; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public int[] getData() { return data; } public void setData(int[] data) { this.data = data; } public void setKthItem(int k, int item) { if(k >= 0 && k < size) { data[k] = item; } } public int pickMaxIndex(int arr[], int start, int end) { int max = start; for(int i=start; i<=end; i++) { if(arr[i] > arr[max]) { max = i; } } return max; } public void swap(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public void selectionSort() { for(int i=0; i<size-1; i++) { int maxIndex = pickMaxIndex(data, i, size-1); swap(data, i, maxIndex); } } public static void main(String[] args) { ArrayReview r = new ArrayReview(10); System.out.println(r); r.selectionSort(); System.out.println(r); } }
************************************************** Your second question is completely unrelated and require different code along with file processing logic. I request you to please post single question for that in a separate thread, so that it helps us in explaining the things better to you. Hope you understand! Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
Get Answers For Free
Most questions answered within 1 hours.