Programming Exercise using Strings with Java Part A Using St

Programming Exercise using Strings with Java

Part A. Using Strings and String methods.

Modify the source code presented in class for searching String arrays,

Change the elements of the books[] array to have six of your favorite books as the array’s elements.

Change the parameters of the methods arrayname[i].endsWith, arrayname[i].startsWith, and arrayname[i].indexOf to reflect the titles of your books

Place your source code and a snapshot of your output below.

Part B. Using the Stringbuilder Class

Create a Java program that will…

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

Append a new element: Hyena

Replace Gorilla with Chimpanzee, then display the modified array

Delete the element Zebra, then display the modified array

Sort the Array and then display the sorted array

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

Starter Code:

//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);

    

}

}

Solution

a)

import java.util.*;
import java.lang.*;
import java.io.*;


class Books
{
public static void main(String[] args)
{
String Books[]={\"book1\",\"book2\",\"book3\",\"book4\",\"book5\"};
System.out.println(Books[2].endsWith(\"2\")); //Arrays starts with 0 index ,will return false as it checks in book3
System.out.println(Books[4].startsWith(\"b\"));// checks in book 5 whether book name starts with b returns true
System.out.println(Books[4].indexOf(\'k\')); // return the index of character k in book5
}
}

Output:

Success time: 0.05 memory: 711168 signal:0

b)

import java.util.*;
import java.lang.*;
import java.io.*;


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(\"Append Hyena-->\\t\"+sb);
//replace Gorilla with Chimpanzee
sb.replace(26,33,\"Chimpanzee\");
System.out.println(\"Replace Gorilla with Chimpanzee--->\"+sb);

//delete Zebra..Find start and end position of Zebra
sb.delete(13,18);
//display the String;
System.out.println(\"Delete Zebra---->\"+sb);
//sorting of Animals Array
Arrays.sort(Animals);
System.out.println(\"After Sorting Animals\");
for(int i =0; i<Animals.length;i++)
System.out.println(Animals[i]+\"\\t\");
  
//reversing Animals Array
System.out.println(\"After Reversing Animals\");
for(int i=Animals.length-1;i>=0; i--)
System.out.println(Animals[i]+\"\\t\");
}
}

Output:

Success time: 0.04 memory: 711168 signal:0

Programming Exercise using Strings with Java Part A. Using Strings and String methods. Modify the source code presented in class for searching String arrays, Ch
Programming Exercise using Strings with Java Part A. Using Strings and String methods. Modify the source code presented in class for searching String arrays, Ch
Programming Exercise using Strings with Java Part A. Using Strings and String methods. Modify the source code presented in class for searching String arrays, Ch

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site