Question

Java: 1. Create a class called ArrayReview with one data field of an integer array. (2...

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)

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1. Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with...
1. Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. 2. Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. 3. You have been given the job of creating a new order processing system for the Yummy Fruit CompanyTM. The system reads pricing information for the various delicious...
Java... Write a class named TestScores. The class constructor should accept an array of test scores...
Java... Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to...
Write a Java class called FlexArray that has/does the following: Has a private variable array that...
Write a Java class called FlexArray that has/does the following: Has a private variable array that holds integers as int[] array. Has a private variable capacity that is the maximum number of integers the array can hold. Has a private variable size that shows the number of currently occupied locations. Obviously, capacity >= size. Capacity tells you how many values the array can hold, not how many it has currently, which is available via the variable size. Has a default...
Step 1: Create a new Java project in NetBeans called “PartnerLab1” Create a new Java Class...
Step 1: Create a new Java project in NetBeans called “PartnerLab1” Create a new Java Class in that project called "BouncyHouse" Create a new Java Class in that project called “Person” Step 2: Implement a properly encapsulated "BouncyHouse" class that has the following attributes: Weight limit Total current weight of all occupants in the bouncy house (Note: all weights in this assignment can be represented as whole numbers) …and methods to perform the following tasks: Set the weight limit Set...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1....
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January - December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs to adjust the...
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1....
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs to adjust the...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. // A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. // At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the...
Write a class called Item that has a string field called name and a double field...
Write a class called Item that has a string field called name and a double field called price and an integer field called quantity. In main, create a bag of type Item and place several item objects in the bag. Then in main calculate the total price of items purchased. You may remove each item and add the price to a total variable in a loop. The loop should end when the bag is empty. Using bag algorithms public final...
In Java please 10.9 Lab 6 In BlueJ, create a project called Lab6 Create a class...
In Java please 10.9 Lab 6 In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as you go Copy the code for LineSegment given below into the class. Take a few minutes to understand what the class does. Besides the mutators and accessors, it has a method called print, that prints the end points of the line segment in the form: (x, y) to (x, y) You will have to write two methods in...