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

Using JAVA Array Exercises: Create a class named ArrayExercises that contains th

ID: 3844279 • Letter: U

Question

Using JAVA

Array Exercises: Create a class named ArrayExercises that contains the following array tasks in the main method:
1. 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);
2. 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.
3. (Challenge:) 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 i

Explanation / Answer

//please change the package name as of yours choice

package com.vimal.config;

import java.util.Arrays;
import java.util.Scanner;

public class ArrayExercises {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
   //   1. 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);
       Scanner sc = new Scanner(System.in);
       int input = Integer.parseInt(sc.nextLine());
       int [] arrayInteger = new int[input];
       for(int i=0;i<input;i++)
       {
           //generate random number between -17 to 19
           int random = (int) (Math.random() * (19 - (-19))) + (-19);
           //add number to aaray
           arrayInteger[i]=random;
       }
      
       //display array elements
       for(int i=0;i<arrayInteger.length;i++)
       {
           if((i)%10==0)
               System.out.println();
           System.out.print(arrayInteger[i]+",");
       }
      
      
      
       //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.
      
       char[] ar = {'t', 'o', 'y', 'o', 't', 'a'};
       char[] b = {'r', 'a', 'c', 'e', 'c', 'a', 'r'};
       char[] revarr = new char[ar.length];
       for(int i=0;i<ar.length;i++)
       {
           revarr[i]=ar[ar.length-i-1]; // here we are reversing array ar in the neew array revarr
       }
      
       // check for palindrome
       System.out.println();
       System.out.println("Array A is palindrom? "+palindrome(ar));
       System.out.println("Array b is palindrom? "+palindrome(b));
      
       //3. (Challenge:) 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 i
      
       reversearrayInpalce(b);
       System.out.println(b);

   }
  
  
   //method to check any array is palindrome or not
   public static boolean palindrome(char[] arr)
   {
       boolean result=true;
       for(int i=0;i<arr.length/2;i++) // for palindrome compare first elemnt from last element. till we reach to
       { // to the middle of array.
           if(arr[i]!=arr[arr.length-i-1])
           {
               result=false;
               break;
           }
          
       }
       return result;
   }
  
   public static void reversearrayInpalce(char [] arr)
   {
       for(int i=0;i<arr.length/2;i++)
       {
           //start swapping from first to last till middle of array
           char temp=arr[i];
           arr[i]=arr[arr.length-i-1];
           arr[arr.length-i-1]=temp;
       }
   }
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote