Вы находитесь на странице: 1из 10

Q1)WRITE A CONSTRUCTOR TO ADD 2D ARRAY?

CLASS ADD2DARRAY
{
ADD2DARRAY()
{
INT[] A={{1,2,3}{1,2,3}};
INT[] B={{4,5,6},{7,8,9}};
INT C[]=NEW INT[2][2];
for(int i=0;i<rows;i++)
{
for(int j=0;j<col;j++)
{
int c[i][j]=a[i][j]+b[i][j]

for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{

System.out.priint("addition of array:"+c[i][j]);
}

}
}
}
PUBLIC STATIC VOID MAIN(string[] args)
{
ADD2DARRAY T=NEW ADD2DARRAY ();

Q2)WRITE A CONSTRUCTOR TO SORT THE INTEGER ARRAY?

CLASS SORTARRAY
{

SORTARRAY()
{
int[] arr={2,1,3,4,10,9,8};
int temp=0;
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println(":"+arrays.tostring(a));
}
}
CLASS ARRSORT
{
PUBLIC STATIC VOI MAIN(STRING[] ARGS)
{
SORTARRASY S=NEW SORTARRASY();

}
}

Q3)WRITE A CONSTRUCTOR TO SORT A STRING ARRAY?

CLASS STRINGARRAYS
{

STRINGARRAYS()
{
string s[]={"d","e","v","u","r","a","m"};
int temp=0;
for(int i=0;i<s.length;i++)
{
for(int j=i+1;j<s.length;j++)
{
if(s[i].compareto(str[j])<0)
{
tmp=s[i];
s[i]=s[j];
s[j]=temp;

}
system.out.println(":"+s[i]);
}
}
}

CLASS MAINSTRING()
{
public static void main(String[] args)
{
MAINSTRING M=NEW MAINSTRING();
}
}
Q4)WRITE A CONSTRUCTOR TO MULTIPLICATION 2D ARRAY?

class MultidimensionArray
{
MultidimensionArray()
{
int m1[][]={{1,2,3},{4,5,6}};
int m2[][]={{7,8},{9,10},{11,12}};

int [][] r=new int[2][2];


int sum=0;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<3;k++)
{
sum=sum+m1[i][k]*m2[k][j];

}
r[i][j]=sum;
sum=0;

}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.print(r[i][j]+" ");
}
System.out.println();
}

}
}
CLASS MAINMULTIARRAYS
{
PUBLIC STATIC VOID MAIN(sTRING[] ARGS )
MultidimensionArray M=NEW MultidimensionArray ();

Q5)WRITE A CONSTRUCTOR TO 1COPY TO ANOTHER ARRAY?

class CONSTRUCORARRAY
{
CONSTRUCORARRAY()
{
int a[]={2,3,5,2,7,8};
int b[]=new int[6];
int n=a.length;
for(int i=0;i<n;i++)
{
b[i]=a[i];
System.out.print(b[i]+" ");
}

} }

CLASS MAINARRAY
{
PUBLIC STATIC VOID MAIN(STRING[] ARGS)
{
CONSTRUCORARRAY C=NEW CONSTRUCORARRAY();
}
}

Q6) WRITE A CONSTRUCTOR TO ASSIGN 1D ARRAY TO 2D ARRAY?

package constructors;

public class One1Dto2DArray {

One1Dto2DArray()
{
int[] a= {1,2,3,4,5,6};
int[][] b=new int[2][3];

int k=0;
for(int i=0;i<2;i++)

{
for(int j=0;j<3;j++)
{
b[i][j]=a[k];
System.out.print(" " +b[i][j]);
k++;
}
System.out.println();
}

public static void main(String[] args)


{
One1Dto2DArray c=new One1Dto2DArray();
}

Q7) WRITE A CONSTRUCTOR TO ASSIGN 2D ARRAY TO 1D ARRAY?

package constructors;

public class TwoDtoOneD


{
TwoDtoOneD()
{
int[][] a={ {1,2},{3,4}};
int[] b=new int[4];
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
b[i]=a[i][j];

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

}
}

}
public static void main(String[] args)
{
TwoDtoOneD t=new TwoDtoOneD();

Q9)WRITE A CONSTRUCTOR TO CHECK THE GIVEN STRING IN RAICHUR?

