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

I am a bit confused with a code in Java that I have to work on. The following ar

ID: 3796286 • Letter: I

Question

I am a bit confused with a code in Java that I have to work on. The following are the instructions:

1) Create a String array with the following elements: Lion, Tiger, Bear, Zebra, Elephant, Gorilla, Rhinoceros, then display the array

2) Append a new element: Hyena

3) Replace Gorilla with Chimpanzee, then display the modified array

4) Delete the element Zebra, then display the modified array

5) //Sort the Array and then display the sorted array

6) Reverse the order of the array and then display the elements of the reverse array

This is what I have so far:

//program to demo StringBuilder

public class ArrayAnimals

{

public static void main(String[] args)

{

    StringBuilder sb = new StringBuilder();

String Animals[] = {"Lion","Tiger","Bear","Zebra" ,"Elephant","Gorilla","Rhinoceros"};

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

sb.append(Animals[i]);

sb.append("Hyena");

     System.out.println(sb);

//delete Zebra..Find start and end position of Zebra

sb.delete(13,18);

//display the String;

    System.out.println(sb);

//replace an element

//sort the array

// But cannot use StringBuilder

//as there is no associated //method

Arrays.sort(Animals);

for (String Animal:Animals)

    System.out.println(Animal);

}

}

Explanation / Answer

StringArray.java

import java.util.Arrays;

public class StringArray {

   public static void main(String[] args) {
       //Declaring and initializing the String array
       String arr[]={"Lion", "Tiger","Bear", "Zebra", "Elephant", "Gorilla", "Rhinoceros"};
      
       //Displaying the array elements using for loop
       System.out.println("Displaying the Array Elements :");
       for(int i=0;i<arr.length;i++)
       {
           System.out.print(arr[i]+" ");
       }
      
       //Appending a new Element to the array
       int index=arr.length;
       arr[index-1]="Hyena";
      
       //Displaying the array elements after appending new element to the array
       System.out.println(" After appending displaying the array elements:");
       for(int i=0;i<arr.length;i++)
       {
           System.out.print(arr[i]+" ");
       }
      
       //Replacing the array element "Gorilla" with "Chimpanzee"
       for(int i=0;i<arr.length;i++)
       {
       if(arr[i].equals("Gorilla"))
       {
           arr[i]="Chimpanzee";
       }
       }
      
       //Displaying the array elements after replacing
       System.out.println(" After Replacing displaying the array elements:");
       for(int i=0;i<arr.length;i++)
       {
           System.out.print(arr[i]+" ");
       }
      
      
       //Deleting an array element "Zebra"
       for(int i=0;i<arr.length;i++)
       {
           if(arr[i].equals("Zebra"))
           {
               for(int j=i;j<arr.length-1;j++)
               {
                   arr[j]=arr[j+1];
               }
           }
          
       }
      
       //Decrementing the length of the array by 1 after deleting an array element
       int len=arr.length-1;
      
      
       //Displaying the array after deleting
       System.out.println(" After Deleting displaying the array elements:");
       for(int i=0;i<len;i++)
       {
           System.out.print(arr[i]+" ");
       }
      
      
       //Sorting the Array elements
   Arrays.sort(arr, 0,len);
      
  
   //Displaying the elements after sorting
       System.out.println(" After Sorting displaying the array elements:");
       for(int i=0;i<len;i++)
       {
           System.out.print(arr[i]+" ");
       }      
      
       //Displaying the array elements in reverse order
       System.out.println(" Displaying the array elements in reverse order:");
       for(int i=len-1;i>=0;i--)
       {
           System.out.print(arr[i]+" ");
       }      


   }

}

______________________

output:

Displaying the Array Elements :
Lion Tiger Bear Zebra Elephant Gorilla Rhinoceros

After appending displaying the array elements:
Lion Tiger Bear Zebra Elephant Gorilla Hyena

After Replacing displaying the array elements:
Lion Tiger Bear Zebra Elephant Chimpanzee Hyena

After Deleting displaying the array elements:
Lion Tiger Bear Elephant Chimpanzee Hyena

After Sorting displaying the array elements:
Bear Chimpanzee Elephant Hyena Lion Tiger

Displaying the array elements in reverse order:
Tiger Lion Hyena Elephant Chimpanzee Bear

______________Thank You