Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

help with this please Create a class named ArrayExercises that contains the foll

ID: 3572618 • Letter: H

Question

help with this please

Create a class named ArrayExercises that contains the following array tasks in the main method, as well as the following array methods:

1. Array Tasks:

a) Write some code that asks the user to enter an integer via the keyboard, use that integer to set the length of an array and populate it with random integers from -17 to 19 (inclusive). Print the array back in a nice way (10 numbers per line with commas and such);

b) Below that write some java to reverse the arrays: char[] a = {’t’, ’o’, ’y’, ’o’, ’t’, ’a’} char[] b = {’r’, ’a’, ’c’, ’e’, ’c’, ’a’, ’r’} You should do this by creating new arrays to store the reversal.

Then write some more java code to prove to me that array b contains a palindrome, while array a does not.

2. Array Methods:

a) Write a method in your class called reverseArray that is of void return type and it takes as argument a String array (any String array the teacher can think of!) and it reverses the given array in place. That is, it manipulates the given array and modifies it (this is because arrays are passed in to methods by reference and not by value...we will talk more about this in our next lecture).

b) Write a method named findMin that takes in an array of strings and a starting index, and calculates the minimum string (i.e. the earliest word alphabetically) in the array starting from the passed in index. The method should return the index where the minimum string is located. As before, make sure to test you method in main (you can hard code the array or use Scanner on keyboard input to populate the array).

Explanation / Answer

package test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class ArrayExercises {
  
   static Scanner in = new Scanner(System.in);
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       ArrayTask();
       Pallindrome();
       System.out.println("Enter the size for string array: ");
       int n=in.nextInt();
       in.nextLine();
       String s1[] = new String[n];
       for(int i=0;i<n;i++){
           s1[i] = in.nextLine();
       }
       reverseArray(s1);
       findMin(s1);
   }
  
   public static void ArrayTask(){  
       Scanner in = new Scanner(System.in);
       System.out.println("Enter the size of Array: ");
       int n = in.nextInt();
       int a[] = new int[n];
       Random rand = new Random();
       for(int i=0;i<n;i++){
           a[i] = -17 + rand.nextInt((19-(-17))/2) *2;;
       }
       for(int i=0;i<n;i++){
           if(i%10==0)
               System.out.println();
           System.out.print(a[i]+", ");  
       }
       System.out.println("");
   }
  
   public static void Pallindrome(){
       char[] a = {'t','o','y','o','t','a'};
       char[] b = {'r','a','c','e','c','a','r'};
       int l1 = a.length, l2 = b.length;
       char[] c = new char[l1];
       char[] d = new char[l2];
      
       for(int i=l1-1,k=0;i>=0;i--,k++){
           c[k] = a[i];
       }
       for(int i=l2-1,k=0;i>=0;i--,k++){
           d[k] = b[i];
       }
       boolean r = Arrays.equals(a, c);
       boolean r1 = Arrays.equals(b, d);
       if(r)
           System.out.println("A is Pallindrom");
       if(r1)
           System.out.println("B is Pallindrom");
   }
  
   public static void reverseArray(String s1[]){
       List<String> list = Arrays.asList(s1);
       Collections.reverse(list);
s1 = (String[]) list.toArray();
System.out.println("String array reversed");
for(int i=0; i < s1.length; i++){
System.out.println(s1[i]);
}
   }
  
   public static void findMin(String s1[]){
       Arrays.sort(s1);
   System.out.println("Min value "+s1[0]);
   }
}