JAVA LanguageSolutionHi Please find my implementation Please

JAVA Language

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.Random;

import java.util.Scanner;

public class ArraySkills {

   public static void printArray(String[] arr){

       for(int i=0; i<arr.length; i++)

           System.out.println(arr[i]);

   }

   // Implementing Fisher–Yates shuffle

   public static void shuffleArray(String[] ar)

   {

       Random rnd = new Random();

       for (int i = ar.length - 1; i > 0; i--)

       {

           int index = rnd.nextInt(i + 1);

           // Simple swap

           String a = ar[index];

           ar[index] = ar[i];

           ar[i] = a;

       }

   }

   public static void main(String[] args) {

       // ***********************

       // For each item below you must code the solution. You may not use any of the

       // methods found in the Arrays class or the Collections classes

       //

       String[] myData;

       Scanner sc = new Scanner(System.in);

       // 1. Instantiate the given array to hold 10 Strings.

       myData = new String[10];

       // 2. Add your name to the Array at index 0 and a friend\'s name to the Array at index 4

       myData[0] = \"Pravesh Kumar\";

       myData[4] = \"Alex Bob\";

       // 3. Move your friend\'s name to index 0 in the array and \"delete\" their name from index 4

       myData[0] = myData[4];

       myData[4] = null;

       //System.out.println(myData[0]);

       // 4. Fill up all of the remaining indexes in the array with more names

       for(int i=1; i<10; i++){

           System.out.print(\"Enter name to store at index \"+i+\" :\" );

           myData[i] = sc.nextLine();

       }

       // 5. Swap the values at index 5 and index 1

       String temp = myData[5];

       myData[5] = myData[1];

       myData[1] = temp;

       // 6. Print the array from beginning to end.

       printArray(myData);

       // 7. Shuffle the array of strings

       shuffleArray(myData);

       // 8. Find and print the longest and shortest Strings in the array

       // initializing with first element

       String longest = myData[0];

       String shortest = myData[0];

       for(int i=1; i<myData.length; i++){

          

           if(myData[i].length() < shortest.length())

               shortest = myData[i];

          

           if(myData[i].length() > longest.length())

               longest = myData[i];

       }

      

       System.out.println(\"Longest string: \"+longest);

       System.out.println(\"Shortest string: \"+shortest);

       // 9. Add up the total number of characters in all of the strings in the array and print the answer

       int total = 0;

       for(int i=0; i<myData.length; i++)

           total += myData[i].length();

       System.out.println(\"Total number of characters: \"+total);

       // 10. Prompt the user for a String and then perform a linear search of the array

       // to see if that string is or is not in the array. Print if it is or is not found.

       System.out.print(\"Enter a string to search: \");

       String search = sc.nextLine();

      

       int i;

       for(i=0; i<myData.length; i++){

          

           if(search.equals(myData[i])){

               System.out.println(search+ \" found at index \"+i);

               break;

           }

       }

       if(i == myData.length){

           System.out.println(search+ \" not found\");

       }

       // 11. Remove the item at index 4 of the array by shifting everything after it one spot to the left.

       // This means your array should have one empty index at the end after the shift (delete the duplicate item at the end).

       for(int j=4; j<myData.length-1; j++){

           myData[j] = myData[j+1];

       }

       myData[myData.length-1] = null;

       // 12. Create a new array that is twice as big as the current array and copy all of the items to the new array.

       // When complete, swap references so our old array gets garbage collected and the new array is pointed to by your array variable myData.

       String[] newArr = new String[myData.length*2];

       for(int j=0; j<myData.length; j++){

           newArr[j] = myData[j];

       }

       myData = newArr;

       newArr = null;

       // 13. Prompt the user to enter 2 numbers within the range of the array\'s length. If the first is larger than the second print backwards from that index to the first.

       // If the second is larger than the first, print forward in the array from the first index to the second.

       int low, high;

       System.out.print(\"Enter lower index: \");

       low = sc.nextInt();

       System.out.print(\"Enter higher index: \");

       high = sc.nextInt();

      

       if(low <= high){

           for(int j=low; j<=high; j++)

               System.out.println(myData[j]);

       }else{

           for(int j=high; j>=low; j--)

               System.out.println(myData[j]);

       }

   }

}

/*

Sample run:

Enter name to store at index 1 :pravesh

Enter name to store at index 2 :alex

Enter name to store at index 3 :bob

Enter name to store at index 4 :mukesh

Enter name to store at index 5 :ramesh

Enter name to store at index 6 :vikash

Enter name to store at index 7 :pintu

Enter name to store at index 8 :ajad

Enter name to store at index 9 :gaurav

Alex Bob

ramesh

alex

bob

mukesh

pravesh

vikash

pintu

ajad

gaurav

Longest string: Alex Bob

Shortest string: bob

Total number of characters: 55

Enter a string to search: ajad

ajad found at index 9

Enter lower index: 2

Enter higher index: 7

pravesh

mukesh

alex

ramesh

bob

vikash

*/

JAVA LanguageSolutionHi, Please find my implementation. Please let me know in case of any issue. import java.util.Random; import java.util.Scanner; public class
JAVA LanguageSolutionHi, Please find my implementation. Please let me know in case of any issue. import java.util.Random; import java.util.Scanner; public class
JAVA LanguageSolutionHi, Please find my implementation. Please let me know in case of any issue. import java.util.Random; import java.util.Scanner; public class
JAVA LanguageSolutionHi, Please find my implementation. Please let me know in case of any issue. import java.util.Random; import java.util.Scanner; public class
JAVA LanguageSolutionHi, Please find my implementation. Please let me know in case of any issue. import java.util.Random; import java.util.Scanner; public class

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site