Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The language is Java. 1. Create a class called ArrayReview with one data field of an integer array. (2 points) 2. Add a constructor to

The language is Java.

1. Create a class called ArrayReview with one data field of an integer array. (2 points) 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. (3 points) 3. Add the following methods to the class and use them in the main method: (5 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 4. Popular a two-dimension array from an input file of 5 rows and 10 columns, sort each row and sort each column, then print out the data to an output file. (5 points) 5. Create a main method to print the integer array after sorted using for-each state without using indexes.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I finished the code, Need help changing my one-dimensional array to a 2- dimensional array?

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.Arrays; import java.util.Random;

public class ArrayReview { private int[] array; public ArrayReview(int length) { Random r = new Random(100); array = new int[length]; for(int i =0 ;i

public void setArray(int[] array) { this.array = array; } public void setKthItem(int k, int item) { array[k] = item; } public int pickMaxIndex( int start, int end) { int max = array[start]; int index=start; for(int i = start + 1 ;i< end ;i++) { if(array[i] >= max) { max = array[i]; index=i; } } return index; } public void swap(int i , int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } public void selectSort() { int max;int pos; for(int i = 0;imax) { max = array[j]; pos = j; } } swap(i,pos); } } public void printArray() { for(int value:array) System.out.println(value+ " "); } public static void main(String[] args) { ArrayReview a1=new ArrayReview(5); System.out.println(a1); System.out.println(" Index of maximum number frm 0 to 4 is:"+a1.pickMaxIndex(0, 4)); a1.swap(2, 3); System.out.println(" After swaping array is :"); System.out.println(a1); a1.selectSort(); System.out.println(" After sorting array is :"); System.out.println(a1); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions