Java Array and String

 

JAVA

Array and String



        Fill in the blanks

 

1.   Array is Collection of Similar data type

2.   Write array syntax datatype variablename[]={value};

3.   Write Array types 2 (single,Multi)

4.   Array of array is called Ragged Array

5.   Array store Multiple values in single variable.[T/F]. True

6.   String is Collection of Character

7.   Write string Concatenation operator +

8.   The String class supports several constructor.[T/F]. True

9.   Write a syntax of string String obj=new String();

10.         String s=’’four:’’+2+2; System.out.println(s)= four:4

11.         S1=’’hai’’.S2=’’HAI’’. S1.equals(S2) = false

12.         A=’’HAPPY’’.lower(A)=  happy

13.         S=”welcome”. upper(S)=  WELCOME

14.         X=”hello”.  length(X)= 5

15.         B=”apple”. reverse(B) = elppa

 

Answer the Following Questions

                                                                (4 marks)

1.Write a Single dimensional array

class ArrayDemo

{

       int i;

       int arr[]={1,2,3,4,5,6,7,8,9,10};

       void result()

       {

       System.out.println("Array details \n");

       for(i=0;i<10;i++)

       {

         System.out.print(arr[i]+"\t");

       }

     }

      public static void main(String args[])

       {

          ArrayDemo d=new ArrayDemo ();

          d.result();

       }

}

 

2.Write a program for Average an array of values

class Average{

       public static void main (String args[]){

             double nums[]={10.1,11.2,12.3,13.4,14.5};

            double result=0;

            int i; 

             for(i=0;i<5;i++)

             result=result+nums[i];

             System.out.println("average is"+result/5);

}

}

3.Write a program for Ragged Array

class TwoDAgain{

        public static void main (String args[]) {

                       int twoD[][]=new int[4][];

                       twoD[0]=new int[1];

                       twoD[1]=new int[4];

                       twoD[2]=new int[3];

               twoD[3]=new int[2];

                       int i,j,k=0;

                       for(i=0;i<4;i++)

                              for(j=0;j<twoD[i].length;j++){

                                            twoD[i][j]=k;

                                            k++;

                   }

              for(i=0;i<4;i++) {

                       for(j=0;j<twoD[i].length;j++)

                                     System.out.print(twoD[i][j]+" ");

                       System.out.println();

          }

   }

}

4.Write a program for string lower ,string upper,string equals() function

class ChangeCase{

public static void main(String args[])

{

String s="This is a test.";

System.out.println("Original:"+s);

String upper=s.toUpperCase();

String lower=s.toLowerCase();

System.out.println("Uppercase:"+upper);

System.out.println("Lowercase:"+lower);

}

}

5.Write a program for index() and lastindexof() program in string

class IndexOfDemo {

    public static void main(String[] args) {

        String s="Now is the time for all good men "+"to come to the aid of their country.";

        System.out.println(s);

        System.out.println("indexOf(t)="+s.indexOf('t'));

        System.out.println("lastIndexOf(t)="+s.lastIndexOf('t'));

        System.out.println("indexOf(the)="+s.indexOf("the"));       System.out.println("lastIndexOf(the)"+s.lastIndexOf("the"));

}

}


 

Answer the Following Questions

                                                                (10 marks)

1.Write a program using String Buffer.

import java.lang.*;

class Manipulate

{

public static void main(String args[])

{

StringBuffer s1=new StringBuffer("Object Language");

System.out.println("\n Original String :"+s1);

System.out.println("\n Length of the String:"+s1.length());

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

{

int p=i+1;

System.out.println("Character at Position "+p+"is:"+s1.charAt(i));

}

String s2=new String(s1.toString());

int pos=s2.indexOf(" Language");

System.out.println("Position of Language:"+pos);

s1.insert(pos," Oriented");

System.out.println("\n Modified String:"+s1);

s1.setCharAt(6,'-');

System.out.println("\nString Now:"+s1);

s1.append("improves security");

System.out.println("\nAppended String:"+s1);

}

}

No comments:

Post a Comment