class Stringpresent {

Stringpresent (){

String[] strvalue= new String[]{"gadag", "bangalore", "raichur"};

List<String> list = Arrays.asList(strvalue);


if(list.contains("raichur"))
{
System.out.println("dist as raichur");
}

}
class Mainstring
{
public static void main(String[] args)
{
Stringpresent S=NEW Stringpresent ();
}
}

Q10)WRITE A CONSTRUCTOR TO VERIFY AMONG 2 NUMBER PARAMETERS WHICH ONE IS BIGGER?

CLASS BIGGERNO
{
BIGGERNO()
{
INT A=10;
INT B=5;
INT BIGVALUE;
IF(A>B)
{

BIGVALUE=A;
}
ELSE
{
BIGVALUE=B;
}
}

}
CLASS BIGGERVALUE
{
PUBLIC STATIC VOID MAIN(STRING[] ARGS)
{
BIGGERNO B=NEW BIGGERNO();
}
}

Q11)WRITE A CONSTRUCTOR TO VERIFY THE ARRAY IS SORTED OR NOT?

EX:INT ARR[]={1,5,7,9,10}
CLASS SORTARRAY
{

SORTARRAY()
{
int[] arr={1,5,7,9,10};
int temp=0;
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println(":"+arrays.tostring(a));
}
}
CLASS ARRSORT
{
PUBLIC STATIC VOI MAIN(STRING[] ARGS)
{
SORTARRASY S=NEW SORTARRASY();

}
}

Q12)WRITE A CONSTRUCTOR TO VERIFY BOTH ARRAYS ARE SAME?


INT A[]={1,3,4,7,9}
INT B[]={2,5,6,8,10}

CLASS SAMEARRAY
{
SAMEARRAY()
{
int[] arr1 = new int [] {1,3,4,7,9};
int[] arr2 = new int [] {2,5,6,8,10};
if(arr1==arr2)

System.out.println("equal arrays");

else

System.out.println("not equal");

}
class ArraysEqual
{
public static void main(String[] args)
{
SAMEARRAY s=new SAMEARRAY();
}
}

Q13)FIND THE LENGTH OF THE ARRAY WITHOUT METHOD?

class Arrayslength
{
public static void main(string[] args)
{

int[] arr={1,2,3,4,5,6};
int i=0;
for(int arrays:arr)
{
i++;
}
system.out.println("arrays of length:"+i);
}

Q14)PRINT THE BELOW PATTERN


1
2 3
4 5 6
class Pattern
{
public static void main(String[] args)
{
int value=1;
for(int r=1;r<=n;r++)
{
for(sp=1;sp<=n;sp++)
{
System.out.println(" ");
}
n--;
for(int p=1;p<=r;p++)
{
System.out.println(":"+value);
value++;
}

}
}
}

Q15)WHAT IS CONSTRUCTOR WHY IS REQUIRED?


Ans:-constructor is a block of code to intialize the object.

*it can not return the values.

*constructor can not contain the abstract class,final,static,


synchronized while method can be.

*They should be declared in the public section.

*They are invoked directly when an object is created.

*They don't have return type, not even void and hence can't return any values.

*They can't be inherited; through a derived class, can call the base class
constructor.

*constructor eliminates placing the default values.

*The constructors can be called explicitly or implicitly.

*Uses of Parameterized constructor: It is used to initialize the various data

*elements of different objects with different values when they are created.

*It is used to overload constructors.

*CONSTRUCTOR IS used to initialise the values of the instance variables.

*when object the class is created , its constructor is called first (default
constructor)
by the JVM.

*The constructor sets the initial values of the instance variables

ex:-

class myclass

{
MYCLASS()
{
SYSTEM.OUT.PRINTLN("CONSTRUCTOR");
}
}
CLASS MYCONSTRUCTOR
{
public static void main(string []args)
{
MYCLASS m=new MYCLASS();
}
}

Q8)WRITE A CONSTRUCTOR TO SWAP A UNEQUAL ARRAYS?


EX: INT A[]={1,2,3};
INT B[]={5,6,7,8,9,10};

Вам также может понравиться